2015-04-23 71 views
0

這是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); 
 
     } 
 
     }; 
 
    } 
 
}

+1

最好使用{searchinput:文字} –

+0

也許我錯了,而是應該你的php查詢不像'$查詢= 「從文本選擇*,其中像」數據「 $ searchcon」。 %'「;' – morne

回答

1

好吧,既然你使用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代碼中使用了一個錯誤的變量。

編輯:AJAX中的JavaScript沒有jQuery的:像下面和使用print_r($_POST) PHP頁面上看到值

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; 
+0

但我定義了一個不使用jQuery的ajax函數 – fiona

+0

@fiona在javaScript中沒有本地的ajax函數。你需要在這種情況下使用'XMLHttpRequest'對象,檢查我沒有jQuery的ajax的更新答案。 –

+0

給那個低估這個的人,照顧解釋一下?或者只是另一個戰術downvote? –

0

發送數據值來還是不來

$.ajax(

    { url: 'test.php', 
     type: 'POST', 
     data:{ 
       searchinput:text 
      }, 
     onsuccess: function (responseText, xhr) { 
      console.log(responseText); 
     } 
    } 
); 
+0

我已經試過這一點,但它仍然不相同erroe – fiona

+0

@fiona工作剛剛更新的代碼,如果不是工作向我們展示你的HTML也 –

+0

THX非常多,但我只是想送「文本'的字符串到PHP,所以錯誤和HTML文件之間有任何關係? – fiona

0

與此代碼你試試以錯誤的方式使用ajax。您可以瞭解更多關於如何Ajax的工作原理,以及如何爲阿賈克斯在http://api.jquery.com/jquery.ajax/

$.ajax( 
    { 
     type: 'POST', 
     url : 'http://localhost/test.php', 
     data: {searchinput:text}, 
     success: function (responseText, xhr) { 
      console.log(responseText); 
     } 
    } 
); 

和你的PHP文件中的代碼,你需要更新你的錯字即你在$ searchcon變量

$searchcon = $_POST["searchinput"]; 
^^^^^^^^^^ 

和您的查詢中你使用

$query = "select * from text where data like'".$searchinput."%' "; 
               ^^^^^^^^^^^^^^ 

讓你POST的價值應該是這樣

$query = "select * from text where data like'".$searchcon."%' "; 
               ^^^^^^^^^^ 
+0

** Downvoters atleast發佈你的理由爲什麼它downvoted。至少另一位用戶讓我們知道這段代碼中出了什麼問題。** –

+1

戰術downvoters。我的答案也一樣。 –

+0

是的,我已經看到,不知道什麼是downvote的原因 –

0
$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); 

在這代碼在ist行上有錯誤,您正在使用變量$ searchcon 並且在查詢中您使用$ searchinput將ist變量名改爲$ searchinput而不是$ searchcon。並改變你的ajax代碼。

$.ajax({ 
     url: "http://localhost/test.php", 
     type: 'POST', 
     data: {searchinput: text}, 
     success: function (responseTxt, xhr) { 
      console.log(responseTxt); 
     } 
    } 
); 
0

試試這個代碼:

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); 
     } 
    });