[英]Can't Send data from php page to another php page via XMLHttpRequest
我有一個表,每一行都有唯一的 ID,所以我試圖將此 ID 發送到另一個 PHP 頁面以在 SQL 語句中使用此 ID。 我嘗試做一些回顯語句來檢查請求是否成功但回顯語句沒有執行
發送數據的函數
<script>
function promote(id) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
alert("Promoted to QA successfully");
}
};
xhttp.open("GET", "promoteAccount.php?id="+id, true);
xhttp.send();
}
</script>
促進帳戶.php
<?php
$id = $_REQUEST['id'];
require_once 'dbconnection.php';
$sql = "SELECT * FROM staf WHERE StaffID = ".$id;
$result = mysqli_query($conn, $sql);
if ($result) {
if ($row = mysqli_fetch_array($result)) {
if ($row['Role'] == "Receptionist") {
$updateSQL = "UPDATE staff SET Role = 'QA' WHERE StaffID = ".$id;
$updateResult = mysqli_query($conn, $updateSQL);
}
}
}
?>
根據上面關於 GET 與 POST 對於這種性質的操作的評論,我建議您將 PHP 更改為僅接受 POST 請求,並在您的 Javascript 中使用更新且功能更強大的fetch api 。 似乎可以簡化 SQL,以便您使用where
子句中的列role
發出單個查詢來驗證更新是否適用,而不是使用單獨的查詢並遍歷記錄集。
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['id'] ) ){
require_once 'dbconnection.php';
$code='QA';
$role='Receptionist';
# error: missing ` from after staffid!!
$sql='update `staff` set `role`=? where `staffid`=? and `role`=?';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('sss', $code, $_POST['id'], $role );
$stmt->execute();
$result=$stmt->affected_rows;
$stmt->close();
$conn->close();
http_response_code( 200 );
exit( $result==1 ? 'Success' : 'Failed' );
}
http_response_code( 404 );
?>
以及使用Fetch
修改的 Javascript 函數
const promote=( id=false )=>{
if( id ){
let fd=new FormData();
fd.set('id',id);
fetch( 'promoteAccount.php',{ method:'post',body:fd } )
.then(r=>r.text())
.then(text=>{
alert('Promoted to QA successfully');
console.log( text );
})
}
}
我創建的單頁演示來測試這個:
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['id'] ) ){
#require_once 'dbconnection.php';
chdir('../../dbo');
require 'db-conn-details.php';
require 'mysqli-conn.php';
$code='QA';
$role='Receptionist';
$sql='update `staff` set `role`=? where `staffid`=? and `role` = ?';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('sss', $code, $_POST['id'], $role );
$stmt->execute();
$result=$stmt->affected_rows;
$stmt->close();
$conn->close();
http_response_code( 200 );
exit( $result==1 ? 'Success' : 'Failed' );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title></title>
</head>
<body>
<input type='button' data-id=1 value='Promote 1' />
<input type='button' data-id=2 value='Promote 2' />
<input type='button' data-id=3 value='Promote 3' />
<input type='button' data-id=4 value='Promote 4' />
<input type='button' data-id=5 value='Promote 5' />
<input type='button' data-id=6 value='Promote 6' />
<input type='button' data-id=7 value='Promote 7' />
<script>
const promote=( id=false )=>{
if( id ){
let fd=new FormData();
fd.set('id',id);
fetch( 'promoteAccount.php',{ method:'post',body:fd } )
.then(r=>r.text())
.then(text=>{
alert('Promoted to QA successfully');
console.log( text );
})
}
}
document.querySelectorAll('input[type="button"]').forEach( bttn=>bttn.addEventListener('click',(e)=>{
promote( e.target.dataset.id );
}))
</script>
</body>
</html>
使用下表架構和數據:
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| staffid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | 0 | |
| role | varchar(50) | NO | | 0 | |
+---------+------------------+------+-----+---------+----------------+
+---------+-----------+--------------+
| staffid | name | role |
+---------+-----------+--------------+
| 1 | Paula | QA |
| 2 | Daniela | PA |
| 3 | Susan | Cleaner |
| 4 | Isobel | Receptionist |
| 5 | Carrie | Team Leader |
| 6 | Chantelle | IT support |
| 7 | Helen | Receptionist |
+---------+-----------+--------------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.