Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/312321-changing-default-value-of-dropdown/
Share on other sites

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>

 

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 by DanEthical
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.

 

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.