简体   繁体   English

php无法获取ajax发送的数据

[英]php can not get the data send by ajax

this is the js code, ajax has two arguments, the first is url, 2nd is a object which contains type data and onsuccess. 这是js代码,ajax有两个参数,第一个是url,第二个是包含类型数据和成功的对象。 (I didn't use jQuery but the function I define myself, the code is at the end of the question) I just want to send the 'text' string to php, so is there any problem to do like this? (我没有使用jQuery,但是我定义了我自己的函数,代码位于问题的末尾)我只想将'text'字符串发送到php,所以这样做有什么问题吗? I also have tried change the data to data: {searchinput:"text"}, but still don't work. 我也尝试将数据更改为data:{searchinput:“ text”},但仍然无法正常工作。

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

this is the php code, sorry for changing the code wrong while pasting it on. 这是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); 

then the error is 那么错误是

Undefined index: searchinput 未定义索引:searchinput

I have search some method like change onsuccess function to setTimeout, and do ajax again, but it doesn't work, just send the data again but the php still can't get the data 我已经搜索了一些方法,例如将onsuccess函数更改为setTimeout,然后再次执行ajax,但是它不起作用,只是再次发送数据,但是php仍然无法获取数据

this is the ajax function 这是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); } }; } } 

Well, since you used the ajax wrong, I'm not surprised. 好吧,由于您使用了错误的Ajax,所以我并不感到惊讶。 There should be a error in the console. 控制台中应该有一个错误。

jQuery AJAX is used like this: jQuery AJAX的用法如下:

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

url is a part of the object the ajax expects, so it needs to be inside and not outside of it. url是ajax期望的对象的一部分,因此它需要在内部而不是外部。 Also, data is expecting another object, you gave it a plain string. 另外,数据需要另一个对象,因此您给了它一个纯字符串。

Also, as @Muhammad Ahmed stated in his answer, you are using a wrong variable in your php code. 另外,正如@Muhammad Ahmed在回答中指出的那样,您在php代码中使用了错误的变量。

Edit: AJAX in JavaScript without jQuery: 编辑:没有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;

send data value like below and use print_r($_POST) on php page to see values are coming or not 发送如下所示的数据值,并在php页面上使用print_r($_POST)来查看值是否到来

$.ajax(

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

Try with this code you were using ajax in wrong manner. 尝试使用此代码以错误的方式使用ajax。 You can learn more about how ajax works and how to code for ajax over http://api.jquery.com/jquery.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);
        }
    }
);

and within your PHP file you need to update your typo ie you were getting value of your POST in $searchcon variable 并且在您的PHP文件中,您需要更新输入错误,即您在$ searchcon变量中获得了POST的

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

and within your query you were using 在查询中您正在使用

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

it should be like 它应该像

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

In This code there is a mistake on ist line you are using variable $searchcon and on query you are using $searchinput change ist varaible name to $searchinput instead of $searchcon. 在此代码中,您使用变量$ searchcon的ist行上存在错误,而使用$ searchinput的查询的ist变量名称更改为$ searchinput而不是$ searchcon。 and also change your ajax code. 并更改您的Ajax代码。

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

Try this code : 试试这个代码:

 var other_data = $('form').serializeArray(); $.ajax({ url: 'work.php', data: other_data, type: 'POST', success: function(data){ console.log(data); } }); 

or 要么

you can also pass the data in url also. 您还可以通过url传递数据。 Try the code which suits your requirement. 尝试适合您要求的代码。

 $.ajax({ url: 'work.php?index=checkbox&action=empty', type: 'POST', success: function(data){ console.log(data); } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM