I'm being a bit of an idiot and seem to be unable to find an answer to my question, I am wanting to send a search request to my PHP file and return results, when I do that I can't seem to do the right thing, I'm definitely using the correct file but I'm not sure how to return results from the PHP file (I'm fairly new to ajax this so I do apologies for sound silly)
Jquery/Ajax:
$('#searchGo').click(function() {
var tempVar = 0;
var request = $.ajax({
async: false,
url: "/../commerce/search.php",
type: "POST",
data: "search=" + $('#search').val(),
dataType: "html"
});
alert(request.responseText + ' test');
});
Search.php:
if(isset($_POST['search'])) {
return 'yes';
}
return 'no';
And the response text isn't giving me anything, am I doing something wrong?
instead of returning, call
use
if(isset($_POST['search']))
{
echo 'yes';
}
else
{
echo 'no';
}
You want echo
not return
.
The JS is reading the HTTP response. So you need to respond with some data, not just return it to the calling (PHP) function.
use echo
, in function you use return
if(isset($_POST['search'])) {
echo 'yes';
}
echo 'no';
Try this :
if(isset($_POST['search'])) {
echo 'yes';
}
echo 'no';