使用ajax url来调用函数
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
function toptable()
{
echo"something happens in here";
}
index.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?
希望我能正确地问这个因为我知道我想要它做什么但似乎无法找到任何答案 来自搜索。 p>
我有一个func.php页面,其中包含我的所有功能,我希望ajax使用该页面中的一个功能。 p>
func.php p>
function toptable()
{
echo“此处发生的事情”;
}
code> pre>
\ n index.php p>
&lt;?php include'func.php'; ?&gt;
&lt; script type =“text / javascript”&gt;
function check_username(){
uname = document.getElementById(“username”)。value;
var params =“user_id =”+ uname; \ n var url =“topoftable()”;
$ .ajax({
type:'POST',
url:url,
dataType:'html',
data:params,
beforeSend:function (){
document.getElementById(“right”)。innerHTML ='checking';
},
完成:function(){
},
success:function(html){
document .getElementById(“right”)。innerHTML = html;
}
});
}
&lt; / script&gt;
code> pre>
有意义吗? p>
div>
It's not working like that.
Your AJAX request should look something like this:
$(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
:
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 $('#...')
.
PHP is server side, JS is client side. You need to call the PHP script in the ajax request, not the function.
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"
.
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