0

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>

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It was one of my options too, but then I decided to try to do it without opening new textbox... Thank you.
So how you wanna do it?
0

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/

2 Comments

Thank you very much, it does solve the problem! But I'm still wondering if I can do it within the same textbox, without having additional one for new option's insert?
Well... the solution would be to have like "Add new" as option to this select field, then after its clicked - javascript prompt is executed, data saved by AJAX to DB and then reload the Select list ;)
0

use datalist tag

link to w3School tutorial

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.