admin管理员组文章数量:1026989
First and foremost thank you for checking out my problem, and for any help you may give!
Okay, so like the title says I'm in need of calling a php function from my index page, which adds a new record in my database as a vote, using JQuery Ajax. This function will return one integer, which will then be printed inside the form button in which it was called from.
Anyone have an idea on how I would acplish this? Any guidance is appreciated!
Edit:
So if I'm using a template object I can just call the
function using ajax and it will return the output?
What would I use as the URL? index.php?
Such as...
function DoVote() {
$.ajax({
type:'POST',
url: 'index.php',
success: function(data) {
$('#voteText').html(data);
}
});
And the form action attribute is posted I'm guessing?
First and foremost thank you for checking out my problem, and for any help you may give!
Okay, so like the title says I'm in need of calling a php function from my index page, which adds a new record in my database as a vote, using JQuery Ajax. This function will return one integer, which will then be printed inside the form button in which it was called from.
Anyone have an idea on how I would acplish this? Any guidance is appreciated!
Edit:
So if I'm using a template object I can just call the
function using ajax and it will return the output?
What would I use as the URL? index.php?
Such as...
function DoVote() {
$.ajax({
type:'POST',
url: 'index.php',
success: function(data) {
$('#voteText').html(data);
}
});
And the form action attribute is posted I'm guessing?
Share Improve this question edited May 16, 2010 at 6:46 NickKampe asked May 16, 2010 at 6:28 NickKampeNickKampe 9623 gold badges14 silver badges25 bronze badges 1- Or just how I can call a php function using Ajax? – NickKampe Commented May 16, 2010 at 6:29
2 Answers
Reset to default 3Yes, create a separate PHP file that calls that function and echos the output. Then load that php file with an AJAX request.
Because PHP is a server side language and jQuery (JavaScript) is Client side you can't directly call a PHP function with it, the most you can do is load a file. However, if the file has something like <?php echo($object->function()); ?>
you can load the file, in essence calling the function.
I'm not sure that (your addition) is correct.
In an arbitrary file you have a PHP function:
<?php
// Called "otherfile.php"
// Function you're trying to call
function doSomething($obj)
{
$ret = $obj + 5;
return($ret);
}
?>
And you have a file (call it ajaxcall.php) that you will load with that AJAX call.
<?php
include("otherfile.php"); // Make the file available, be aware that if that file
// outputs anything (i.e. not wrapped in a function) it
// may be executed
// Grab the POST data from the AJAX request
$obj = $_POST['obj'];
echo($doSomething());
?>
The ajax request simply forces some URL to be access, which runs the php located there. And by any additional parameters (even GET) you may either change the function being called or pass these to the function itself.
First and foremost thank you for checking out my problem, and for any help you may give!
Okay, so like the title says I'm in need of calling a php function from my index page, which adds a new record in my database as a vote, using JQuery Ajax. This function will return one integer, which will then be printed inside the form button in which it was called from.
Anyone have an idea on how I would acplish this? Any guidance is appreciated!
Edit:
So if I'm using a template object I can just call the
function using ajax and it will return the output?
What would I use as the URL? index.php?
Such as...
function DoVote() {
$.ajax({
type:'POST',
url: 'index.php',
success: function(data) {
$('#voteText').html(data);
}
});
And the form action attribute is posted I'm guessing?
First and foremost thank you for checking out my problem, and for any help you may give!
Okay, so like the title says I'm in need of calling a php function from my index page, which adds a new record in my database as a vote, using JQuery Ajax. This function will return one integer, which will then be printed inside the form button in which it was called from.
Anyone have an idea on how I would acplish this? Any guidance is appreciated!
Edit:
So if I'm using a template object I can just call the
function using ajax and it will return the output?
What would I use as the URL? index.php?
Such as...
function DoVote() {
$.ajax({
type:'POST',
url: 'index.php',
success: function(data) {
$('#voteText').html(data);
}
});
And the form action attribute is posted I'm guessing?
Share Improve this question edited May 16, 2010 at 6:46 NickKampe asked May 16, 2010 at 6:28 NickKampeNickKampe 9623 gold badges14 silver badges25 bronze badges 1- Or just how I can call a php function using Ajax? – NickKampe Commented May 16, 2010 at 6:29
2 Answers
Reset to default 3Yes, create a separate PHP file that calls that function and echos the output. Then load that php file with an AJAX request.
Because PHP is a server side language and jQuery (JavaScript) is Client side you can't directly call a PHP function with it, the most you can do is load a file. However, if the file has something like <?php echo($object->function()); ?>
you can load the file, in essence calling the function.
I'm not sure that (your addition) is correct.
In an arbitrary file you have a PHP function:
<?php
// Called "otherfile.php"
// Function you're trying to call
function doSomething($obj)
{
$ret = $obj + 5;
return($ret);
}
?>
And you have a file (call it ajaxcall.php) that you will load with that AJAX call.
<?php
include("otherfile.php"); // Make the file available, be aware that if that file
// outputs anything (i.e. not wrapped in a function) it
// may be executed
// Grab the POST data from the AJAX request
$obj = $_POST['obj'];
echo($doSomething());
?>
The ajax request simply forces some URL to be access, which runs the php located there. And by any additional parameters (even GET) you may either change the function being called or pass these to the function itself.
本文标签: javascriptUsing JQuery Ajax to call a php functionStack Overflow
版权声明:本文标题:javascript - Using JQuery Ajax to call a php function - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660885a2161883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论