How do you read and pass the selected option from a dropdown?
I dont want to use the querystring as I need it in a hidden text field that I can write to a cookie.
Thanks
Printable View
How do you read and pass the selected option from a dropdown?
I dont want to use the querystring as I need it in a hidden text field that I can write to a cookie.
Thanks
What do you want? A hidden field or a drop down? Whatever you want you need to enclose the value in a POST form and give it a name.
Then access it in the code using:
Code:$_POST['varName'];
On a page I have a couple of dropdowns. When its submitted the selected values need to be transferred to the same page upon reloading and update the selections in the database too.
The HTML form element has an action attribute - set this to the name of the PHP script or $_SERVER['PHP_SELF'] in your case. If you give the submit button a name you can then check for this when the form is submitted.
I wouldn't recommend submitting to the same page though, as this causes those nasty "would you like to re-submit the data" dialogs when you press the back button.PHP Code:<?php
if (isset($_POST['submit']])) {
// process form here
}
?>
<html>
<body>
<form action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post">
<p><input type="submit" name="submit" /></p>
</form>
</body>
</html>
The best approach IMO is to have IN and OUT scripts like variable in PLSQL. The OUT scripts produce HTML and the IN scripts process posted form data. So the flow would go as follows:
Code:myform.php ---> POST --> myform_in.php
^ |
| |
yes |
| R |
| E V
|----- error <---D-- [process data]
| I
| R
no E
| C
| T
V
results.php
I use a If on the same page but everytime a user needs to update a record it should just reload the page to show the update. the form is nested in a per record basis in order to identify which record is being updated.
The manage is the do parameter of the action element "action=myfile.php?do='manage'"
Currently doing...
But if the page needs to remain and let them "manage" it then it seems too many redirects your way?PHP Code:if ($_REQUEST['do'] == 'manage')
{
//Stuff
}
The redirection is almost transparent to the user if you use a Location header. You may also want to consider using caching headers to prevent the dynamic data from being recalled from the browser cache.
See: http://www.php.net/header
But what about reading the selected value from the dropdown?
PHP Code:<form action="page1.php?do=manage" method="post">
<div>
<select name="ddl1" />
<option value=""> </option>
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
</div>
<input type="hidden" name="ddl1" value="???" />
</form>
I do this too. The scripts that handle non-idempotent requests produce 303 redirections to their eventual location (static HTML page, or PHP-generated HTML).Quote:
Originally Posted by visualAd
$_POST['ddl1'] will contain the selection value: either blank, '1', '2', or '3'.Quote:
Originally Posted by RobDog888
I am sure I described how to do this earlier :confused:
There is no need for a hidden one if you are auto selecting the blank one. :)
If you post to a separate file, and then redirect to the original file, won't you lose the sent data? You can't repopulate the form with the data? (unless you pass it back in the url? ($_GET)).Quote:
Originally Posted by visualAd
That is correct, this is where sessions will come in handy as you can repopulate the data from the session variables in addition to any error messages you may / may not wish to display.
I suppose that would be one method. And then clearing it so you don't keep populating the form with the most recent data? If the user was to go to the same page again?
Yes, I've made a form class to do this all for me. :D