我有三個php文件。做這些事的在PHP變量中獲取Ajax腳本響應
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代碼客戶端的值,因爲服務器&客戶端不能交換變量值對方。
預期:響應應該存儲在main.php中的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'];
}
}
所以,我怎麼能得到主Ajax響應值.php @ $ ajaxResponse = ????。
感謝您的幫助提前!
一定有什麼事情。任何選擇? –