【问题标题】:javascript json to php variablejavascript json 到 php 变量
【发布时间】:2018-12-06 23:46:48
【问题描述】:

我将一些 json 传递给一个 php 文件,但 php 中的变量仍然为空,我确定这是一些小的语法错误,但我无法找到导致此问题的原因,任何帮助将不胜感激。

JAVASCRIPT

if(score >= 100)
{
    console.log("HERE WE GO");
    $.ajax({
        type: "POST",
        url: "FAKENAME.php",
        data: { "data": "{ \"id\": " + id + ", \"quiz\": \"" + getDateTime() + "\"}" },

    }).done(function (data) {
        console.log(JSON.stringify(data) + "This is the data on .done");
        //alert( data );
    })
    .fail(function () {
        console.log("error");
        //alert( "error" );
    })
    .always(function (data) {
        console.log("finished");
        console.log(JSON.stringify(data));
        //alert( "finished" );
    });
}

PHP

$data = json_decode($_POST['data']);
$sql = $conn->prepare("SELECT * FROM FAKEDATABASETABLENAME WHERE id = :id");//no error
$sql->bindParam(':id', $data->id);
//$sql->bindParam(':quiz', $data->quiz);
$sql->execute(); //syntax error

if(!empty($data->id))
{
    $qry = $conn->prepare("UPDATE FAKEDATABASETABLENAME SET Quiz = '2018-06-27 14:44:49' WHERE id = 000007"); //no error and result
    $qry->bindParam(':id', $data->id);
    $qry->bindParam(':quiz', $data->quiz);
    $qry->execute();
}
else
{
    $mailto = "FAKEEMAIL.com" ; //Recipent of the email
    $from = "From: PHP_DEBUG";
    $subject = "PHP_DEBUG";
    $data = json_decode($_POST['data']);
    $content = "id is: " . $data->id. " plaese note.   quiz is: " . $data->quiz. " please note.";
    mail($mailto, $subject, $content, $from);

}

【问题讨论】:

  • 什么是getDateTime()
  • 返回调用php文件时的时间戳
  • 为什么不在你的 PHP 文件中创建 JSON 而只是在你的 ajax 中传递“id”?
  • Javascript POST 最终需要在处理标头后从原始输入中提取到 PHP - 即 $json_obj=json_decode(file_get_contents("php://input"))

标签: php jquery ajax


【解决方案1】:
if(score >= 100)
{
    var params = JSON.stringify({id: id, quiz: getDateTime()})
    $.ajax({
       type: "POST",
       url: "FAKENAME.php",
       dataType: "json",
       data: {data: params}
    }).done(function (data) {
       console.log(JSON.stringify(data) + "This is the data on .done");
    }).fail(function () {
       console.log("error");
       //alert( "error" );
    }).always(function (data) {
       console.log("finished");
       console.log(JSON.stringify(data));
       //alert( "finished" );
    });
}

你可以像这样简化你的代码

【讨论】:

  • 感谢您的帮助,这有助于解决我的问题
【解决方案2】:
var person = {
        name: $("#id-name").val(),
        address:$("#id-address").val(),
        phone:$("#id-phone").val()
    }

    $('#target').html('sending..');

    $.ajax({
        url: '/test/PersonSubmit',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            $('#target').html(data.msg);
        },
        data: JSON.stringify(person)
    });

请添加您的 php 页面的完整 url,然后您必须像这样检查 print_r($_POST)

【讨论】:

  • dataType: 'json', 添加这个
猜你喜欢
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
相关资源
最近更新 更多