1

So I half got jQuery's ajax ($.post) to work. But, for some reason, I haven't been successful with finding the right online article to explain to me how PHP retrieves the ajax data that is sent. I've found some stuff on json_decode, but upon me doing that to basically decode it, it wont work (and yes, I am using json for the $.post command).

Here is my javascript code

$.post("notificationNum.php", {"user":"1"},
                function(data){
                        $(".example-number").html(data.amount);
                }, "json");

Here is my PHP code

<?php
session_start();
//link to db info here

$user_id_got = json_decode($_REQUEST['user']);

$checknoti = mysql_query("SELECT * FROM notifications WHERE notification_users = '".$user_id_got."' AND notification_viewed= '0'");

  echo json_encode(array("amount"=>mysql_num_rows($checknoti)));
?>  

Mind you all, I've also tried using the $_POST command instead of the $_REQUEST. Any ideas how to send data to the PHP file so I can use it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Majo0od
  • 2,278
  • 10
  • 33
  • 58
  • 1
    Try `print_r( $_POST )`. – JJJ Oct 04 '11 at 18:25
  • Is `notificationNum.php` getting called in the first place? Can you do something like a `var_dump` to see the contents of `$_REQUEST`? Maybe try using a tool like Fiddler (http://www.fiddler2.com/fiddler2/) to see what data is flowing back and forth to the server. – mellamokb Oct 04 '11 at 18:26
  • notificationNum.php is getting called, I already did something to make sure it is being called, and it works. Just getting the information is the problem and retrieving it in PHP. – Majo0od Oct 04 '11 at 18:28
  • Your code is assuming that the jquery is actually sending over a JSON string. You're not, you're just constructing a regular POST system, so passing that data through json_decode in PHP will return null (check json_last_error() to confirm). – Marc B Oct 04 '11 at 18:33

2 Answers2

4

"json" in your jQuery call is how your php should write its output, not how jQuery sends it. Use normal $_REQUEST in your php:

 $user_id_got = $_REQUEST['user'];
user187291
  • 53,363
  • 19
  • 95
  • 127
  • Actually, wait, this did work, but there was a problem with my code. However, is there a way to get a session in php and pass it through jQuery's ajax call? – Majo0od Oct 04 '11 at 18:33
  • I have a $_SESSION[''] in place in the very same file the $.post is being executed. Can I send the information in the $_SESSION through $.post? Such as.... $.post("notificationNum.php", {"user":}, function(data){ $(".example-number").html(data.amount); }, "json"); – Majo0od Oct 04 '11 at 18:37
  • @Majed: The session is on the server already, why would you need to pass it from the client to the server?? – mellamokb Oct 04 '11 at 18:37
  • Because, notificationNum.php is not accessing it for some reason, so instead, I need to pass it to the file. – Majo0od Oct 04 '11 at 18:38
  • Good idea, thanks for the help, you gave me the right answer, thus you will be rewarded for that. :-) – Majo0od Oct 04 '11 at 18:45
0

try this

notificationNum.php

<?php
//link to db info here

$user_id_got = intval($_POST['user']);

$checknoti = mysql_query("SELECT * FROM notifications WHERE notification_users = '".$user_id_got."' AND notification_viewed= '0'");

echo json_encode(array("amount"=>mysql_num_rows($checknoti)));
?>
Community
  • 1
  • 1
xkeshav
  • 53,360
  • 44
  • 177
  • 245