【问题标题】:php can not get the data send by ajaxphp无法获取ajax发送的数据
【发布时间】:2015-04-23 15:06:57
【问题描述】:

这是js代码,ajax有两个参数,第一个是url,第二个是一个包含类型数据和onsuccess的对象。 (我没有使用jQuery但是我自己定义的函数,代码在问题的最后) 我只想将“文本”字符串发送到 php,那么这样做有什么问题吗?我也尝试将数据更改为数据:{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);

那么错误是

未定义索引:搜索输入

我已经搜索了一些方法,例如将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: text}
  • 也许我错了,但你的 php 查询应该不像 $query = "select * from text where data like'".$searchcon."%' ";

标签: javascript php ajax


【解决方案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 代码中使用了错误的变量。

编辑:没有 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;

【讨论】:

  • 但是我定义了一个ajax函数而不使用jQuery
  • @fiona javaScript 中没有本机 ajax 函数。在这种情况下,您需要使用 XMLHttpRequest 对象,请检查我更新的答案以获取没有 jquery 的 ajax。
  • 致对此投反对票的人,想解释一下吗?还是只是另一个战术性的否决票?
  • @Y U NO WORK ,我刚才没有粘贴ajax函数,但我的js文件中有它。在浏览器控制台中,post信息是对的
  • @Y U NO WORK,非常感谢,我厌倦了 jQuery 并且它可以工作,所以我的 ajax 函数一定有问题。我认为问题在于发送(options.data),发送()中应该发送什么?请..
【解决方案2】:
$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);
        }
    }
);

【讨论】:

    【解决方案3】:

    发送如下数据值并在 php 页面上使用 print_r($_POST) 来查看值是否到来

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

    【讨论】:

    • @fiona 刚刚更新了代码,如果不起作用,也可以向我们展示您的 html
    • 非常感谢,但我只是想将'text'字符串发送到php,那么错误和html文件之间有什么关系吗?
    • @fiona 我以为你从某个地方得到了text 作为价值!!控制台有什么错误???
    • 控制台没有报错,post信息是对的,如果'text'有问题,我觉得php还是能得到['searchinput']的,我搜了这个问题很久了,也许我应该试试jQuery..sigh
    【解决方案4】:

    尝试使用您以错误方式使用 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."%' ";
                                                   ^^^^^^^^^^
    

    【讨论】:

    • Downvoters 至少发布您为什么它被否决的原因。至少有另一个用户知道这段代码出了什么问题。
    【解决方案5】:

    试试这个代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 2021-08-18
      • 2020-09-24
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多