這是js代碼,ajax有兩個參數,第一個是url,第二個是包含類型數據和onsuccess的對象。 (我沒有使用jQuery,但我定義了自己的函數,代碼在問題的末尾) 我只是想將'text'字符串發送到php,那麼是否有任何問題需要這樣做?我也嘗試將數據更改爲數據:{searchinput:「text」},但仍然無效。 php無法獲取由ajax發送的數據
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);
}
};
}
}
最好使用{searchinput:文字} –
也許我錯了,而是應該你的php查詢不像'$查詢= 「從文本選擇*,其中像」數據「 $ searchcon」。 %'「;' – morne