Retrieve object from jquery in php script
I'm trying to rebuild an object encoded with Json but i'm not getting any value.
JQuery:
$.post("views/insert_tasks.php",{ clickedRows : clickrows , <?php echo "tasks:'" . json_encode($tasks) . "'"; ?> }, function(data)
{开发者_开发问答
});
this is the PHPcode to retrieve the object:
$tasks = json_decode(stripslashes($_POST['tasks']), true);
$tasks is empty after execute the code above.
This is what I'm getting with the $_POST['tasks']:
[{"task_id":"1","description":"<p>Fazer heroi</p>","createdat":"Saturday 22nd of May 2010 11:37:37 PM","createdby":"Miguel Cardoso","max_requests":"2","max_duration":"5","job_id":"Concept Artist"},{"task_id":"2","description":"<p>teste2</p>","createdat":"Sunday 23rd of May 2010 11:23:55 AM","createdby":"Miguel Cardoso","max_requests":"2","max_duration":"5","job_id":"3D Modeller"},{"task_id":"3","description":"<p>teste3</p>","createdat":"Sunday 23rd of May 2010 11:45:39 AM","createdby":"Miguel Cardoso","max_requests":"1","max_duration":"10","job_id":"Writer"}]
What I'm doing wrong?
Thanks for your reply Brian. I get it work replacing:
$tasks = json_decode(stripslashes($_POST['tasks']), true);
by
$tasks = json_decode($_POST['tasks']);
I was seeing the content of the variable $tasks writing it to a file. Here is the all code of php file:
<?php
session_start();
require_once("../database/db_connect.php");
if(isset($_POST['clickedRows']))
{
$clickedRows = json_decode($_POST['clickedRows']);
$tasks = json_decode($_POST['tasks']);
foreach($clickedRows as $i)
{
$task_id = $tasks[$i]->task_id;
$myFile = "debug.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $_POST['tasks']);
fclose($fh);
$user_id = $_SESSION['id'];
$requestedat = date('l jS \of F Y h:i:s A');
$requestedby = $_SESSION['first'] . " " . $_SESSION['last'];
$sql ="INSERT INTO
darkfuture.users_tasks
(
`task_id`,
`user_id`,
`requestedat`,
`requestedby`
)
VALUE (
$task_id,
$user_id,
'$requestedat',
'$requestedby'
)";
$res = mysql_query($sql) or die(mysql_error());
}
}
?>
精彩评论