【问题标题】:PHP POST data array is emptyPHP POST 数据数组为空
【发布时间】:2015-11-09 14:24:34
【问题描述】:

我正在使用 HTML、PHP 和 AJAX 来创建搜索字段。 这是我的 HTML 代码:

<form action="search.php" id="search_form" method="post" >
    <div class="search_bar">
    <input type="text"  name="search_text" id="search_text" placeholder="Search anything" >
    </div>
    <div class="search_button">
    <button type="submit" id="search_button"  name="search_submit" >Search</button>
    </div>
    </form>

这是我的 AJAX 代码:

$('#search_button').click(function(event) {

    var search_data = $('#search_text').val();

    var postData ={
            "content":search_data};

    event.preventDefault();

    $.ajax({
        type: 'POST',
        url: 'search.php',
        data:{myData: postData},
        error: function()
        {
           alert("Request Failed");
        },
        success: function(response)
        {        
            alert("Success");
        }
    });

});

在 PHP 中我尝试了以下方法:

$obj = $_POST['myData'];
echo $obj;
print_r($_POST);

我得到的只是:

注意:未定义索引:第 9 行 C:\xampp\htdocs\workspace\MakeMyApp\WebContent\search.php 中的 myData

Array ( )

我也试过:

file_get_contents('php //input')

但我也得到了空数组。我不知道到底是什么问题。我错过了什么吗?

【问题讨论】:

  • 你看过浏览器控制台中的请求/响应吗?
  • 如果您从等式中删除 AJAX,PHP 是否有效?
  • 永远不要假设设置了$_POST super。在访问它之前检查以确保它存在。
  • 你检查过你的请求头Content-Type (application/x-www-form-urlencoded)吗?
  • @Legionar 将数据更改为字符串与问题无关。传递对象是最常见的做法

标签: php jquery ajax post


【解决方案1】:

抱歉,我无法发表评论,因为我没有足够的“声誉”。

我已尝试复制您的问题,但对我来说似乎没问题。

这是 HTML 页面...

<html>
<head>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){

    $('#search_button').click(function(event) {

    var search_data = $('#search_text').val();

    var postData ={
            "content":search_data};

    event.preventDefault();

    $.ajax({
        type: 'POST',
        url: 'search-submit.php',
        data:{myData: postData},
        error: function()
        {
           alert("Request Failed");
        },
        success: function(response)
        {        
            alert(response);
        }
    });

});

});
</script>

</head>
<body>
<form action="search.php" id="search_form" method="post" >
    <div class="search_bar">
    <input type="text"  name="search_text" id="search_text" placeholder="Search anything" >
    </div>
    <div class="search_button">
    <button type="submit" id="search_button"  name="search_submit" >Search</button>
    </div>
    </form>
</body>
</html>

这是接收 PHP 页面

<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>

在ajax请求中,你可以看到我正在使用alert来输出alert中的响应,但是如果一切顺利它应该输出接收PHP页面输出的内容。

另外,它可能没有多大帮助,但他就是我完成 ajax 请求的方式;它的代码略少,您不必单独定义每个表单字段(如果您有多个字段)

$(document).ready(function(){

    $('#search_form').submit(function() {
        var formData = $(this).serialize();

    $.ajax({
        type: 'POST',
        url: 'search-submit.php',
        data: formData,
        error: function()
        {
           alert("Request Failed");
        },
        success: function(response)
        {        
            alert(response);
        }

    });

    return false;

});

});

【讨论】:

  • 那么我的代码中可能出现的错误是什么?还是其他错误?喜欢文件配置??
  • 你能告诉我你是怎么知道请求没有被正确提交的吗?您是否尝试过像我上面所做的那样提醒响应?我怀疑您的表单工作正常,但您看不到输出。
  • 我为响应添加了警报。令人惊讶的是,我在警报中得到了正确的响应,但不是来自 PHP 文件
  • 好的,我认为您没有理解 ajax 请求的工作原理。您正在做的是将数据提交到“后台”中的 PHP 页面,这意味着您将无法实际看到 PHP 正在检索什么。如果您从页面中完全删除所有 javascript,您应该会看到表单提交到 search.php 页面,并且它将输出它收到的内容。你的代码没什么问题,只是我觉得你没看懂。
  • 此外,您在警报中看到的响应是 PHP 文件的输出。 PHP 文件接收表单提交,并且表单的输出在名为“response”的变量中返回到您的 ajax 方法,您将在警报中输出该变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
  • 2020-12-11
  • 2011-02-28
  • 2015-01-01
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
相关资源
最近更新 更多