我正在處理一個檔案,form.php該檔案需要使用 AJAX 請求處理用戶輸入,因此頁面不會重新加載。用戶有 2 個選項 - 檢查或重繪 資料。下面是代碼。問題是,$_POST沒有被填充。當檢查 Firefox 開發工具中的網路選項卡時,它顯示請求正在發生。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<form action="" method="post">
<p>Select action for data -</p>
<input type="radio" name="action" id="check" value="check">
<label for="check">check</label>
<br>
<input type="radio" name="action" id="refresh" value="refresh">
<label for="refresh">refresh</label>
<br>
</form>
<script>
$(document).ready(function() {
$("input[type=radio][name='action']").change(function() {
var action = $("input[name='action']:checked").val();
executeAjax(action);
});
});
function executeAjax(action) {
return $.ajax({
method: "POST",
data: { action: action }
})
.done(function() {
console.log('success - ' action ' is the action');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log('ERROR');
console.log(jqXHR.status);
console.log(textStatus);
console.log(errorThrown);
});
}
</script>
</body>
</html>
<?php
echo '<hr>';
echo 'post - ' . json_encode($_POST) . '<br>';
$action = isset($_POST['action']) ? $_POST['action'] : 'no value';
echo $action;
// put logic here for what to do with with data based on value in $action'
?>
請求在網路選項卡中顯示成功:
我正在處理一個檔案,form.php該檔案需要使用 AJAX 請求處理用戶輸入,因此頁面不會重新加載。用戶有 2 個選項 - 檢查或重繪 資料。下面是代碼。問題是,$_POST沒有被填充。當檢查 Firefox 開發工具中的網路選項卡時,它顯示請求正在發生。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<form action="" method="post">
<p>Select action for data -</p>
<input type="radio" name="action" id="check" value="check">
<label for="check">check</label>
<br>
<input type="radio" name="action" id="refresh" value="refresh">
<label for="refresh">refresh</label>
<br>
</form>
<script>
$(document).ready(function() {
$("input[type=radio][name='action']").change(function() {
var action = $("input[name='action']:checked").val();
executeAjax(action);
});
});
function executeAjax(action) {
return $.ajax({
method: "POST",
data: { action: action }
})
.done(function() {
console.log('success - ' action ' is the action');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log('ERROR');
console.log(jqXHR.status);
console.log(textStatus);
console.log(errorThrown);
});
}
</script>
</body>
</html>
<?php
echo '<hr>';
echo 'post - ' . json_encode($_POST) . '<br>';
$action = isset($_POST['action']) ? $_POST['action'] : 'no value';
echo $action;
// put logic here for what to do with with data based on value in $action'
?>
請求在網路選項卡中顯示成功:

控制臺顯示來自.done部分 AJAX 呼叫的成功訊息:

那么為什么不$_POST包含'action'用戶選擇的任何值的key呢?
uj5u.com熱心網友回復:
在您的代碼中,您沒有使用 ajax 方法的回應。
done(function(reposne) {
console.log('success - ' action ' is the action');
console.log(response);
})
您也使用相同的 form.php 進行 ajax 方法。因此,您的 ajax 回應將列印表單 html,然后是您的 php $_POST 相關代碼。
您能否將您的 php 代碼放在頁面頂部,如果表單已發布,則將退出。否則列印表格
<?php
if(!empty($_POST)){
// enter code here
exit;
}
?>
<html>
...
</html>
POST">
控制臺顯示來自.done部分 AJAX 呼叫的成功訊息:
我正在處理一個檔案,form.php該檔案需要使用 AJAX 請求處理用戶輸入,因此頁面不會重新加載。用戶有 2 個選項 - 檢查或重繪 資料。下面是代碼。問題是,$_POST沒有被填充。當檢查 Firefox 開發工具中的網路選項卡時,它顯示請求正在發生。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<form action="" method="post">
<p>Select action for data -</p>
<input type="radio" name="action" id="check" value="check">
<label for="check">check</label>
<br>
<input type="radio" name="action" id="refresh" value="refresh">
<label for="refresh">refresh</label>
<br>
</form>
<script>
$(document).ready(function() {
$("input[type=radio][name='action']").change(function() {
var action = $("input[name='action']:checked").val();
executeAjax(action);
});
});
function executeAjax(action) {
return $.ajax({
method: "POST",
data: { action: action }
})
.done(function() {
console.log('success - ' action ' is the action');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log('ERROR');
console.log(jqXHR.status);
console.log(textStatus);
console.log(errorThrown);
});
}
</script>
</body>
</html>
<?php
echo '<hr>';
echo 'post - ' . json_encode($_POST) . '<br>';
$action = isset($_POST['action']) ? $_POST['action'] : 'no value';
echo $action;
// put logic here for what to do with with data based on value in $action'
?>
請求在網路選項卡中顯示成功:

控制臺顯示來自.done部分 AJAX 呼叫的成功訊息:

那么為什么不$_POST包含'action'用戶選擇的任何值的key呢?
uj5u.com熱心網友回復:
在您的代碼中,您沒有使用 ajax 方法的回應。
done(function(reposne) {
console.log('success - ' action ' is the action');
console.log(response);
})
您也使用相同的 form.php 進行 ajax 方法。因此,您的 ajax 回應將列印表單 html,然后是您的 php $_POST 相關代碼。
您能否將您的 php 代碼放在頁面頂部,如果表單已發布,則將退出。否則列印表格
<?php
if(!empty($_POST)){
// enter code here
exit;
}
?>
<html>
...
</html>
POST">
那么為什么不$_POST包含'action'用戶選擇的任何值的key呢?
uj5u.com熱心網友回復:
在您的代碼中,您沒有使用 ajax 方法的回應。
done(function(reposne) {
console.log('success - ' action ' is the action');
console.log(response);
})
您也使用相同的 form.php 進行 ajax 方法。因此,您的 ajax 回應將列印表單 html,然后是您的 php $_POST 相關代碼。
您能否將您的 php 代碼放在頁面頂部,如果表單已發布,則將退出。否則列印表格
<?php
if(!empty($_POST)){
// enter code here
exit;
}
?>
<html>
...
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/442707.html
