Kichu
Kichu

Reputation: 3267

ajax calling of php function from a php page

This is my jQuery code:

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

In process.php page I have more than one function. One function is sendmail().

How can I call this function through ajax? I wrote the code as: url: "process.php/sendmail", but nothing happens.

Upvotes: 3

Views: 4763

Answers (6)

KV Prajapati
KV Prajapati

Reputation: 94645

With Ajax call pass some data to process.php.

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

In php page,

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

Upvotes: 5

MAK
MAK

Reputation: 291

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

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

Upvotes: 8

Chandresh M
Chandresh M

Reputation: 3828

May below code helpful to you..

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

Try this..

Thanks.

Upvotes: 2

jogesh_pi
jogesh_pi

Reputation: 9782

to call send mail function pass a parameter to process.php file, and detect that parameter like:

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

now in process.php USE:

$_REQUEST['sendMail'] 

to detect the value..

Upvotes: 0

Amit Patil
Amit Patil

Reputation: 3067

Try using if else and process.php?fun=sendmail

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

Upvotes: 0

Umbrella
Umbrella

Reputation: 4788

There is no automatic means of telling PHP to call your functions, but you can accomplish this with a simple check. Since you are trying URLs like "process.php/sendmail" you can test on the PHP variable $_SERVER['PATH_INFO']

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

Upvotes: 0

Related Questions