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?

4
  • 1
    Try print_r( $_POST ). Commented Oct 4, 2011 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 (fiddler2.com/fiddler2) to see what data is flowing back and forth to the server. Commented Oct 4, 2011 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. Commented Oct 4, 2011 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). Commented Oct 4, 2011 at 18:33

2 Answers 2

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'];
Sign up to request clarification or add additional context in comments.

5 Comments

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?
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":<?php $_SESSION['here']; ?>}, function(data){ $(".example-number").html(data.amount); }, "json");
@Majed: The session is on the server already, why would you need to pass it from the client to the server??
Because, notificationNum.php is not accessing it for some reason, so instead, I need to pass it to the file.
Good idea, thanks for the help, you gave me the right answer, thus you will be rewarded for that. :-)
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)));
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.