Page 1 of 1

Drop down resets value in a form

Posted: Mon Mar 09, 2009 5:04 pm
by cprescott1972
Hello! I am just getting started with PHP and I need some advice on a form I am doing. I am submitting a form and then if there are blank form fields it shows an error and the form to the user while keep any values they entered. No big deal. but the problem is I can't figure out how to pass a value for the dropdown. I am hoping that if a person selects something then that will stay selected when the page refreshes with an error. This is probably simple but I am not sure how to pass this $_POST[dropdown] variable back tot he form so it displays as selected. Any ideas?
Thanks.

Re: Drop down resets value in a form

Posted: Tue Mar 10, 2009 12:24 am
by susrisha
you can do that using javascript.

not sure if i can give the example.
dropdown.selectedIndex is the property to be set

So if your dropdown has values
1. value1
2. value2
3. value3,

Look for the value on load and get the index.

Re: Drop down resets value in a form

Posted: Tue Mar 10, 2009 5:16 am
by Paul Arnold
Try this.

Code: Select all

 
<select name="field_name">
<?PHP
     $getData = "SELECT * FROM table_name";
     $getData_q = mysql_query($getData, $connection);
     while($getData_r = mysql_fetch_assoc($getData_q)) {
?>
     <option value="<?PHP echo $getData_r['field']; ?>" <?PHP if($_POST['field_name'] == $getData_r['field']) { echo 'selected'; } ?>><?PHP echo $getData_r['field']; ?></option>
<?PHP
     }
?>
</select>
 

Re: Drop down resets value in a form

Posted: Tue Mar 10, 2009 6:25 pm
by cprescott1972
Thanks for the responses. I have been looking into it more and am discovering another issue.
The idea of checking the $_POST value for the form field and then selecting based on what is passed seems like the way to go. My snag is that the form is inside a string being assigned to a variable called $form_block. So when I insert an if statement inside the HTML it's nesting the PHP tags ( <? ?> ) and if I don't put the <? ?> in there it just prints out the if statement in the html output as if it's text. I tried eval() but it didn't work either. I think I messed up the syntax...
Here's the code before passing the "selected" attribute. Thanks very much.

Code: Select all

 
 
<?
$form_block ="
<FORM METHOD=\"POST\" ACTION=\"$_SERVER[PHP_SELF]\">
<table width=\"576\" cellspacing=\"0\" cellpadding=\"0\">
      <tr>
        <td><label for=\"inquiryTypeId\">type of inquiry:</label><br />
          <select name=\"inquiryType\"   id=\"inquiryTypeId\">
            <option value=\"general\">general</option>
            <option value=\"pr inquiry\">pr inquiry</option>
            <option value=\"other\">other</option>
          </select>
        </td>
      </tr>
      <tr>
        <td><input type=\"submit\" style=\"margin-top:15px;\" /></td>
      </tr>
    </table>
    
    <INPUT type=\"hidden\" name=\"op\" value=\"ds\">
</FORM>
 
";
 
if ($_POST[op] != "ds") {
 
    //they need to see the form
    echo "$form_block";
 
?>
 
 

Re: Drop down resets value in a form

Posted: Wed Mar 11, 2009 5:11 am
by Paul Arnold
Theoretically :| the following should display your form if it has or hasn't been posted (leaving you with scope to validate the submission).
It should also hold onto the submitted drop down value.

Also, try to use <?PHP rather than <? as some servers don't support short tags making your code less portable.

Code: Select all

 
 
<?PHP
if($_POST[op] != "ds" || !$_POST) {
?> 
<FORM METHOD="POST" ACTION="<?PHP echo $_SERVER[PHP_SELF]; ?>">
<table width="576" cellspacing="0" cellpadding="0">
      <tr>
        <td><label for="inquiryTypeId">type of inquiry:</label><br />
          <select name="inquiryType"   id="inquiryTypeId">
            <option value="general" <?PHP if($_POST['inquiryType'] == 'general') { echo 'selected'; } ?>>general</option>
            <option value="pr inquiry" <?PHP if($_POST['inquiryType'] == 'pr inquiry') { echo 'selected'; } ?>>pr inquiry</option>
            <option value="other" <?PHP if($_POST['inquiryType'] == 'other') { echo 'selected'; } ?>>other</option>
          </select>
        </td>
      </tr>
      <tr>
        <td><input type="submit" style="margin-top:15px;" /></td>
      </tr>
    </table>
    
    <INPUT type="hidden" name="op" value="ds">
</FORM>
<?PHP } ?>