UXerUIer
UXerUIer

Reputation: 2338

$jquery.post() PHP and Mysql

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?

Upvotes: 1

Views: 451

Answers (2)

xkeshav
xkeshav

Reputation: 54050

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)));
?>

Upvotes: 0

user187291
user187291

Reputation: 53940

"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'];

Upvotes: 4

Related Questions