Drop down resets value in a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cprescott1972
Forum Newbie
Posts: 2
Joined: Mon Mar 09, 2009 4:53 pm

Drop down resets value in a form

Post 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.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Drop down resets value in a form

Post 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.
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Drop down resets value in a form

Post 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>
 
cprescott1972
Forum Newbie
Posts: 2
Joined: Mon Mar 09, 2009 4:53 pm

Re: Drop down resets value in a form

Post 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";
 
?>
 
 
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Drop down resets value in a form

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