php 使用ajax url调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19869146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using ajax url to call function
提问by Steven Vanerp
Hopefully I can ask this correctly cuz I know what I want it to do but can't seem to find any answers from searching.
希望我能正确地提出这个问题,因为我知道我想要它做什么,但似乎无法从搜索中找到任何答案。
I have a func.php page where I have all my functions and I want ajax to use one function from that page.
我有一个 func.php 页面,其中包含我所有的功能,并且我希望 ajax 使用该页面中的一个功能。
func.php
函数文件
function toptable()
{
echo"something happens in here";
}
index.php
索引.php
<?php include 'func.php'; ?>
<script type="text/javascript">
function check_username() {
uname=document.getElementById("username").value;
var params = "user_id="+uname;
var url = "topoftable()";
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: params,
beforeSend: function() {
document.getElementById("right").innerHTML= 'checking' ;
},
complete: function() {
},
success: function(html) {
document.getElementById("right").innerHTML= html ;
}
});
}
</script>
Make sense?
有道理?
回答by speccode
It's not working like that.
它不是那样工作的。
Your AJAX request should look something like this:
您的 AJAX 请求应如下所示:
$(document).ready(function() {
//some even that will run ajax request - for example click on a button
var uname = $('#username').val();
$.ajax({
type: 'POST',
url: 'func.php', //this should be url to your PHP file
dataType: 'html',
data: {func: 'toptable', user_id: uname},
beforeSend: function() {
$('#right').html('checking');
},
complete: function() {},
success: function(html) {
$('#right').html(html);
}
});
});
And your func.php
:
还有你的func.php
:
function toptable()
{
echo 'something happens in here';
}
//here you can do some "routing"
$func = $_POST['func']; //remember to escape it
switch ($func) {
case 'toptable':
toptable();
break;
default:
//function not found, error or something
break;
}
Also check that I change document.getElementById
to jQuery selector $('#...')
.
还要检查我是否更改document.getElementById
为 jQuery selector $('#...')
。
回答by RafH
PHP is server side, JS is client side. You need to call the PHP script in the ajax request, not the function.
PHP是服务器端,JS是客户端。您需要在 ajax 请求中调用 PHP 脚本,而不是函数。
Something like
就像是
[...]
var url = "func.php";
$.ajax({
type: 'POST',
url: url,
[...]
func.php
函数文件
<?php
function toptable()
{
echo "something happens in here";
}
toptable();
?>
In this case, the html
argument in the success handler of the ajax request will be equal to "something happens in here"
.
在这种情况下,html
ajax 请求的成功处理程序中的参数将等于"something happens in here"
。
回答by RafH
Make sense?- no. You can't mix PHP (a server-side technology) with Javascript (client-side). You can use AJAX to ask a server to perform some function for you, but not by using the function name as a URL. You should create a PHP page to accept your AJAX request, perform the function and return the result, and use the URL of that page in your AJAX request
有道理?- 不。您不能将 PHP(一种服务器端技术)与 Javascript(客户端)混合使用。您可以使用 AJAX 请求服务器为您执行某些功能,但不能使用函数名称作为 URL。您应该创建一个 PHP 页面来接受您的 AJAX 请求,执行该功能并返回结果,并在您的 AJAX 请求中使用该页面的 URL