如何在ajax的URL中调用PHP函数?
我已经编写了一个PHP代码,其中包含一个带有echo语句的单个函数,我希望在ajax代码中将该函数作为url调用。 我做这件事很累如何在ajax的URL中调用PHP函数?,php,html,ajax,Php,Html,Ajax,我已经编写了一个PHP代码,其中包含一个带有echo语句的单个函数,我希望在ajax代码中将该函数作为url调用。 我做这件事很累 <html> <head> <title>Writing PHP Function</title> <script> $.ajax( { url : localhost/practice.php/f=myFirst(
<html>
<head>
<title>Writing PHP Function</title>
<script>
$.ajax(
{
url : localhost/practice.php/f=myFirst();
type: "GET",
data: dataString,
success: function(result)
{
alert(result);
}
});
</script>
</head>
<body>
<?php
function myFirst()
{
echo 'The First ran successfully.';
}
?>
</body>
</html>
编写PHP函数
$.ajax(
{
url:localhost/practice.php/f=myFirst();
键入:“获取”,
数据:dataString,
成功:功能(结果)
{
警报(结果);
}
});
我会尝试根据URL中的参数调用函数。
阿贾克斯部分
$.ajax({url:'localhost/practice.php?f=myFirst',type:'GET'}).done(function(response){
alert(response); // or you can write to your document
});
和PHP文件
<?php
if(isset($_GET) && $_GET['f'] == 'myFirst'){
myFirst();
}else{
die('No GET params received');
}
function myFirst(){
echo 'The First ran successfully.';
}
?>
你所拥有的东西有很多问题。这里只是一个简单的例子,应该对你有用 practice.php
<?php
if(isset($_GET['f']))
echo strip_tags($_GET['f']);
?>
<?php function myFirst() {
echo 'The First ran successfully.';
} ?><html>
<head>
<title>Writing PHP Function</title>
<!-- You need to add the jQuery library -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
// You should activate it on an event like click (unless you want it to autoload)
$("#content").click(function() {
$.ajax({
// Your url is funky, and cannot be terminated with a semicolon
// but rather a comma
url : 'localhost/practice.php?f=<?php myFirst(); ?>',
type: "GET",
// You can do an alert, but I just made it populate a div instead
success: function(result) {
$("#place-to-load").html(result);
}
});
});
</script>
</head>
<body>
<!-- Your php function will not work as you intend because one -->
<!-- is a server-side function and the other is a client-side script -->
<!-- The functions are not interchangeable. That being said, you can -->
<!-- populate a javascript with a php variable but it would need to be -->
<!-- echoed like <?php myFirst(); ?> -->
<div id="place-to-load"></div>
<div id="content">Load</div>
</body>
</html>
index.php
<?php
if(isset($_GET['f']))
echo strip_tags($_GET['f']);
?>
<?php function myFirst() {
echo 'The First ran successfully.';
} ?><html>
<head>
<title>Writing PHP Function</title>
<!-- You need to add the jQuery library -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
// You should activate it on an event like click (unless you want it to autoload)
$("#content").click(function() {
$.ajax({
// Your url is funky, and cannot be terminated with a semicolon
// but rather a comma
url : 'localhost/practice.php?f=<?php myFirst(); ?>',
type: "GET",
// You can do an alert, but I just made it populate a div instead
success: function(result) {
$("#place-to-load").html(result);
}
});
});
</script>
</head>
<body>
<!-- Your php function will not work as you intend because one -->
<!-- is a server-side function and the other is a client-side script -->
<!-- The functions are not interchangeable. That being said, you can -->
<!-- populate a javascript with a php variable but it would need to be -->
<!-- echoed like <?php myFirst(); ?> -->
<div id="place-to-load"></div>
<div id="content">Load</div>
</body>
</html>
编写PHP函数
//您应该在单击之类的事件中激活它(除非您希望它自动加载)
$(“#内容”)。单击(函数(){
$.ajax({
//您的url很奇怪,不能以分号结尾
//而是一个逗号
url:'localhost/practice.php?f=',
键入:“获取”,
//您可以执行警报,但我只是让它填充一个div
成功:功能(结果){
$(“#加载位置”).html(结果);
}
});
});
负载
您想要实现什么目标?如果您调用该函数,它将追加成功运行的第一个,这没有意义您需要进一步详细说明。提供php代码并告诉我们确切的问题是什么。您需要将url更改为localhost/practice.php/?f=myFirst
和2。您需要返回您的值,而不是打印它。3.您应该将请求映射到函数Sir感谢此函数正在工作。。但是我的主要任务是拥有一个PHP文件,它具有多个函数,比如insert,retrieve from数据库。我假设在我的页面上单击insert and show按钮时使用Ajax调用一个特定函数。您将所有这些函数放在practice.php
页面上。您所有的php脚本都在practice.php
页面上。index.php
页面所做的就是激活这些脚本/函数,并将它们调用到index.php
页面上的容器中