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
  • php.net/manual/en/reserved.variables.get.php Commented Dec 22, 2011 at 16:01
  • 1
    Use $_GET. If you're breaking down a query string from another location, use parse_str(). No need to explode and loop everything. Commented Dec 22, 2011 at 16:15

4 Answers 4

9

Why not using $_GET global variable?

foreach($_GET as $key => $value)
{  
  // do your thing.
}
Sign up to request clarification or add additional context in comments.

Comments

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

var_dump($queryArray);

4 Comments

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 ...
How would $_GET handle ?arr[]=foo+bar&arr[]=baz
$_GET['arr']; <- would be an array
In that case, it's just me being nerdy
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

Comments

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"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.