Baptiste Baptiste - 3 months ago 9
Ajax Question

Get value of posted json data with Ajax

When I make an ajax call, I get the ID of my input and I add the id to my json data.

I need to get this id to use it id my success function.

IE : In data id = 1, i need in success to get .qte1

If you have any idea... :)

$('#action-button').click(function() {
$.ajax({
type: 'POST',
url: 'get_qty.php',
data: {
'id': $('input[name=qte]').val(),
},
success: function(data) {
$(".qte").html("");
$(".qte").append(data);

}
});
return false;
});

Answer

If I understood you correctly you can just concat the value as such

var data = JSON.parse(data);
var $div = "#qte" + data.id;
$($div).html("test");

I have converted to json object because if you don't have access to the php file this at least converts from string to object. Make sure you are using a #to call a specific div not a class.

This is my PHP:

header('Content-type: application/json');
$arr = array("id"=> $_POST['id'],"qty"=> "1");
echo json_encode($arr);