I am trying to make a search box in my web application, and I used ajax post to make a request to my server. My question is:
Is it possible to send looped array values from PHP to my JavaScript?
I want to get all of the results from my server.
CLIENT SIDE: Ajax POST request
<script type="text/javascript">
$(document).ready( function() {
$.ajax({
type: "POST",
url: "searchPlaces.php",
data: { searchInput: form.searchTxtId.value },
success: function (result)
{
// Get the search result
}
});
});
</script>
SERVER SIDE (after retrieving the post from ajax, and making queries):
while ($result = mysql_fetch_assoc ($query))
{
$resultName = $result['name'];
$resultAddress = $result['address'];
}
In your
successcallback you can then iterate overresult.resultswhich contains an object with the column names from your query as attributes.It is also advisable to use
dataType: 'json'in your$.ajax({...});arguments to avoid unnecessary guessing of the response type.In case you have more columns in the SQL resultset than you want to forward to the client, you could add a custom array in the loop: