无法弄清楚为什么PHP无法通过$ .ajax调用接收POST数据 [英] Can't figure out why PHP not receiving POST data from $.ajax call

查看:122
本文介绍了无法弄清楚为什么PHP无法通过$ .ajax调用接收POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不像我之前没有做过同样的过程,但是我不知道为什么我的PHP脚本的POST数据为空.这是我已经完成/发现的事情:

It's not like I haven't done this same process before, but I can't figure out why my PHP script's POST data is empty. Here's what I've done/found:

  1. 我已验证$ .ajax调用的数据"参数具有值(submitSearch函数中的警告和成功参数中的警告显示搜索变量的正确值).

  1. I've verified that the $.ajax call's "data" parameter has a value (alerts in the submitSearch function and in the success parameter show the correct value of the search variable).

我知道该脚本已被找到"-在浏览器的js控制台中找不到未找到文件的消息.

I know that the script is being "found" - no file not found messages in the js console in the browser.

我没有任何数据库连接错误

I'm not getting any db connection errors

我还知道PHP脚本正在运行,因为$ .ajax调用的success参数中的警报正在PHP脚本的else子句中显示$ message值.

I also know that the PHP script is running because the alert in the $.ajax call's success parameter is displaying the $message value in the PHP script's else clause.

我在PHP脚本中设置的日志记录未显示POST数据

The logging I have set up in the PHP script is displaying nothing for the POST data

如果有人可以看一下并希望指出我所缺少的内容,我将不胜感激.

I'd greatly appreciate it if someone can take a look at this and hopefully point out what I'm missing.

以下是所有相关代码:

Javascript(位于HTML文件中的script标签中)

Javascript (in a script tag within the HTML file)

$('input#btnSubmitSearch').click(function() {
// Clear the text box in case an error was indicated previously
$('input#txtSearch').css({'background-color' : ''});

var search = $('input#txtSearch').val();

if (search == '' || search.length != 5) {
    alert ('Not a valid entry!');
    $('input#txtSearch').css({'background-color' : '#FDC3C3'});
    return false;
}
else {
    submitSearch(search);
    return false;
}
});

function submitSearch(search) {
alert ('Sending ' + search + ' to the database.');

$.ajax({
    type: 'POST',
    url: 'scripts/search.php',
    data: search,
    cache: false,
    success: function(response) {
        alert ('search is: ' + search + ', Response from PHP script: ' + response);
    },
    error: function(xhr) {
        var response = xhr.responseText;
        console.log(response);
        var statusMessage = xhr.status + ' ' + xhr.statusText;
        var message  = 'Query failed, php script returned this status: ';
        var message = message + statusMessage + ' response: ' + response;
        alert(message);
    }
});
}


PHP脚本(scripts/search.php)


PHP script (scripts/search.php)

<?php
require_once ('logging.php');
$log = new Logging();

$dbc = @mysqli_connect([connection stuff])
    OR die ('Could not connect to MySQL server: ' . mysqli_connect_error() );

$log->lwrite('$_POST[\'search\']: ' . $_POST['search']);

if (isset($_POST['search'])) {

    $search = $_POST['search'];

    $log->lwrite('$search: ' . $search);

    $querySearch= "SELECT id, value
                            FROM table
                            WHERE value LIKE '%" . $search . "%'";

    $log->lwrite('$querySearch: ' . $querySearch);

    $resultSearch = @mysqli_query($dbc, $querySearch);

    $numRowsSearch = mysqli_num_rows($resultSearch);

    $log->lwrite('rows returned: ' . $numRowsSearch);

    if ($numRowsSearch > 0) {
        while ($rowSearch = mysqli_fetch_array($resultSearch, MYSQLI_ASSOC)) {
            echo 'Value found: ' . $rowSearch['value'];
        }
    } 
}
else {
    $errorMessage ='$_POST[\'search\'] doesn\'t have a value!'; 
    $log->lwrite($errorMessage);
    echo ($errorMessage);
}

推荐答案

$.ajax({
    type: 'POST',
    url: 'scripts/search.php',
    data: {'search': search}, 
...

我相信您需要如上所述设置$_POST['search'].

I believe you need to set $_POST['search'] as above.

这篇关于无法弄清楚为什么PHP无法通过$ .ajax调用接收POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆