Skip to content

Instantly share code, notes, and snippets.

@mazfreelance
Created January 14, 2020 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazfreelance/cc343e85d8c2433112637338e258723a to your computer and use it in GitHub Desktop.
Save mazfreelance/cc343e85d8c2433112637338e258723a to your computer and use it in GitHub Desktop.
append data to selection form using ajax
$.ajax({
url: '../example.php',
type: 'post',
data: {},
success: function( data, textStatus, jQxhr ){
// console.log(data);
var itemID = <?php echo json_encode($item_id); ?>;
// console.log(itemID);
$.each(data['data'], function(key, value) {
$('select#select_item').append($("<option></option>").attr("value",key).text(value)).trigger('change');
if(itemID){
$.each(itemID, function(id, item) {
if(key == item){
$('select#select_item option[value="'+item+'"]').prop("selected", true);
}
});
}
});
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment