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 logged the
datavariable? 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$messagevariable. 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'], usedata.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.