<?php
require_once './db_connect.php';
$db = new DB_Connect();
$db->connect();
$data = json_decode($_POST['myData']);
$array=json_decode($_REQUEST['question']);
if(isset($_POST['myData'])){
$obj = json_decode($_POST['myData']);
//some php operation
$q = "insert into questions(question)
values ('". $obj."')";
$result = mysql_query($q) or die(mysql_error());
}
?>
我想检索从另一个php页面发送到此页面的JSON数据,但我不能,为什么呢?
这是另一页
function validateForm()
{
var q = document.forms["form1"]["question"].value;
var T = document.forms["form1"]["title"].value;
if (T == null || T == "")
{
alert("please type you form title first");
return false;
}
if (q == null || q == "")
{
document.getElementById("question").style.color="black";
alert("please enter your question");
return false;
}
question.push(q);
//alert(JSON.stringify(question));
var xhr = new XMLHttpRequest();
xhr.open('post', 'create_form.php',true);
// Track the state changes of the request
xhr.onreadystatechange = function(){
// Ready state 4 means the request is done
if(xhr.readyState === 4){
// 200 is a successful return
if(xhr.status === 200){
alert(xhr.responseText); // 'This is the returned text.'
}else{
alert('Error: '+xhr.status); // An error occurred during the request
}
}
}
// Send the request to send-ajax-data.php
xhr.send({myData:JSON.stringify(question)}); //+encodeURI(JSON.stringify(question))
// addField();
return true;
}
有人可以帮帮我吗?
我试图用jquery ajax解决这个问题,它只是一样,这就是为什么我试图只使用javascript来解决这个问题
答案 0 :(得分:0)
试试这个
$json = file_get_contents('php://input');
$obj = json_decode($json, TRUE);
而不是
$data = json_decode($_POST['myData']);
$array=json_decode($_REQUEST['question']);
if(isset($_POST['myData'])){
$obj = json_decode($_POST['myData']);