[英]Ajax submit doesn't work (can't read $_POST in php)
我正在嘗試使用Ajax提交表單。 單擊按鈕時,將調用hit()函數, 並將 文本框的內容傳遞回test.php
$_POST
似乎是空的,因為我收到了來自ajax的警報(已提交表單),但是卻看不到echo( echo $_POST['textbox']
)
test.php
<?php
echo "test";
if($_POST)
{
echo $_POST['textbox'];
}
?>
<html>
<body>
<form action="index.php" method="post" align="center" id="form" name="form">
<script type="text/javascript" src="test2.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
<input type="text" class="form-control" id="input" name="input" oninput="check();">
<input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">
</form>
</body>
</div>
</html>
test2.js
function hit() {
var inputText = $("#input").val();
var inputTextString = "textbox=" + inputText;
$.ajax({
type: 'post',
url: 'test.php',
data: inputTextString,
success: function () {
alert('form was submitted');
}
});
}
您將在響應中獲得理想的結果。
function hit() {
var inputText = $("#input").val();
$.ajax({
type: 'post',
url: 'test.php',
data: {textbox : inputText},
success: function (res) {
alert(res);
}
});
}
並且您需要更改test.php文件。
<?php
echo "test";
if($_POST)
{
echo $_POST['textbox'];exit;
}
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="test2.js"> </script>
</head>
<body>
<form action="index.php" method="post" align="center" id="form" name="form">
<input type="text" class="form-control" id="input" name="input" oninput="check();">
<input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">
</form>
</body>
</div>
</html>
這里還有更多問題
在test.php中
$()
而不會出現$ undefined
錯誤,而在包含jquery之后運行該函數..對不起壞 <head></head>
或</body>
之前 <form>
您的代碼應該是這樣的
<?php
echo "test";
if($_POST)
{
echo $_POST['textbox'];
return false;
}
?>
<html>
<head>
</head>
<body>
<input type="text" class="form-control" id="input" name="input" oninput="check();">
<input type="button" class="form-control" id="send" name="send" value="Send" onclick="hit();">
<script type="text/javascript" src="test2.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
</body>
</div>
</html>
在test2.js上
function hit() {
var inputText = $("#input").val();
var inputTextString = {textbox : inputText}; // use this
$.ajax({
type: 'post',
url: 'test.php',
data: inputTextString,
success: function () {
alert('form was submitted');
}
});
}
注意:對我來說,我更喜歡使用分離的php文件與ajax一起使用..這樣可以簡化輸出
如果您需要使用表單..您可以使用包括上面我的注釋的表單代碼,並使提交按鈕的類型為“ submit”,並從中刪除onclick="hit()"
,然后在您的js文件中使用
$(document).ready(function(){
$('form').on('submit',function(e){
e.preventDefault(); // to prevent form reload
var inputText = $("#input").val();
var inputTextString = {textbox : inputText}; // use this
$.ajax({
type: 'post',
url: 'test.php',
data: inputTextString,
success: function () {
alert('form was submitted');
}
});
});
});
您永遠不會測試是否看到echo
的響應-因此根本不會警告php的響應。
要查看您的php腳本返回的內容,您必須對成功回調中傳入的參數進行警報(或記錄或進行一些有用的操作):
....
success: function (response) {
alert(response);
console.log(response);
},
....
無論如何,您應該確保不要將其他數據(例如您的情況下不需要的html)發送回ajax,而僅發送回value / json。 因此,在您的情況下exit;
echo
后會有所幫助。
還請按照@ Mohammed-Yousef的說明處理其他問題!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.