简体   繁体   中英

Adding values (by user) to drop down list populated from db

I'm trying to create an "editable" drop down list. I have a drop down list populated from a table "city" in my db, it works perfect but now I want user to be able to add a new value to the list if it doesn't exist there (and I want to save this value, so next time it appears in the drop down list). Any ideas? Thank you

        <select name="city"> 
    <?php 
    // connecting to DB
    $con=  mysqli_connect('localhost','root','1234','e-sage');
    // enable hebrew input
    mysqli_set_charset($con,'utf8');
    // read the city table from DB
    $sql = mysqli_query($con,"SELECT CityName FROM city");
    while ($row = mysqli_fetch_array($sql)){
    // creating temporary variable for each row
    $city1=$row["CityName"];
    //assigning a value to each option in dropdown list
    echo "<option value=\"$city1\"> $city1 </option>";
    }
    ?>
    </select>

You can provide a 'other' selection in the dropdown. If user selects that then display a new textbox from which you can insert in the database.

use datalist tag

link to w3School tutorial

Then try this: Lets agree that you already got all the cities in array OK?

<select name="city">
<?PHP
foreach($cities as $city)
{
echo '<option value="'.$city.'">'.$city.'</option>';
}
?>
</select>

<form method="post">
<label for="new_city">New city</label>
<input id="new_city" name="new_city" placeholder="Enter new city name" />
<input type="button" id="addCity" value="Insert city" />
</form>



  <script>
        $(document).on('click','#addCity',function()
                       {

                          var new_city=$("#new_city").val();
                          var e=0;
                           $("[name='city'] option").each(function(i,item)
                                                          {
                                                              var check_city=$(item).val();

                                                              if(check_city==new_city)
                                                              {
                                                                 e++;
                                                              }
                                                              else
                                                              {

                                                              }

                                                          });
                           if(e>0)
                           {
                               alert('The same');
                           }
                           else
                           {
                               /*Submit the form*/
alert('This city is fresh and new');

                           }


                       });
    </script>

So the script is checking if the value is already assigned to option. If not - allows to submit the form.

Check this one: http://jsfiddle.net/n47N2/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM