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

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教