8

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="">';
    }   
} 
2

4 Answers 4

9

Why not using $_GET global variable?

foreach($_GET as $key => $value)
{  
  // do your thing.
}
0
7
$queryArray = array();
parse_str($_SERVER['QUERY_STRING'],$queryArray);

var_dump($queryArray);
4
  • Just for me ... why would you use parse_str() over $_GET ? i can understand the nerd to use it if the string you want to parse if not the url used to call the executing php ...
    – Manse
    Dec 22, 2011 at 16:29
  • How would $_GET handle ?arr[]=foo+bar&arr[]=baz
    – Mark Baker
    Dec 22, 2011 at 16:32
  • $_GET['arr']; <- would be an array
    – Manse
    Dec 22, 2011 at 17:37
  • 1
    In that case, it's just me being nerdy
    – Mark Baker
    Dec 22, 2011 at 17:50
4

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

1

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"

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.