Home » Php » arrays – PHP Get URL Parameter and its Value

arrays – PHP Get URL Parameter and its Value

Posted by: admin July 12, 2020 Leave a comment

Questions:

How do I split the URL and then get its value and store the value on each text input?

URL:

other.php?add_client_verify&f_name=test&l_name=testing&dob_day=03&dob_month=01&dob_year=2009&gender=0&house_no=&street_address=&city=&county=&postcode=&email=&telp=234&mobile=2342&newsletter=1&deposit=1

PHP:

$url = $_SERVER['QUERY_STRING'];
$para = explode("&", $url); 
foreach($para as $key => $value){   
    echo '<input type="text" value="" name="">';    
} 

above code will return:

l_name=testing  
dob_day=3  
doby_month=01  
....

and i tried another method:

$url = $_SERVER['QUERY_STRING'];
$para = explode("&", $url); 
foreach($para as $key => $value){   
    $p = explode("&", $value);
    foreach($p as $key => $val) {
       echo '<input type="text" value="" name="">';
    }   
} 
How to&Answers:

Why not using $_GET global variable?

foreach($_GET as $key => $value)
{

…..

}

Answer:

$queryArray = array();
parse_str($_SERVER['QUERY_STRING'],$queryArray);

var_dump($queryArray);

Answer:

php has a predefined variable that is an associative array of the query string … so you can use $_GET['<name>'] to return a value, link to $_GET[] docs

You really need to have a look at the Code Injection page on wikipedia you need to ensure that any values sent to a PHP script are carefully checked for malicious code, PHP provides methods to assist in preventing this problem, htmlspecialchars

Answer:

You should use the $_GET array.

If your query string looks like this:
?foo=bar&fuz=baz

Then you can access the values like this:

echo $_GET['foo']; // "bar"
echo $_GET['fuz']; // "baz"