1

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">

<SELECT NAME="ClientNamefour" OnChange="this.form.submit()"
)">
  <OPTION VALUE=0>Client
   <?php echo $options4?> 
</SELECT>
</FORM>
1
  • Is there more to your form than this one option? Commented Jan 22, 2010 at 16:47

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

The only thing I'd suggest doing is setting the onChange to call a function, and then have the function check the value of the selection box, just to ensure it's not 0. ex. onChange="selectionChanged();" function selectionChanged() { if (document.getElementById('select').value!="0") { document.getElementById('form').submit(); } } You could also do this inline, but it's less pretty: onChange="if (this.value!="0") { document.getElementById('form').submit(); }"
The OP stated that it should not match the default value, i.e. the selected value. onchange is exactly doing this.
1

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...

  1. Run your SQL Query

  2. 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
    }
    

6 Comments

You're correct, there isn't a method of simulating a POST. Well unless you generate a seperate cURL request, but that would be madness for this.
Submitting forms happen in the browser, where it is perfectly possible to submit POST requests. And when looking at the code, a drop-down menu is created to select one of the available users/clients for the web service.
Yeah he could use javascript etc (You can't make PHP submit a form though?), but it looks like he only wants the form to display if the SQL query equals 0? If the SQL result is great than 0 then he wants to do something else with it. Why bother displaying/loading the form if it isn't necessary?
@vegar I am well aware of that. I was stating that it's not really possible to simulate a POST from the serverside (without something like cURL).
He uses the SQL to dynamically build a drop-down menu containing the users. When one is selected the page is probably changed (which is removed) showing the details of that user (or something similar) and the drop-down menu shows the newly selected user.
|
0

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 } ?>

4 Comments

I initially thought the OP could use JS as well, but I think the problem is with how he handles the SQL result, rather than how the form is sent? (I.e. He either wants to do something with the result if it is not equal to 0, or he wants a form to display). Maybe I'm reading this wrong?
@Matt, I guess my point was that the most you can do on the server side is to generate javascript via PHP. The question ought to really better describe what the desired sequence of events is.
What Im trying to solve is im getting a session variable after submitting the form/drop down above. What I want to do is if that session variable is not equal to 0 i want to make that form auto submit.
@Eric, editing my post now to do what I think you're asking. I gather that you mean, you get to this page and you want to present this form with user feedback only if this session variable is not equal to zero, otherwise you basically want to pass through to the next page. There might be a more elegant way to do this without javascript by combining this page and your next page into a single php page and switching on your session variable on the server side to decide which content to serve up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.