magicianiam magicianiam - 1 month ago 14
jQuery Question

ajax to retrieve data from another page after post

I know this is very easy, but i only know the DOM equivalent of this code. the very long one. i've already searched trough some of the questions here in stack but i cant seem to find the solution.

so basically i have this script:

function searchNow(str)
{
$.ajax({
url: "search.php",
type: "POST",
async: false,
data: {"search": str},
success: function(data){
alert("test");
$("#result").html(data);
}
});
}

<table>

<tr>
<td>Search: </td>
<td><input type = "text" name = "search" onBlur="searchNow(this.value)"> </td>
</tr>




this will submit search to search.php to do a query search and retrieve the result and display it at id result.

i can do this easily using the old DOM ajax but then i wanna try using this jquery version instead since it is cleaner and maybe faster.

at my search.php
i have this:

$search = $_POST['search'];
return $search;


sadly i cant seem to return anything at all.

some input would be greatly appreciated, im already starting to be familiar with jquery ajax but only on the same page, not on inter page manipulation.

thank you,
-magician

Answer

Your PHP file should output the value. The ajax is going read that page and get it's content.

$search = $_POST['search'];
echo $search;