Reputation: 5068
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:
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).
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
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.
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
Reputation: 81
Try This before your success
beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")}
Upvotes: 2
Reputation: 20446
$.ajax({
type: 'POST',
url: 'scripts/search.php',
data: {'search': search},
...
I believe you need to set $_POST['search']
as above.
Upvotes: 8
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
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