Jump to content

list selection and pass value as varibale in URL


Darkmatter5

Recommended Posts

I need to have a dropdown list and a submit button that when a selection is made in the list and the submit button is clicked the current page is reloaded with the value of the selection of the list.

 

Something like this:

 

<form method="post">
<select name='pet' tabindex='2'>
  <option>---Select---</option>
  <option value='1'>dog</option>
  <option value='2'>cat</option>
</select>
<input name='load' type='submit' onclick="editpet.php?pet=$_POST["pet"]" value='Load record' />
</form>

 

The reason for all this is I want when the page is reloaded to then display with the selection from the list selected in the list. So if I can get the value to pass on the reload I have then write code to have that selection be selected in the list. Any thoughts? Any suggestions?

<?php
// If the form has been submitted...
if(isset($_POST['load']))
{
// Set $name as the value of the name field in your form ($_POST just references the form)
$pet = $_POST['pet'];
echo $pet;
}
// If the form hasn't been submitted yet...
else
{
// Display the form
echo '<form method="post">
        <select name="pet" tabindex="2">
        <option>---Select---</option>
        <option value="1">dog</option>
        <option value="2">cat</option>
        </select>
        <input name="load" type="submit" value="Load record" />
</form>';
}
?>

Should do the trick. Let me know if you need any more help with how that code works.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.