jQuery $ .get函数的问题 [英] Problem with the jquery $.get function

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

问题描述

我的jQuery $ .get函数有问题. 我正在尝试从PHP文件中获取数据,以检查特定用户是否已订阅并被其他用户记录在忽略列表中.

使用此jquery代码,我只能使用PHP文件从MySql中获得这两个数据之一.

jquery代码是:

$(document).ready(function(){
    $.get("getdata.php", function(returned_data) { 
        if(returned_data === "1") {
            $("div#wall").html('user has no subscription');
            $("#message_wall").attr("disabled", "disabled");
            return false;
        }
    });
});

PHP文件具有MySQL查询,该页面如下所示:

$query = mysql_query("SELECT * FROM subscription WHERE id=$ipr");
$result = mysql_fetch_assoc($query);

if (time() > $result['date']) {
    echo "1";
} else {
    echo "5";
}


$res = mysql_query("SELECT * FROM ignor WHERE migid=$ipr AND igid=$id") or die(mysql_error());
$num_rows = mysql_num_rows($res);
if ($num_rows >= "1") {
    echo "2";
}

对于每个PHP回显响应代码,我需要从$ query的$ .get函数中分离出.attr()和.html()(对于echo"1",echo"2"和echo"5")

如何使用Jquery $ .get做到这一点?

解决方案

就像Jake所说的那样,使用$ .getJSON更好. jQuery内置了对从Web服务器接收JSON作为响应的支持,因此对于这样一个简单的任务,我们在javascript端不需要任何其他JSON库.同样在PHP方面,对于这种简单情况,我们可以使用手动JSON构建JSON响应(发送到javascript).

PHP代码如下所示:

$query = mysql_query("SELECT * FROM subscription WHERE id=$ipr");
$result = mysql_fetch_assoc($query);

if (time() > $result['date']) {
    echo '{ "resultCode":1, "description":"message 1 to be displayed to user" }';
} else {
    echo '{ "resultCode":5, "description":"message 5 to be displayed to user" }';
}

$res = mysql_query("SELECT * FROM ignor WHERE migid=$ipr AND igid=$id") or die(mysql_error());
$num_rows = mysql_num_rows($res);
if ($num_rows >= "1") {
    echo '{ "resultCode":2, "description":"message 2 to be displayed to user" }';
}

然后可以将javascript更新为以下内容:

$(document).ready(function(){
    $.getJSON("getdata.php", function(returned_data) { 

        // returned_data is now a javascript object with 2 properties:
        //   returned_data.resultCode
        //   returned_date.message

        // if the user has a valid subscription
        if (returned_data.statusCode === 5) {
            $("#message_wall").removeAttr("disabled");
            return true;
        }
        // if the user has an invalid subscription, show the message returned from the PHP
        else {
            $("div#wall").html(returned_data.message);
            $("#message_wall").attr("disabled", "disabled");
            return false;                
        }
    });
});

此外,如果您需要能够将多个响应发送回javascript,则可以将JSON响应对象构建到javascript数组中,以便来自PHP代码的响应如下所示:

[ { "resultCode":1, "description":"msg" }, { "resultCode":2, "description":"msg" }, ... ]

然后,您的Javacript将在return_data参数中接收一个数组,可以使用内置的$ .each()函数对其进行迭代.

有关JSON的更多信息,请参见 json.org

I have a problem with the jquery $.get function. I'm trying to get data from PHP file needed to check does the specific user has subscription and is he recorded at ignore list by other users.

With this jquery code I can get only one of this two data from MySql using PHP file.

The jquery code is:

$(document).ready(function(){
    $.get("getdata.php", function(returned_data) { 
        if(returned_data === "1") {
            $("div#wall").html('user has no subscription');
            $("#message_wall").attr("disabled", "disabled");
            return false;
        }
    });
});

The PHP file has MySQL query and that page looks like this:

$query = mysql_query("SELECT * FROM subscription WHERE id=$ipr");
$result = mysql_fetch_assoc($query);

if (time() > $result['date']) {
    echo "1";
} else {
    echo "5";
}


$res = mysql_query("SELECT * FROM ignor WHERE migid=$ipr AND igid=$id") or die(mysql_error());
$num_rows = mysql_num_rows($res);
if ($num_rows >= "1") {
    echo "2";
}

The thing that I need from jquery $.get function is separeted .attr() and .html() for each PHP echo response code (for echo "1", echo "2" and echo "5")

How can I do that with Jquery $.get?

解决方案

Like Jake said, using $.getJSON is better. jQuery has built in support for receiving JSON as a response from the web server, so we don't need any additional JSON libraries on the javascript side for such a simple task. Also on the PHP side, we can build up the JSON response (to be sent to the javascript) using hand-rolled JSON for this simple scenario.

The PHP code would look something like this:

$query = mysql_query("SELECT * FROM subscription WHERE id=$ipr");
$result = mysql_fetch_assoc($query);

if (time() > $result['date']) {
    echo '{ "resultCode":1, "description":"message 1 to be displayed to user" }';
} else {
    echo '{ "resultCode":5, "description":"message 5 to be displayed to user" }';
}

$res = mysql_query("SELECT * FROM ignor WHERE migid=$ipr AND igid=$id") or die(mysql_error());
$num_rows = mysql_num_rows($res);
if ($num_rows >= "1") {
    echo '{ "resultCode":2, "description":"message 2 to be displayed to user" }';
}

The javascript can then be updated to the following:

$(document).ready(function(){
    $.getJSON("getdata.php", function(returned_data) { 

        // returned_data is now a javascript object with 2 properties:
        //   returned_data.resultCode
        //   returned_date.message

        // if the user has a valid subscription
        if (returned_data.statusCode === 5) {
            $("#message_wall").removeAttr("disabled");
            return true;
        }
        // if the user has an invalid subscription, show the message returned from the PHP
        else {
            $("div#wall").html(returned_data.message);
            $("#message_wall").attr("disabled", "disabled");
            return false;                
        }
    });
});

Also if you need to be able to send multiple responses back to the javascript, you can build up the JSON response objects into a javascript array so that the response from the PHP code looks like this:

[ { "resultCode":1, "description":"msg" }, { "resultCode":2, "description":"msg" }, ... ]

your javacript will then receive an array in the returned_data parameter, which can be iterated by using the built-in $.each() function.

For more info on JSON, see json.org

这篇关于jQuery $ .get函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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