0

I have a code like this

<script type="text/javascript">

function email(){
var myTextField = document.getElementById('email_hidden').value;
//alert(myTextField);
$.ajax({    
    type: "POST",
    url: "sendmail.php",
    data: "email_user="+myTextField,
    success: function(response){
    alert(response);    

}
});
}
</script>

My sendmail.php code is like this.

    <?php
    $to = $_REQUEST['email_user'];
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: Haan Test<[email protected]>\r\n";
    $message_body = "Your Details";
    $subject = "Check mail";
    mail($to, $subject, $message_body, $headers);
    echo "success";
    ?>

and my html looks like this.

   <div id="email_hide"><a href="#" onclick="email()" >Send Email</a></div>

I am getting the response in my firebug console as success but i could not alert the response. I can receive the mail also for the above jquery ajax functionality(sendmail.php)

In other words, how to alert the response after ajax call.. I have tried onComplete also. plz help me with this.

My Scenerio is like this: User page(index1.html) >> Paypal Sandbox (for transaction) >> User page(index2.html) Here in index2.html, i fetch last transaction details from paypal sandbox with a send mail at the top right corner, so when user clicks the send mail(anchor tag) the mail should be sent via ajax jquery call.

Thanks Haan

8
  • 1
    what data format is response? Commented Mar 7, 2012 at 5:31
  • can you paste the exact message data received in your firebug, after the request is complete? Commented Mar 7, 2012 at 5:34
  • @charlietfl - the response from sendmail.php is success. i have edited my code. plz check it Commented Mar 7, 2012 at 6:02
  • @linuxeasy - juz i received a success in my firebug console, and i can receive mails from the above code. sendmail.php is working fine. i want my response alert :( Commented Mar 7, 2012 at 6:04
  • 1
    firebug console? firebug console can give you javascript messages like those frmo console.log('success'). A right way would be to check your network tab for the url -> sendmail.php and see what its response is and tell us! Commented Mar 7, 2012 at 6:12

3 Answers 3

5

If ur datatype is json ,then try this

<script type="text/javascript">

function email(){
var myTextField = document.getElementById('email_hidden').value;
//alert(myTextField);
$.ajax({    
    type: "POST",
    url: "sendmail.php",
    data: "email_user="+myTextField,
     dataType: "json", 
    success: function(response){
    alert(response);    

}
});
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

please check my question again, i have reviewed. :D
1

Try using the shorthand jquery method for post:

<script type="text/javascript">

    function email(){
        var myTextField = document.getElementById('email_hidden').value;
        //alert(myTextField);
        $.post("sendmail.php", {"email_user": myTextField}, function(data) {
            alert(data);
        });
    }

</script>

1 Comment

please check my question again, i have reviewed. :D
1

Try add

$.ajax({    
   type: "POST",
   url: "sendmail.php",
   data: "email_user="+myTextField,
   success: function(response){
      alert("Success\n" + response);    
   },
   error: function(errMsg) {
      alert("Error\n" + errMsg);
   }
});

and see which one gets called.

7 Comments

no change. i dnt get any alert even after adding ur code. any other suggestions plz?
@hjaffer2001 it could be possible that your code is timing out on the server side? If you want you can try modify so that echo 'success'; is before mail(); and call flush(); Actually, give it a test and comment out mail(); see what happens. Maybe just do a print_r($_POST); die(); at the beginning of the code?
the scenerio is lik this. i have a page that is redirected from payment paypal sandbox along with my last transaction details. so when user clicks the send mail button after redirect from sandbox paypal, the mail should be sent via jquery ajax
Never mind the scenario :) Have you tried out what I suggested?
This is my response to the print_r($POST);die(); Array ( [email_user] => [email protected] ) Now, your comments plz
|

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.