Assign ajax Response Values to PHP Variables

2019-09-22 08:56发布

I'm calling ajax from AddSomething.php file as follows

<script> 
    var dataValue; 
    $("#schedule").change(function () { 
        var value = $('#schedule option:selected').text(); 
        var ajaxCheck = $.ajax({ 
            url: 'processClg.php', 
            type: 'POST', 
            dataType: 'json', // processClg.php will return string means change to text 
            data: { id: value }, 
            success: function(data){ 
                dataValue = data; 
                console.log(dataValue); 
            } 
        }); 
    }); 
</script>

Getting response in Network as follows

[{
    "id": "2",
    "scheduleName": "abc",
    "subject": "Patho",
    "university": "xyz",
    "facultyName": "Dr",
    "scheduleStartDate": "2015-06-05",
    "scheduleEndDate": "2015-06-09"
}]

And in console it is showing [Object]

How can I assign the above values to the following PHP variables present in the same page

<?php
    $scheduleStartDate ='';//Here How can i assign respected ajax responsevalue
    $scheduleEndDate = '';//Here How can i assign respected ajax response value

    $qry = "SELECT * FROM `mytable`
        WHERE `university` LIKE ''//Here How can i assign value  
        ORDER BY `university` DESC,student_name ASC";
?>

processClg.php file code

<?php
    include "config.php";
    $qry = "SELECT * FROM `schedule` WHERE scheduleName LIKE '".$_POST['id']."'";
    $res = mysqli_query($link, $qry);
    //echo $res;

    while($row = mysqli_fetch_assoc($res))
        $test[] = $row; 

    echo json_encode($test);
?>

3条回答
太酷不给撩
2楼-- · 2019-09-22 09:43

You can store ajax result in php variable by embedding ajax result div into php tag

<?php
 $result='<div id="result"></div>';// Here you embed div in php tag and store in php varible
?>
查看更多
Ridiculous、
3楼-- · 2019-09-22 09:44

You can't assign the result of an Ajax call to PHP variables. The PHP has already run on the server, done it's bit and sent the HTML / JavaScript to the browser. At that point you can no longer use PHP as it's a server side language and will only run on the server. Ajax uses JavaScript, which is a client side language. It only runs after the PHP has finished and the server has sent the HTML / JavaScript to the users browser. JavaScript is executed in the clients browser.

You need to rethink what you're doing and where you're doing it. If you need the results of the Ajax call to do something in PHP then do it in your processClg.php file. You do all your PHP first, then send a response to the browser to display something to the user. If the user doesn't need any sort of confirmation or acknowledgement then you can just send the Ajax request and not bother with a response.

It looks like you're trying to dynamically load some University information. What you should do is put this part:

 $scheduleStartDate ='';//Here How can i assign respected ajax responsevalue
 $scheduleEndDate = '';//Here How can i assign respected ajax response value

$qry = "SELECT * FROM `mytable`
    WHERE `university` LIKE ''//Here How can i assign value  
    ORDER BY `university` DESC,student_name ASC";

into processClg.php after the first query. Then you get the results of the query and pass them back to your HTML page, like this:

$results = array();
$qry = "SELECT * FROM `mytable`
    WHERE `university` LIKE ''  
    ORDER BY `university` DESC,student_name ASC";
$result = mysqli_query($link, $qry);
while ($row = mysqli_fetch_assoc($result))
{
    $results[] = $row;
}
echo json_encode($results);
查看更多
Root(大扎)
4楼-- · 2019-09-22 09:47

You cann't do this job as you think the way.

For that , you need to assign the return values to any hidden field form value and submit the form.

Also keep in mind for ajax response data keep in the js variable, you need to pass the parameter async:false

查看更多
登录 后发表回答