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
console.log(suburb);between thevarand$.ajaxlines, what do you see in the console?get.phpthat answers the ajax requests.var suburb = ...but never uses it? Felix shows how to use it so the code doesn't repeat itself.