I’m kind of new to jquery/ajax calls, I have this code below to pull from php page json values.
$(function(){
$('#search').keyup(function() {
sendValue($(this).val);
});
});
function sendValue(str)
{
$.post(
"back/search.php",
{ sendToValue: str },
function(data) {
if(!data.empty){
//Put the result in the suggest div
$("#suggest").html(data.returnedFromValue);
}
},"json"
);
}
and here is the search.php file
$values = $database->get_by_name($_POST['sendToValue']);
if( !$values ) {
echo json_encode(array("returnedFromValue" => "ERROR"));
}
else
{
foreach($values as $value) {
echo json_encode(array("returnedFromValue" => $value));
}
}
The problem that that the value doesn’t appear in the suggest div, the only output there is “error”, when I check the ajax request in firebug under post section it gives me the message that sendToValue is not defined any ideas? Thanks!
The first thing that leaps out at me (there may be other problems, but this is the one I noticed first) is that you’re missing the parentheses after
valin the call tosendValue:should be:
Currently, you’re passing the
valmethod itself into thesendValuefunction, rather than the result of calling that method.