简体   繁体   English

ajax从php页面调用php函数

[英]ajax calling of php function from a php page

This is my jQuery code: 这是我的jQuery代码:

$.ajax({  
  type: "POST",  
  url: "process.php",
  success: function(msg){
  }  
});

In process.php page I have more than one function. 在process.php页面中,我有多个函数。 One function is sendmail(). 一个函数是sendmail()。

How can I call this function through ajax? 如何通过ajax调用此函数? I wrote the code as: url: "process.php/sendmail", but nothing happens. 我把代码编写为: url: "process.php/sendmail",但没有任何反应。

this is your script file 这是你的脚本文件

 $.ajax({
       url: "process.php",
       type: "POST",
       data: "functionName=sendmail",
       cache: true,
       success: function(response){

       }

and this is your process.php file 这是你的process.php文件

<?php
if(isset($_POST))
{
   if($_POST["functionName"] == "sendmail")
   {
      sendmail();
   }
}
?>

With Ajax call pass some data to process.php . 使用Ajax调用将一些data传递给process.php

$.ajax({  
  type: "POST",  
  url: "process.php",
  data: {method: 'one'},
  success: function(msg){
  }  
});

In php page, 在php页面中,

if(isset($_POST["method"])) {
    $method = $_POST["method"];
    if($method=="one") {
       //call methods/functions here
    }
}

May below code helpful to you.. 可能低于对您有帮助的代码..

 $.ajax({  
      type: "POST",  
      url: "process.php",
      data: {action: 'sendmail'},
      success: function(msg){
      }  
    });

Try this.. 尝试这个..

Thanks. 谢谢。

There is no automatic means of telling PHP to call your functions, but you can accomplish this with a simple check. 没有自动方法告诉PHP调用您的函数,但您可以通过简单的检查来完成此操作。 Since you are trying URLs like "process.php/sendmail" you can test on the PHP variable $_SERVER['PATH_INFO'] 由于你正在尝试像“process.php / sendmail”这样的URL,你可以测试PHP变量$ _SERVER ['PATH_INFO']

if ('sendmail' == $_SERVER['PATH_INFO']) {
    sendmail();
}

Try using if else and process.php?fun=sendmail 尝试使用if else和process.php?fun = sendmail

if($_GET['fun'] == "Sendmail")
  sendmail()
else
  // something else 

to call send mail function pass a parameter to process.php file, and detect that parameter like: 调用send mail函数将一个参数传递给process.php文件,并检测该参数如:

$.ajax({  
type: "POST",  
url: "process.php",
type: "POST",
data: {sendMail: "1"},
success: function(msg){
}  
});

now in process.php USE: 现在在process.php USE:

$_REQUEST['sendMail'] 

to detect the value.. 检测值..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM