How to use multiples $ _GET in PHP through a URL

2

Through a URL, I want to display a set of information in the page's HTML, however, there are values I want to repeat in certain URL's and not in others.

Example:

link

<?php echo "<div class='campo1'>" . $_GET['c'] . "</div>" ;?>  

I want to repeat the $_GET['c'] in the URL, and automatically generate a new DIV with the new value passed by the URL

link

What I can not do is hit PHP in HTML. I've tried duplicating the echo function above, but if I do not enter the value of her GET in the URL, the DIV is displayed anyway (blank), something I do not want to happen. How should I proceed?

    
asked by anonymous 21.11.2014 / 14:56

3 answers

2

Just make a condition using the isset function that checks whether the variable is set and is not null.

if(isset($_GET['c']))
    echo "<div class='campo1'>" . $_GET['c'] . "</div>" ;
    
21.11.2014 / 15:05
2

You should do the following:

<?php 
    echo isset($_GET['c']) ? "<div class='campo1'>" . $_GET['c'] . "</div>" : ""; 
?> 

or

<?php 
    if(isset($_GET['c'])){
       echo  "<div class='campo1'>" . $_GET['c'] . "</div>"; 
    }
?> 
    
21.11.2014 / 15:09
1

There is also a way to hide the div by CSS when it is empty.

Just use

<style>
#campo1:empty{
  display:none;
}
</style>

I would just like to draw attention to the care you should take when displaying content coming from user data.

Because the code below leaves your code vulnerable to xss (cross-site scripting) injections:

<?php 
    echo isset($_GET['c']) ? "<div class='campo1'>" . $_GET['c'] . "</div>" : ""; 
?> 

If this really goes into production (and is not just a test, for study purposes), I recommend you do it as follows

<?php 
  echo isset($_GET['c']) ? "<div class='campo1'>" . htmlentities($_GET['c']) . "</div>" : ""; 
?>

This way you prevent someone from entering a javascript in the url and inject codes into your page.

    
21.11.2014 / 16:58