Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending PHP json_encode array to jQuery

Tags:

json

jquery

php

ok, i guess I need help ! I searched with every keyword I could think off, but I still cant figure out, please help. Am more of a php guy, and I've just started with jQuery.

Basically, what I am trying to do is to send a jQuery post from a click function. And based on whatever is returned by my php function, show/hide 2 divs. My php function returns a "json_encode" array with 2 simple values, like such :

//==================PHP code ==================================

$message_for_user = "blah blah";
$calculatedValue = 1230;
$responseVar = array(
                    'message'=>$message_for_user,
                    'calculatedValue'=>$calculatedValue
                    );
echo (json_encode($responseVar));

//==================PHP code End ==================================

My javascript code is supposed to accept the values returned by php :

//==================Javascript code ==================================

$("div.calculator_result").click(function()
{
    $.post('myCalculator.php' ,{qid:itemID},function(response)
    {
        $("div.calculation_value").show(500).html(response['calculatedValue']);
        $("div#message_for_user").show(500).html(response['message']);
    }
}

//==================Javascript code End ==================================

Unfortunately, on the javascript side of my project, the divs are not updated with the values returned by my php functions .... where am I wrong? I hope I was clear in my question, if not, do let me know, and I shall provide any extra info required.

Another thing is that earlier, I was echo'ing only a single value, that is the calculated value (echo $calculatedValue), and everything worked fine, its only after I shifted to echo'in the json encode array that things dont work

like image 684
SIndhu Avatar asked May 19 '26 23:05

SIndhu


1 Answers

var json = $.parseJSON(response); alert(json.message);
like image 116
Kemo Avatar answered May 21 '26 14:05

Kemo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!