Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating list of <select> options using jquery and ajax

I am trying to make an html select list of options update according to a selection made on a prior html select object. My jquery is below. This is being called correctly.

var brandName = $("#Brand").val();

$.get("updateTypes.php?q="+brandName, function(data) {

    $("#Type").remove();
    var typeData = JSON.parse(data);

    for (loop=0; loop < typeData.length; ++loop) {
        $("#Type").options.add(new Option(typeData[loop]));
    }
});

As I am using a singleton to interface with my mySQL database, this jquery function calls a 'go-between' .php file called updateTypes.php which is below:

include 'databaseInterface.php';
$brand = $_GET["q"];
$typesData = databaseInterface::getBrandTypes($brand);
return $typesData;

This calls the getBrandTypes function in my singleton below:

$query = "SELECT psTypeName FROM types WHERE brands_psBrandName='$BrandName'";
$result = mysqli_query($con, $query) or die ("Couldn't execute query. ".mysqli_error($con));
$resultArray = array();
while ($row = mysqli_fetch_assoc($result)) { 
    extract($row);
    $resultArray[] = $psTypeName;   
}

return json_encode($resultArray);

The webpage correctly removes the existing options from the jquery function but fails to update them. It seems to go wrong when I decode the JSON data in the jquery. Why is it going wrong? Is the loop for updating the select object appropriate?

like image 816
Ben Thompson Avatar asked Jun 29 '13 21:06

Ben Thompson


People also ask

How do I update form data in Ajax?

Update Data using Ajax Create a custom function with id parameter and assign it to a variable editData. This function will execute when you click the edit button then an update form will be loaded with value based on passing id.

How do I change the select element after another select element?

For example: You might want to change the SELECT element after another SELECT element has been changed. In that case, you would modify the code above and wrap it inside the onchange event handler. Inside the success handler of our AJAX request, we used the $.each function to loop through each element in the JSON array that our PHP script returned.

How do I update a database in PHP?

Update Data Using PHP Include the database connection file. Create a custom edit_data () to fetch data on the basis of a particular id. Create another custom function update_data () to update data using PHP and MySQL.

How to get all city names in Ajax response from city table?

The same procedure here but we replace the name of the function if we select any state name that causes the state_change () function, Inside ajax { ( )} function, sends selected state id to request to 'city.jsp' page using the HTTP POST method to get all city names in ajax response from city table.


1 Answers

You can use $.getJSON if your expecting a json response. You might also be able to use $.each() and then simply .append() to the select tag. You can reference this.property inside the .each().

Something like the following:

$.getJSON("updateTypes.php?q="+brandName, function(data) {
    $("#Type").html('');
    $.each(data, function(){
        $("#Type").append('<option value="'+ this.value +'">'+ this.name +'</option>')
    )
})

This would assume your json response is something like the following:

[ { name : "foo", value : "bar" }, { name : "another", value : "example" } ]

like image 187
Shawn Northrop Avatar answered Sep 22 '22 07:09

Shawn Northrop