[英]php can not get the data send by ajax
這是js代碼,ajax有兩個參數,第一個是url,第二個是包含類型數據和成功的對象。 (我沒有使用jQuery,但是我定義了我自己的函數,代碼位於問題的末尾)我只想將'text'字符串發送到php,所以這樣做有什么問題嗎? 我也嘗試將數據更改為data:{searchinput:“ text”},但仍然無法正常工作。
ajax( 'http://localhost/test.php', { type: 'POST', data: "searchinput=text", onsuccess: function (responseText, xhr) { console.log(responseText); } } );
這是php代碼,很抱歉在粘貼時錯誤地更改了代碼。
$searchinput = $_POST["searchinput"]; @ $db = new mysqli('localhost', 'root', '', 'text'); if (mysqli_connect_errno()) { echo "error:can not connect database"; } $query = "select * from text where data like'".$searchinput."%' "; $result = $db->query($query);
那么錯誤是
未定義索引:searchinput
我已經搜索了一些方法,例如將onsuccess函數更改為setTimeout,然后再次執行ajax,但是它不起作用,只是再次發送數據,但是php仍然無法獲取數據
這是ajax功能
function ajax(url, options) { if (!options.type) { options.type = "post" }; var xhr = new XMLHttpRequest(); xhr.open(options.type, url, true); xhr.send(options.data); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { options.onsuccess(xhr.responseText, xhr) } else { options.onfail(xhr.responseText, xhr); } }; } }
好吧,由於您使用了錯誤的Ajax,所以我並不感到驚訝。 控制台中應該有一個錯誤。
jQuery AJAX的用法如下:
$.ajax({
url: "http://localhost/test.php",
type: 'POST',
data: {searchinput: text},
success: function (responseText, xhr) {
console.log(responseText);
}
}
);
url是ajax期望的對象的一部分,因此它需要在內部而不是外部。 另外,數據需要另一個對象,因此您給了它一個純字符串。
另外,正如@Muhammad Ahmed在回答中指出的那樣,您在php代碼中使用了錯誤的變量。
編輯:沒有jQuery的JavaScript中的AJAX:
var request = new XMLHttpRequest();
request.open('POST', 'http://localhost/test.php', true);
request.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
// worked
var data = JSON.parse(this.responseText);
} else {
// failed
}
}
};
request.send();
request = null;
發送如下所示的數據值,並在php頁面上使用print_r($_POST)
來查看值是否到來
$.ajax(
{ url: 'test.php',
type: 'POST',
data:{
searchinput:text
},
onsuccess: function (responseText, xhr) {
console.log(responseText);
}
}
);
嘗試使用此代碼以錯誤的方式使用ajax。 您可以通過http://api.jquery.com/jquery.ajax/了解有關ajax如何工作以及如何為ajax進行編碼的更多信息。
$.ajax(
{
type: 'POST',
url : 'http://localhost/test.php',
data: {searchinput:text},
success: function (responseText, xhr) {
console.log(responseText);
}
}
);
並且在您的PHP文件中,您需要更新輸入錯誤,即您在$ searchcon變量中獲得了POST的值
$searchcon = $_POST["searchinput"];
^^^^^^^^^^
在查詢中您正在使用
$query = "select * from text where data like'".$searchinput."%' ";
^^^^^^^^^^^^^^
它應該像
$query = "select * from text where data like'".$searchcon."%' ";
^^^^^^^^^^
$searchcon = $_POST["searchinput"];
@ $db = new mysqli('localhost', 'root', '', 'text');
if (mysqli_connect_errno()) {
echo "error:can not connect database";
}
$query = "select * from text where data like'".$searchinput."%' ";
$result = $db->query($query);
在此代碼中,您使用變量$ searchcon的ist行上存在錯誤,而使用$ searchinput的查詢的ist變量名稱更改為$ searchinput而不是$ searchcon。 並更改您的Ajax代碼。
$.ajax({
url: "http://localhost/test.php",
type: 'POST',
data: {searchinput: text},
success: function (responseTxt, xhr) {
console.log(responseTxt);
}
}
);
試試這個代碼:
var other_data = $('form').serializeArray(); $.ajax({ url: 'work.php', data: other_data, type: 'POST', success: function(data){ console.log(data); } });
要么
您還可以通過url傳遞數據。 嘗試適合您要求的代碼。
$.ajax({ url: 'work.php?index=checkbox&action=empty', type: 'POST', success: function(data){ console.log(data); } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.