Jump to content

Keep value of selectbox if there is an error on the form


mike3075

Recommended Posts

I have a select box on a form that is populated with the following function:

function getStates(){
  // OPEN CONNECTION TO MYSQL ON LOCALHOST
  $dbhost = 'localhost' ;
  $username = 'root' ;
  $password = '' ;
  $conn = mysqli_connect("$dbhost", "$username", "$password");
  if (!$conn) {
    die('Could not connect: ' . mysqli_error()); 
  }
  mysqli_select_db($conn, "mydb"); 
  $retval = mysqli_query($conn, "SELECT * FROM tblstates") or die("Error: " . mysqli_error($conn));
  echo "<option value=''>Select one...</option>";
  while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
    $rec = $row['name'];
    echo "<option value = " . $row['name']. ">";
    echo $row['name'];
    echo "</option>";
  }
}

On the form I have several text boxes and the following select box:

<div class="formfieldgroup">
  <label class="label">Birth State/Country: <span class="required">&#149;</span></label>
  <select class="fatherbirthstate" name="varfatherbirthstate" tabindex="9"><?=getStates()?></select>
  <span class="form-error2"><?= $varfatherbirthstateErr ?></span>
</div>

TESTING: I intentionally leave a text box blank so I will get an error and I make sure to make a selection from the select box.

PROBLEM: When the form is submitted, the select box doesn't retain the value selected and is reset to "Select one..."

QUESTION: How do  I go about retaining the selected value when the form errors? 

Link to comment
Share on other sites

You need to compare the values to what was submitted as you generate the options and add the selected attribute to the one they chose.  So you'd end up with one of the options looking like:

<option value="blah" selected>Blah</option>

One method for accomplishing that would be to add a parameter to your getStates() function that indicates which item to select.  Then when you call it pass in their post value.

Link to comment
Share on other sites

What kicken said ^

 

Have your function take an optional argument for the current/default/selected/whatever you want to call it, value. In the while loop, mark the <option> as selected if it matches.
Then your script calls the function with whatever value came from the form - if there was one.

Link to comment
Share on other sites

Thank you both for the quick response.  I tried changing the While statement in the function to following, but it didn't work. It still did the same thing.

  while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
    $rec = $row['name'];
    echo "<option value = " . $row['name']. " if (isset($varfatherbirthstate) && $varfatherbirthstate == " . $row['name']. ") echo 'selected' >" . $row['name']. "</option>";
  }

Please forgive me if I'm way off on this. I am still fairly new to PHP

Link to comment
Share on other sites

your function should not be responsible for making a database connection or querying for the choices. those are the responsibility of your main application code.

i would make your function general-purpose, reusable, and supply an array of option choices, with id, label entries, and an optional array, to support the multiple attribute, of currently selected choices.

Edited by mac_gyver
Link to comment
Share on other sites

mac_gyver, to be honest with you your 2nd paragraph went over my head.  I have a select box on another form that works fine

<select class="myselectbox" name="varposition" id="varposition" tabindex="5"><option value="" selected disabled>Select one...</option><option value="Chaperone" <?php if (isset($varposition) && $varposition == "Chaperone") echo "selected" ?>>Chaperone</option><option value="Class Monitor" <?php if (isset($varposition) && $varposition == "Class Monitor") echo "selected" ?>>Class Monitor</option></select>

I suppose I could modify it for each state, but I has hoping there would be an easier way

Link to comment
Share on other sites

Thank all of you that responded so quickly. For now, I just used the code from another form instead of using the function to populate the select box so I could get the form up and running.  It's bulk but it works for now.  When I gain more experience and knowledge I will go back and work on it.  Again, thanks to everyone for the advice and suggestions!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.