发送PHP json_encode数组到jQuery [英] Sending PHP json_encode array to jQuery

查看:100
本文介绍了发送PHP json_encode数组到jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我想我需要帮助!我搜索了所有可以考虑的关键字,但仍然无法找出答案,请帮忙.更多的是一个php家伙,而我刚开始使用jQuery.

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.

基本上,我想做的是从click函数发送一个jQuery帖子.根据我的php函数返回的值,显示/隐藏2个div.我的php函数返回一个带有2个简单值的"json_encode"数组,例如:

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代码========================== =========

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

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

//================== PHP代码结束========================= ==========

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

我的JavaScript代码应该接受php返回的值:

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

//================== Javascript代码========================== =========

//==================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代码结束========================= ==========

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

不幸的是,在我的项目的javascript端,div并没有更新为我的php函数返回的值....我在哪里错了?我希望我的问题很清楚,如果没有,请告诉我,我将提供所需的任何其他信息.

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.

另一件事是,在更早的时候,我只回显一个值,即计算值(echo $ calculatedValue),并且一切正常,只有当我转移到json编码数组中的echo后,不工作

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

推荐答案

var json = $.parseJSON(response); alert(json.message);

这篇关于发送PHP json_encode数组到jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆