I have a question regarding, PHP functions, jQuery and AJAX. If I have a button in my php index like this:
<input type="submit" value="Download" id="download"/>
And I have another php file (dubs.php) that contains this:
<?php
function first(){
echo 'first';
}
function second(){
echo 'second';
}
?>
And my jQuery, like this:
$(document).ready(function(e) {
$("#download").click(function(){
$.ajax({
type: "GET",
url: "dubs.php",
});
});
});
How do I tell my AJAX request to select for example the second function?
I have no idea on how to do this, I've tried it with "success: first()"
or with "success: function(){ first() }"
but that did not work.
I have a question regarding, PHP functions, jQuery and AJAX. If I have a button in my php index like this:
<input type="submit" value="Download" id="download"/>
And I have another php file (dubs.php) that contains this:
<?php
function first(){
echo 'first';
}
function second(){
echo 'second';
}
?>
And my jQuery, like this:
$(document).ready(function(e) {
$("#download").click(function(){
$.ajax({
type: "GET",
url: "dubs.php",
});
});
});
How do I tell my AJAX request to select for example the second function?
I have no idea on how to do this, I've tried it with "success: first()"
or with "success: function(){ first() }"
but that did not work.
5 Answers
Reset to default 14In your ajax pass some params to identify which function you want to use like this
$("#download").click(function(){
$.ajax({
type : "POST",//If you are using GET use $_GET to retrive the values in php
url : "dubs.php",
data : {'func':'first'},
success: function(res){
//Do something after successfully pleting the request if required
},
error:function(){
//If some error occurs catch it here
}
});
});
And in your php file
you can retrive the values in data
send via ajax and do the following
if(isset($_POST['func']) && $_POST['func']=='first'){
first();
}
else{
second();
}
This is what I would do:
Your PHP:
<?php
function first(){
echo 'first';
}
function second(){
echo 'second';
}
if (isset($_POST["first"])) first();
if (isset($_POST["second"])) second(); //add 'else' if needed
?>
your jQuery:
$.post("dubs.php", {"first":true}, function(result) {
$("#someDivToShowText").text(result);
});
Then, according to the object you send to $.post
, the php file will know which function to run.
Try this in your PHP page:
<?php
function first(){
echo 'first';
}
function second(){
echo 'second';
}
switch($_POST['func']) {
case "first":
first();
break;
case "second":
second();
break;
default:
// Define your default here }
?>
and this in your JS:
$(document).ready(function(e) {
$("#download").click(function(){
$.ajax({
type: "GET",
url: "dubs.php",
data: {'func':'first'}
});
});
The func
variable will tell php which function to run!
});
why don't you try to pass with data:{func:f1}
and get it on php side and if f1
is there then fire the first function. Although you can send multiple:
jQuery:
$("#download").click(function(e){
e.preventDefault(); // <----stops the page refresh
$.ajax({
type: "GET",
url: "dubs.php",
data:{'func':'f1'}
});
});
PHP:
<?php
function first(){
echo 'first';
}
function second(){
echo 'second';
}
if(isset($_GET['func']=='f1'){
first();
}else{
second();
}
?>
JS
$("#download").click(function(){
$.ajax({
type: "GET",
url: "dubs.php",
data: {'function':'first'}
});
});
PHP
call_user_func($_GET['function']);
NOTE
Be careful with $_GET
parameters, better check first the contents of $_GET