Reputation: 2509
This is my jquery
code:
var jsObj = {"user_id":5, "login":"hsm"};
var str_json = JSON.stringify(jsObj);
$.post("json_handler.php", str_json)
.done(function(){
alert('data sent successfully');
window.location = "http://localhost/quranMapping/php/json_handler.php";
})
.fail(function(){
alert('fail');
});
NOTE: I made the redirection to check out where is the problem.
And this is my php code (json_handler.php
file):
<?php
//create a DB connection
$con=mysqli_connect("127.0.0.1","root","","my_db");
$input = file_get_contents('php://input');
$result = json_decode($input);
echo $result->user_id;
mysql_close($con);
?>
As error have a look to the attached picture,
How can i correct such problem?
UPDATE: when I used $input = $_POST['str_json']
the error was undefined index
Upvotes: 0
Views: 311
Reputation: 4463
Why dont you try using
JS:
var jsObj = {"user_id":5, "login":"hsm"};
var str_json = JSON.stringify(jsObj);
$.post("json_handler.php", {"str_json" :str_json})
.done(function(){
alert('data sent successfully');
window.location = "http://localhost/quranMapping/php/json_handler.php";
})
.fail(function(){
alert('fail');
});
PHP:
<?php
//create a DB connection
$con=mysqli_connect("127.0.0.1","root","","my_db");
$input= $_POST['str_json']
$result = json_decode($input);
echo $result->user_id;
mysql_close($con);
?>
Upvotes: 0
Reputation: 360762
The json_decode probably failed, returning a NULL
value. Do var_dump($result)
and var_dump($input)
to see what you're getting from the client and from json_decode.
Upvotes: 1