I have created this form: <form action="/import.php" method="post"> <p><input type="text" name="site_import" id="site_import" value="" /><input type="submit" value="Import" /></p> </form> Code (markup): Now, if I enter a url into the box: http://www.awebsite.com/?apples=1&oranges=2&pears=3 Code (markup): I want the form to make a file that has this in it: <?php $apples = "1"; $oranges = "2"; $pears = "3" ?> Code (markup): How can I do this?
This is what I have so far, but it's not working out to good: <?php $pieces = explode('&', $_POST['site_import']); foreach ($pieces as $p) { list($name, $value) = explode('=', $p); $_POST['$name'] = $value; echo "\$".$name."=".$value."\n\n"; } ?> Code (markup):
$urlParts = explode('&', $_SERVER['QUERY_STRING']); foreach ($urlParts as $urlPart) { $parsedPart = explode('=', $urlPart); global $$parsedPart[0]; $$parsedPart[0] = $parsedPart[1]; } echo "Apples: $apples <br/>"; echo "Oranges: $oranges <br/>"; PHP: index.php?oranges=1&apples=3 Code (markup): Not one of the cleanest ways of doing it but you should get the idea.
There are a few built-in functions for this. One grabs the querystring from your URL, the other parses the querystring. http://www.php.net/manual/en/function.parse-url.php http://php.net/manual/en/function.parse-str.php