marky
marky

Reputation: 5068

Can't figure out why PHP not receiving POST data from $.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);
}

Upvotes: 0

Views: 13644

Answers (4)

Rajnish
Rajnish

Reputation: 81

Try This before your success

beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")}

Upvotes: 2

dnagirl
dnagirl

Reputation: 20446

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

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

Upvotes: 8

user1995997
user1995997

Reputation: 591

Don't use click function directly. Use $.on function instead.

$('form').on('click', ".submit", function() {
     //code here...
});

For making ajax request, try,

var search = 'something';
$.post("scripts/search.php", {search:search}, function(response) {
     alert ('search is: ' + search + ', Response from PHP script: ' + response);
});

Upvotes: 1

Santiago Rojo
Santiago Rojo

Reputation: 79

may be you should change (in your Javascript) the line:

data: search,

to:

data: {search: search},

You're sending a simple string instead of a named parameter.

Upvotes: 1

Related Questions