0

I have a form which will populate a select box "city" based on the selections of another select box "suburb". This works for my data.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script>
$(document).ready(function()
{
    $("#suburb").change(function(){
        var suburb = $(this).val();//get select value
        $.ajax({
            url: "get.php",
            type: "post",
            data: { suburb: $(this).val() },
            success: function( responce ){
                $("#city").html( responce );
            }
        });
    });
});
</script>
Suburb:

<select id="suburb">
    <option id="Williamstown">Williamstown</option>
    <option id="Altona">altona</option>
    <option id="Newport">newport</option>
 </select> 
 <select id="city"></select>

Instead of having the <select id="suburb"> box I would like to make it a text input <input type="text" name="suburb"> and use the value in the input text field to populate the city select box.

I have tried changing it to

<script>
$(document).ready(function(){
    $("#suburb").keyup(function(){
        var suburb = $("#suburb").val(); //get select value
        $.ajax({
             url: "get.php",
             type: "post",
             data: { suburb: $("#suburb").val() },
             success: function( responce ){
                  $("#city").html( responce );
             }
        });
    });
});
</script>
Suburb:

 <input type="text" name="suburb">
 <select id="city"></select>

But this doesn't return anything. Im sure Im not reading the input in the "suburb" field correctly

5
  • If you add console.log(suburb); between the var and $.ajax lines, what do you see in the console? Commented Apr 9, 2014 at 5:37
  • What should happen if the suburb name the user enters isn't available in the select box? Commented Apr 9, 2014 at 5:38
  • Please post the PHP code in get.php that answers the ajax requests. Commented Apr 9, 2014 at 5:40
  • Felix's answer below works for me Commented Apr 9, 2014 at 5:44
  • Do you realize that the code initializes var suburb = ... but never uses it? Felix shows how to use it so the code doesn't repeat itself. Commented Apr 9, 2014 at 5:44

2 Answers 2

1

Since you give your input name attribute, you can target it by name instead of id:

$(document).ready(function () {
    $('input[name=suburb]').keyup(function () {
        var suburb = $(this).val(); //get select value
        $.ajax({
            url: "get.php",
            type: "post",
            data: {
                suburb: suburb
            },
            success: function (responce) {
                $("#city").html(responce);
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

<script> $(document).ready(function() { $("#suburb").keyup(function() { var suburb = $("input[name=suburb]").val();//get select value $.ajax( { url:"get.php", type:"post", data:{suburb:$("input[name=suburb]").val()}, success:function(responce) { $("#city").html(responce); } }); }); }); </script> This is what I'm using, and still not returning anything
ok thanks this works.. I have to wait 5 minutes to accept your answer
0

try this

<script>
$(document).ready(function()
{
$("#suburb").blur(function()
   {
     var suburb = $("#suburb").val();//get select value
     $.ajax(
     {
     url:"get.php",
     type:"post",
     data:{suburb:$("#suburb").val()},
     success:function(responce)
    { $("#city").html(responce);}
    });
    });
  });
</script>
Suburb:

<input type="text" name="suburb">
<select id="city"></select>

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.