将ajax响应值分配给PHP变量 [英] Assign ajax Response Values to PHP Variables

查看:111
本文介绍了将ajax响应值分配给PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从AddSomething.php文件调用ajax,如下所示

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"
}]

在控制台中,它显示[Object]

如何将上述值分配给同一页面中存在的以下PHP变量

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文件代码

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

推荐答案

您不能将Ajax调用的结果分配给PHP变量. PHP已经在服务器上运行,完成了一点工作,然后将HTML/JavaScript发送到浏览器.到那时,您将无法再使用PHP,因为它是服务器端语言,并且将在服务器上运行. Ajax使用JavaScript,这是一种客户端语言.它仅在PHP完成并且服务器已将HTML/JavaScript发送到用户浏览器后,才能运行. JavaScript在客户端浏览器中执行.

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.

您需要重新考虑您在做什么和在哪里做.如果您需要Ajax调用的结果来在PHP中执行某些操作,请在您的 processClg.php 文件中进行操作.您首先要完成所有PHP,然后将响应发送到浏览器以向用户显示内容.如果用户不需要任何确认或确认,那么您只需发送Ajax请求,而不必理会响应.

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

在第一个查询后进入processClg.php.然后,您将获得查询结果,并将其传递回HTML页面,如下所示:

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

这篇关于将ajax响应值分配给PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆