Thursday, 30 August 2018

Unable to understand why PHP does not receive POST data from the $ .ajax call

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. 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).
  2. I know that the script is being "found" - no file not found messages in the js console in the browser.
  3. I'm not getting any db connection errors
  4. 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.
  5. 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.
Here is all of the relevant code:

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 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},
...

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

0 comments:

Post a Comment