mike3075 Posted July 30, 2020 Share Posted July 30, 2020 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">•</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? Quote Link to comment Share on other sites More sharing options...
kicken Posted July 30, 2020 Share Posted July 30, 2020 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 30, 2020 Share Posted July 30, 2020 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. Quote Link to comment Share on other sites More sharing options...
mike3075 Posted July 30, 2020 Author Share Posted July 30, 2020 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 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 30, 2020 Share Posted July 30, 2020 (edited) 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 July 30, 2020 by mac_gyver Quote Link to comment Share on other sites More sharing options...
mike3075 Posted July 30, 2020 Author Share Posted July 30, 2020 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 Quote Link to comment Share on other sites More sharing options...
mike3075 Posted July 30, 2020 Author Share Posted July 30, 2020 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.