简体   繁体   中英

Issues with json_encode array

I am having issues with my JSON_encode and jQuery, I am getting it to post the full array with the following code, but I can't get it to echo just the one array I need.

$.get("/indexget.php", { page: "goals" },function(data) {
$('#bodyContentStuff').text(data);

Full jQuery code

$("#navLinkGoals").click(function () {
    $("#bodyTitleBar").text("Goals");
    $.get("/indexget.php", { page: "goals" },function(data) {
        $('#bodyContentStuff').text(data);
    }
);

Full PHP code

if($_REQUEST['page'] = 'goals'){
    $result = mysql_query("SELECT * FROM content WHERE page='indexGoals'") 
    or die(mysql_error()); 
    $row = mysql_fetch_array( $result );
    $message = $row['message'];
}
echo json_encode(array("content"=>"$message"));

I am sort of a noob with programming, but the site is http://www.gronge.com if you'd like to see what it is doing, just click the goals link.

Edit: Oh, I have also tried

$('#bodyContentStuff').text(data['content']);

But it didn't work.

have you tried putting ", 'json'" like so:

$.get("/indexget.php", { page: "goals" },function(data) {
    $('#bodyContentStuff').text(data.content);
}, 'json')

without that jquery makes an intelligent guess as to what the format is...

and as mentioned it should be data.content

and as mentioned use console.log(data) to see what data really is (maybe a string while it should be a json object?)

If that was a verbatim paste there is a missing closing parenthesis on the call to $.get

$("#navLinkGoals").click(function () {
    $("#bodyTitleBar").text("Goals");
    $.get(
        "/indexget.php",
        {
            page: "goals"
        },
        function(data) {
            $('#bodyContentStuff').text(data);
        }
    ); // <== Right here
);

(Code reformatted to highlight difference)

Just noticed a potential issue with the PHP:

if($_REQUEST['page'] = 'goals'){ //<== are you assigning or comparing?
    $result = mysql_query("SELECT * FROM content WHERE page='indexGoals'") 
    or die(mysql_error()); 
    $row = mysql_fetch_array( $result );
    $message = $row['message'];
}
echo json_encode(array("content"=>"$message"));

instead of get use $.post

 $("#navLinkGoals").click(function () {
   $("#bodyTitleBar").text("Goals");
    $.post("/indexget.php", { page: "goals" },function(data) {
    $('#bodyContentStuff').text(data);
}
);

Have you logged the data variable? What does it log? Also: mysql_* is deprecated, steer clear and start using PDO. But I think you meant to assign $row[0]['message'] to the $message variable. If memory serves me well, mysql_* always returns either an array of assoc arrays, or an array of arrays.

While we're on the subject: JavaScript doesn't have assoc arrays, only objects, so rather then accessing the data like data['content'] , use data.content . The bracket notation is there for convenience, so you can use the value of a variable as property name (without eval), which is EVIL , of course.

Also on your php:

$row = mysql_fetch_assoc($result);

And not mysql_fetch_array, if you want to use the $row['message'] notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM