繁体   English   中英

php无法获取ajax发送的数据

[英]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.

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