在PHP变量中获取Ajax脚本响应 [英] Getting Ajax script response in PHP variable

查看:55
本文介绍了在PHP变量中获取Ajax脚本响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个php文件.

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

做所有这些事情的主要目的是使用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.

响应应存储在main.php中的php变量中.

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:

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:

insert.php:

if ($_POST) {

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

        echo $_POST['id1'];

    }

}

那么如何在$ajaxResponse = ????的main.php中获得Ajax响应值?

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

推荐答案

以下是答案:

使用以下方法在main.php中获取名称:

Get names in main.php using below way:

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

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

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

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

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

session_start();
$issued_to = explode(",", $names);

session_unset();
session_destroy();

这篇关于在PHP变量中获取Ajax脚本响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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