3

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.

6 Answers 6

8

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();
   }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

5

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
    }
}

4 Comments

but when iam using this i got a error is below Notice: Undefined index: method in E:\wamp\www\promo\install\process.php on line 4
@KichuUser - What is at line 4 in process.php?
$method = $_POST["method"]; this is the code iam wrote in line 4 for checking if the method have any value
ok i got the mistake in your code.there is no need for "" in this code data: "{method: 'one'}",
1

May below code helpful to you..

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

Try this..

Thanks.

1 Comment

when iam using this nothing will happen
0

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();
}

Comments

0

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

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

Comments

0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.