-4

I am working on asp.net project. I am trying to send mail using php script (as godaddy is not allowing me to use c# sendmail()). I have to pass subject and body parameters to the mail.

I am able to send parameters to separate php file however I want to do it with inline php. following code works fine but not sure how to pass parameters

<html>
<head>
<script>
function sendmyMail(){

<?php

    mail("mymailid@gmail.com","mySubject","mybodytext","From: \" abc \"");
    echo "Notification Sent!";
?>
}
</script>
</head>
<body onload=sendmyMail();>
</body>
</html>

Any help will be appreciated.

Regards Nikunj

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
earthling
  • 3
  • 3
  • use AJAX or pass the variables from js to php by reloading the page with the data stored as GET/ POST variables. – Traian Tatic Sep 03 '14 at 09:22
  • All your php will be executed before the page is sent to the browser, at which point your javascript will execute. This means that there is never any php in your client side page. You will always have to pass the parameters from your javascript back to the server, whether by AJAX or just navigating to the new page. – Jon Taylor Sep 03 '14 at 09:33
  • Thanks to everyone for taking pains to answer. I was not sure so far why should I learn AJAX. I think now I have a reason... Thanks – earthling Sep 03 '14 at 12:34

1 Answers1

0

Is the mail function a php function? If so, you can't call it with javascript. You have to use something called AJAX.

http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

Upon receiving the ajax call, execute the PHP script.

sleepless_in_seattle
  • 2,132
  • 4
  • 24
  • 35
  • The code I have mentioned is working perfectly fine. Its a javascript function that is executed upon loading of html. I want to pass parameters. Something like function sendmyMail(mySubject, myMessage) that can be used in inline php – earthling Sep 03 '14 at 09:23