Moorcam Posted March 18, 2021 Share Posted March 18, 2021 Hi guys, Before we go further, yes, I know I should use prepared statements. This is just a project that will probably never go live. If I do decide to go live, I will change to prepared statements. Anyways, I am populating a text box from the selection of an options dropdown and updating MySQL. This all works fine. However, what I want to do is have the newly inserted option display by default in the dropdown. Hope this makes sense. Here is the dropdown code with the select statement... <div class="row form-group"> <div class="col-6"> <div class="form-group"><label for="location" class=" form-control-label">location</label> <?php $sql = "SELECT location_name, location_phone FROM locations"; $result = $con->query($sql); ?> <select name="location" id="location" onchange="myFunction()" class="form-control"> <?php while($r = mysqli_fetch_row($result)) { echo "<option data-location_name='$r[1]' data-location_phone='$r[2]' value='$r[0]'> $r[0] </option>"; } ?> </select> <label>Phone</label><input type="text" class="form-control" name="location_phone" id="location_name" value = "<?php echo $row['location_phone']; ?>"/> I am grabbing the location name and phone number from locations. Then inserting them into tours. So after the form is submitted, I want the location name to stay in the dropdown. This is in the table "tours". How do I implement that in here? Thanks heaps. Quote Link to comment https://forums.phpfreaks.com/topic/312321-changing-default-value-of-dropdown/ Share on other sites More sharing options...
requinix Posted March 18, 2021 Share Posted March 18, 2021 1. Figure out which one was just submitted. Best method would be to create the location, note the location ID (you have an ID, right?), and check $r for a match (after you've added that ID column to the query). 2. As you output <option>s, mark the new one as selected. <option data-location_name="..." data-location_phone="..." value="..." selected> Quote Link to comment https://forums.phpfreaks.com/topic/312321-changing-default-value-of-dropdown/#findComment-1585182 Share on other sites More sharing options...
Moorcam Posted March 18, 2021 Author Share Posted March 18, 2021 (edited) Hi requinix, Thanks for the reply. When I add location_id to the query I only get the id number in the dropdown. <?php $sql = "SELECT DISTINCT location_id, location_name, location_phone FROM locations"; $result = $con->query($sql); ?> <select name="location" id="location" onchange="myFunction()" class="form-control"> <?php while($r = mysqli_fetch_row($result)) { echo "<option data-location_name='$r[1]' data-location_phone='$r[2]' value='$r[0]' selected> $r[0] </option>"; } ?> Sorry mate. Struggling with this one. Edited March 18, 2021 by DanEthical Quote Link to comment https://forums.phpfreaks.com/topic/312321-changing-default-value-of-dropdown/#findComment-1585186 Share on other sites More sharing options...
Phi11W Posted March 18, 2021 Share Posted March 18, 2021 1 hour ago, DanEthical said: When I add location_id to the query I only get the id number in the dropdown. I'm guessing that's because you told it to? while( $r = mysqli_fetch_row( $result ) ) { echo "<option data-location_name='$r[1]' data-location_phone='$r[2]' value='$r[0]' selected> $r[0] </option>"; // ^ ^ ^ ID!! // | | ID // | Phone // Name // } Trying putting the name ($r['location_id']) inside the option element, not the id or, rather, whatever happens to be the first column that your query retrieves ($r[0]). Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/312321-changing-default-value-of-dropdown/#findComment-1585187 Share on other sites More sharing options...
Moorcam Posted March 19, 2021 Author Share Posted March 19, 2021 Thanks mate. Still wasn't working for me. So, gave in and reverted back to good old text boxes. Quote Link to comment https://forums.phpfreaks.com/topic/312321-changing-default-value-of-dropdown/#findComment-1585218 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.