0

Based on this answer trying to modify jQuery to send data to PHP and get back in JSON format.

Created this

$.post(
    "__02.php", 
    {
        'date_day': date_day,
        'currency': currency
    }, 
    function (data, textStatus) {
        $('#currency_load').html(data);
        $('#is_row_changed_currency' + suffix).val(0)
    }, 
    "json"
);

PHP is like this

 $arr = array ('item1'=>"I love jquery4u",'item2'=>"You love jQuery4u",'item3'=>"We love jQuery4u");
echo json_encode($arr);

But does not work. What need to correct?

For comparison without JSON this works:

$.post("__02.php", { 'date_day': date_day, 'currency': currency }, function(data) {
    $('#currency_load').html(data);
    $('#is_row_changed_currency' + suffix).val(0)
});

json is necessary to get following aim

jquery code like

$('#div1').html("<p>item1="+data.item1+"</p>");
$('#div2').html("<p>item2="+data.item2+"</p>");

html like

<div id="div1"></div>
<div id="div2"></div>

Aim is in certain id to display corresponding value/element from php array. Without json does not know how to do it. html additionally to div need to use also input. So in input1 php array value[0] and so on

Seems function (data, textStatus) must modify to function (data, success)

Community
  • 1
  • 1
user2465936
  • 1,030
  • 4
  • 17
  • 32

1 Answers1

1

You are passing an object (the entire JSON response) to .html() which won't work. You need to pick something from the object to display, for example:

$( '#currency_load' ).html( data.item1 );

If you just want to confirm that the data has been received correctly use the console instead: console.log( data )

JJJ
  • 32,902
  • 20
  • 89
  • 102