-1

I am passing some json to a php file but the variables in the php remain empty im sure it is some small syntax error but i am unable to find what is causing this, any help will be greatly appreciated.

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

}
6
  • What is getDateTime()? Commented Jun 28, 2018 at 2:10
  • It returns a timestamp at the time that the php file was called Commented Jun 28, 2018 at 2:12
  • Why not just create the JSON in your PHP file instead and just pass the "id" in your ajax? Commented Jun 28, 2018 at 2:13
  • Javascript POSTs end up needing to be pulled from the raw input to PHP after the headers are dealt with - ie, $json_obj=json_decode(file_get_contents("php://input")) Commented Jun 28, 2018 at 2:22
  • Possible duplicate of Sending JSON to PHP using ajax Commented Jun 28, 2018 at 2:22

2 Answers 2

2
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" );
    });
}

You can simplify your code like this

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help this has helped to solve my issue
-1
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)
    });

Please add full url of your php page then you have to check print_r($_POST) like that

1 Comment

dataType: 'json', add this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.