PHP drop down coding question
Can someone show me how to make a If statement where if a drop down does not equal the default value of 0 (in my case) to automatically submit the form if possible?
session_start();
$current = isset($_SESSION['ClientNamefour']) ? $_SESSION['ClientNamefour'] : 0;
while ($row = mysql_fetch_array($result)) {
$id = $row["Client_Code"];
$thing = $row["Client_Full_Name"];
$value = "$id, $thing";
$sel=($id==$current)?'SELECTED':'';
$options4.="<OPTION $sel VALUE=\"$value\">$thing</option>";
}
?>
<FORM name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
&开发者_开发问答lt;SELECT NAME="ClientNamefour" OnChange="this.form.submit()"
)">
<OPTION VALUE=0>Client
<?php echo $options4?>
</SELECT>
</FORM>
Use the onchange
event and some JavaScript. In general your generated HTML should look something like this:
<form id="form" name="form" method="POST" action="http://example/script.php">
<select id="select" name="select" onChange="document.getElementById('form').submit();">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</form>
The onchange
event is only fired when you select an unselected option, so no additional checks are required.
Compared with your solution:
- the
id
is missing in your<form>
tag - you need closing
</option>
tags - you'd probably need to change the JavaScript in the
onchange
attribute of the<select>
tag
My understanding is that you can't replicate someone pushing a "submit" button using a server side language such as PHP. You could use javascript to submit the form if the user changes it (to something other than 0)
However, it looks like you only want the form to display if the SQL query returns a result that isn't 0?
In that case I'd do something like this...
Run your SQL Query
Code something like this:
if($option != 0){ //Function to do whatever happens after the form is sent (add to a database etc) } else{ //display the form }
This is really sort of a javascript question... add an id attribute to your form like <form id="myfrm"...>,
then after the </form>
put:
<?php
if(((int) $_SESSION["your_session_variable"]) != 0){
?>
<SCRIPT language="javascript">
document.getElmenetById("myfrm").submit();
</SCRIPT>
<?ph } ?>
精彩评论