简体   繁体   中英

Pass data from jQuery Ajax to PHP and saving those values in Database

I want to send JS variables using Ajax, to a PHP page so that I could save those variable in the Database. However, it isn't working as I expected. My JS code:

var a= payment;
var b= count_days;
var c= <?php echo $teacher_id; ?>;

var jsonString1 = JSON.stringify(a);
var jsonString2= JSON.stringify(b);
var jsonString3= JSON.stringify(c);

$.ajax({
    type: "POST",
    url: "student_searching_availibility.php",
    data: {
        data1 : jsonString1, 
        data2:jsonString2,
        data3:jsonString3 
    }, 
    cache: false,
    success: function() {
        alert('success');
    }
});

Now my PHP

$data1 = json_decode($_POST['data1']);
$data2 = json_decode($_POST['data2']);
$data3 = json_decode($_POST['data3']);

include "db.php";

$sql2= "INSERT INTO availibility_status_date(student_id,teacher_id,status) VALUES ('$data1','$data2','$data3')";

if(!mysqli_query($con,$sql2)){
    die("error". mysqli_connect_error());
}

I have searched almost all such queries here but still could not solve the problem.

JSON is javascrip object notation you cannot JSON.stringify a variable, it has to be an object. Try this.

var data = {}; //defines that data is a javascript object
//assign your values to there respective indexes
data['a'] = payment;
data['b'] = count_days;
data['c'] = <?php echo $teahcer_id; ?> 
//since data is now a object you can json.stringify it
var jsonData = JSON.stringify(data);

//make ajax request with json data
$.ajax({
    type: "POST",
    url: "student_searching_availibility.php",
    data: { data:jsonData }, 
    cache: false,

    success: function()
    {
        alert('success');
    }
});

On your PHP, remember that json_decode() expects second parameter the $assoc parameter as a boolean, its default is false which means that if you only specity the json string to be decoded then it will return you data in an object form, for associative array pass true as second parameter.

$data = json_decode($_POST['data']); //$data is now an object
$data->a; //your payment
$data->b; //your count_days

$data = json_decode($_POST['data'],true); //$data is now an associative array
$data[a]; //your payment
$data[c]; //your count_days

when you were inserting into sql $data1, $data2 you were actually trying to store objects into your table, here mysqli_query should have returned false and your code died thus you are not getting any error.

So your sql insert values depending upon your json_decode($_POST['data'], true or false) use either $data->a or $data[a] ie,

"INSERT INTO availibility_status_date(student_id,teacher_id,status) VALUES  ('$data[a]','$data[b]','$data[c]')";

OR

"INSERT INTO availibility_status_date(student_id,teacher_id,status) VALUES ('$data->a','$data->b','$data->c')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM