开发者

Boolean value not being passed with jQuery/ $.get - Why?

开发者 https://www.devze.com 2023-01-10 09:10 出处:网络
I have my front end script which has the following jQuery code: $.get(\"/backend/post.php\", { title: title,

I have my front end script which has the following jQuery code:

$.get("/backend/post.php", { 
            title: title,
            message: post,
            stick: sticky
        }, function (output) {
            alert("Call Back");
            }
        }, "json");

stick is a boolean value, the other two are strings. I've confirmed that they are i开发者_JAVA技巧n fact set. Now on the backend page, using $_GET, title and, message and being passed, but $_GET['stick'] is returning empty, and the PHP function gettype() is telling me it is NULL.

In request to one of the comments:

sticky comes from a form, it's a check box and I just select it with this code:

var sticky = $("input[name=sticky]").attr('checked');

When I use alert() to output the value of sticky, it will return true/false depending on what was selected.

Why is this boolean value not being passed? The JSON site tells me you can have the boolean values true/false in it. But it is not working here for some reason.


I can't be entirely sure because I haven't tested it myself, but I think this is related to how GET requests transfer as strings. Try something like this:

var sticky = $("input[name=sticky]").attr('checked')?"true":"false";

$.get("/backend/post.php", { 
    title: title,
    message: post,
    stick: sticky
}, function (output) {
        alert("Call Back");
    }
}, "json");

It's entirely likely that this won't work, but give it a try.


If in a form an input does not have a value it will not be sent as a parameter. So the absence of of a $_GET['stick'] is equivalent to the query string parameter stick=false. So if you must have a parameter:

var sticky = 'true';

if($("input[name=sticky]").not(':checked')) {
    sticky = 'false';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消