I have the following piece of code which resembles a form with a drop-down list. 开发者_开发技巧The selected item from the list should be sent to process.php page which will do some work.
<form name="form" method="GET" action="process.php">
<div id="extra-components">
<select name="boolean">
<option value="and" selected="yes">AND</option>
<option value="or">OR</option>
<option value="not">NOT</option>
</select>
</div>
</form>
My question is: in the process.php file how do I obtain the selected value from the form? via the $_GET method? Thanks in advance
If this is all the code of the form, it probably will not work. What you have forgotten is some submit button or at least some JS code to submit form without the submit button.
The proper code would be something similar to this (notice the additional 'submit' input):
<form name="form" method="GET" action="process.php">
<div id="extra-components">
<select name="boolean">
<option value="and" selected="yes">AND</option>
<option value="or">OR</option>
<option value="not">NOT</option>
</select>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
And then, after clicking "Submit", the data will be sent to the process.php script and should be accessible by using:
$_GET['boolean']
Plus, it should be some value from the following strings:
- 'and'
- 'or'
- 'not'
Try it - it should not hurt ;) The example solution:
if (isset($_GET['boolean']) && in_array($_GET['boolean'], array('and', 'or', 'not'))) {
$chosen_boolean = $_GET['boolean'];
} else {
$chosen_boolean = null;
}
Then if the
$chosen_boolean === null
that means that the value given was inproper or not supported (not from the available options). That should help you avoid any errors regarding the inproper value for this.
Depending on your HTML code you can access boolean
value by $_GET['boolean']
. $_GET['boolean']
will contain one of <option>
's value
element until user will change it in his URL.
Esit:
Tip: Don't forget to filter user-submitted data, always. HTML, JS restrictions are nothing. 'Always assume that user is malicious' (@MarkB)
Yes. The value of the selected option will be passed as the value of the select parameter. So with this code, you'd get
$_GET['boolean']
set to 'and'
Yes, you find the content in $_GET['boolean']
.
精彩评论