APD Toronto
asked on
How Do I Call PHP from JS/JQuery? AJAX?
Hi Experts,
How Do I Call PHP from JS/JQuery? I'm thinking AJAX?
A common example is "Load More Results", where without refreshing the page, I use a jquery listener to call a PHP function (or, if I cannot be as specific, then a PHP page), that would return to the JS a JSON object.
In theory, this should be possible with AJAX, but how? as well, how wold I stop everything else from firing until this process is complete.
Thank you!
How Do I Call PHP from JS/JQuery? I'm thinking AJAX?
A common example is "Load More Results", where without refreshing the page, I use a jquery listener to call a PHP function (or, if I cannot be as specific, then a PHP page), that would return to the JS a JSON object.
In theory, this should be possible with AJAX, but how? as well, how wold I stop everything else from firing until this process is complete.
Thank you!
ASKER
Hi Dave,
What would the returnfunc.php look like?
I mean, once you have the $results array, how do you pass it back to JS?
What would the returnfunc.php look like?
I mean, once you have the $results array, how do you pass it back to JS?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
AJAX makes a call to a script. How you structure that script is up to you - the script returns data and in the AJAX complete function you define what must be done with that data.
A simple example
This approach uses jQuery's load function which executes the script and loads the returned content into the specified element.
If you want more control
For a JSON return
Remember the A in AJAX is for asynchronous which means you do your processing of returned data in the completion function.
Now if you want to trigger the above on an event you simply link it to an event handler
This demonstrates executing the AJAX call on a click event - it also shows the $.ajax() function of which the $.post and $.load are aliases.
A simple example
<div id="content"></div>
<script>
$(function() {
$('#content').load('yourscript.php');
});
</script>
This approach uses jQuery's load function which executes the script and loads the returned content into the specified element.
If you want more control
<script>
$(function() {
$.post('yourscript.php',data, function(resp) {
// do something with return
});
});
</script>
For a JSON return
<script>
$(function() {
$.post('yourscript.php',data, function(resp) {
// do something with JSON return
}, 'JSON');
});
</script>
Remember the A in AJAX is for asynchronous which means you do your processing of returned data in the completion function.
Now if you want to trigger the above on an event you simply link it to an event handler
This demonstrates executing the AJAX call on a click event - it also shows the $.ajax() function of which the $.post and $.load are aliases.
$(function() {
$('.someclass').click(function(e) {
e.preventDefault();
$.ajax({
url: 'yourscript.php',
data: {val: 1, someothervalue: 'text'},
type:'POST',
dataType:'JSON'
}).then(function(resp) {
// completion processing here
});
});
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.dibsplace.com/webdev/ndxv1.php