duangong0690 2015-09-02 08:42
浏览 35
已采纳

在PHP变量中获取Ajax脚本响应

I have three php files.

main.php - to use stored Ajax response.
filter.php - to send Ajax request and get response
insert.php - to store Ajax response for using in main.php

Primary purpose of doing all these thing is to use client side values using PHP code because server and client can't exchange variable values each other.

The response should be stored in php variable in main.php.

main.php:

?>

<script>
$.ajax({
 type: "POST",
 url: "filter.php",
 data: { id1: name, id2:"employees"},


  success:function(response) {
    var res = response;
    $.ajax({
        type: "POST",
        url: "insert.php",
        data: { id1: res },

    success:function(data){
      alert(data);
      }
   });
});
<script>

<?php

$ajaxResponse = ???? <need to get value of data over here>

filter.php:

// Return employee names

    if ($_POST['id1'] == "name" && $_POST['id2'] == "employees") {

        $conn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT " .$_POST['id1']. " FROM 1_employees";
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0) {
            while($row =  mysqli_fetch_array($result)) {
                $rows[] = $row['name'];
            }
        }

        echo json_encode($rows);

        mysqli_close($conn);

        exit (0);

    }

insert.php:

if ($_POST) {

    if ($_POST['id1'] !== "") {

        echo $_POST['id1'];

    }

}

So how can I get ajax response value in main.php at $ajaxResponse = ?????

  • 写回答

3条回答 默认 最新

  • douxiong1994 2015-09-02 11:13
    关注

    Here is the answer:

    Get names in main.php using below way:

    -------- filter.php ----------

    session_start(); //at the top
    $_SESSION['names'] = json_encode($rows);
    

    -------- main.php ----------

    session_start();
    $issued_to = explode(",", $names);
    
    session_unset();
    session_destroy();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!