tags:

views:

37

answers:

1

Hi all, I'm learning php and I've got to say I've googled everywhere and I'm stuck :(

My question is, in other words, how can I get new variables added to previous variables on the page?

Imagine this being www.mysite.com/getvartest.php?orgvar=12345

but after submitting this form it'll go to

www.mysite.com/getvartest.php?varselection=selection1

What I would expect would be:

Imagine this being www.mysite.com/getvartest.php?orgvar=12345&varselection=selection1

How could I resolve this?

<p>Testing two ore more variables</p>

<form action="getvartest.php?orgvar=12345" method="get">
<select name="varselection">
<option value="selection1">selection1</option>
<option value="selection2">selection2</option>
</select>
<input type="submit" />
</form>


<?php
@$originalvar = $_GET['orgvar'];
@$varselection = $_GET['varselection'];

if($originalvar&&$varselection)
echo "Testing original variable $originalvar. Testing second passthrough variable through here: $varselection";


?>
+4  A: 

Instead of submitting to an action URL with part of the query string already filled out, use a hidden input field to hold the values of the variables you want to be fixed. See if you can get your PHP script to generate a form like this:

<form action="getvartest.php" method="get">
  <input type="hidden" name="orgvar" value="12345" />
  <select name="varselection">
    <option value="selection1">selection1</option>
    <option value="selection2">selection2</option>
  </select>
  <input type="submit" />
</form>

Perhaps something like:

<input type="hidden" name="orgvar" value="<?= htmlspecialchars($_GET['orgvar']) ?>" />

(The htmlspecialchars() call escapes characters like <, &, and " just in case there are any of these in orgvar.)

Or if you wanted to save every query parameter you could use a generic loop like:

<?php foreach ($_GET as $name => $value) { ?>
    <input type="hidden" name="<?= htmlspecialchars($name)  ?>"
                        value="<?= htmlspecialchars($value) ?>" /> 
<?php } ?>
John Kugelman
This is a great answer! It worked like a charm. Is this the only way to do it though? I mean it would seem tedious to do for every form I make. Then again as of right now I just need something that works. Thanks!
Tek
Nope, that's pretty much how everyone does it! Unless you want to turn it into a POST form, in which case the POST parameters don't affect the GET parameters.
bobince