0

How to return multiple data using jquery, json, ajax and php.

$.ajax({
    url:'ajax.php',
    type:'post',
    data:{function:'postAnswer', questionID:$('#questionID').val(), answer:$('#answer').val()},
    success:function(data)
    {
        alert(data);
        location.reload();
    }
    });
Avijit Palit
  • 21
  • 1
  • 3
  • 1
    if you are doing `location.reload();` why are you doing ajax???????????????????????????????????????????????????????????????????????????? – madalinivascu Jul 27 '16 at 05:27

2 Answers2

0

Use double quotes for json value or it would be variable change like this

  data:{function:"postAnswer", questionID:$('#questionID').val(), answer:$('#answer').val()},
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

first of all, You need to json encode() all the data which was returned in ajax.php You need to encode it, at that page before return.

echo json_encode(data variable name in ajax page);

then you need to use this code at the success phase

$.ajax({
    url:'ajax.php',
    type:'post',
    data:"passing_dataname"+passing_data,
    success:functiondata, textStatus, jqXHR)
    {
        var answer=jQuery.parseJSON(data);
        alert(answer);

    }
    });
dulaj sanjaya
  • 1,290
  • 13
  • 25