简体   繁体   中英

PHP mail function gets triggered every time i load the page

I am trying to send mails using PHP's mail() function.

I have a button (Send Mail), clicking on which the mail has to be triggered. Since PHP is a server side scripting language, I used Javascript to trigger the PHP function. A very unusual event occurs. Whenever I load the page, the mail gets triggered. So, I put alerts and echos to check if the code logic is correct.

Funny thing is that the mail does not get triggered when I click the button. Where am I going wrong? Please see my code:

<input type="button" onclick="jMail()" value="Send Mail"/>        
<script>
  function jMail()
  {
    alert("Inside Javascript Function");
    alert("<?php PHPFunction(); ?>");
  }
</script>

<?php
  function PHPFunction(){
    echo("Inside PHP Function");
    mail("to@example.com", "Help Mee", "Definitely Help me", "From:from@example.com");
    echo("Mail Sent");
  }
?>

PHP is a server side language, while Javascript is a client side language. I think you are confusing the two, and trying to mix their use in a way that would never work.

When the page is loaded, these steps occur in sequence:

  1. The server interprets the PHP code in your page, and renders a page that does not contain any PHP code.
  2. The client, viewing the page, does not obviously have access to any PHP function, because it sees only the result of the elaboration. It still can use Javascript to achieve dinamic behavior of the page (ie changes without refreshing), and things like AJAX to make requests to the server still without re-rendering the page.

<input type="button" onclick="jMail()" value="Send Mail"/>

The event onclick is indeed triggered when you press the button, but after the page has been fully loaded. At this time, all the PHP code has been already interpreted by the server, and there is no chance to execute it again without reloading the page.

EXAMPLE : here you can see the result of the elaboration of your code (under stdout ). As you can see, the client is left with a PHP-free web page.

If you're looking for a way to trigger PHP code when an event occurs after the page has been loaded, I suggest you take a look at this question .
Also, this question on programmers.stackexcange.com could help you clarify the difference between client side and server side if it isn't clear.

You cannot trigger PHP from javascript that way. Create another PHP file, and call it using AJAX javascript requests.

<form method="post">
    <input type="submit" value="Send Mail" />
</form>
<?php

    if(isset($_POST)){
        ///do sent mail here
        mail("to@example.com","Help Mee","Definitely Help me","From:from@example.com");
    }
?>

every time you do

<?php PHPFunction();

you send the mail..

maybe you could play with something like

<?php
if(array_key_exists('postMail',$_POST)){
echo ("Inside PHP Function");
//if(empty($_POST['mailContent'])){/*angry blablabla*/}
mail("to@example.com","Help Mee",$_POST['mailContent'],"From:from@example.com");
echo ("Mail Sent");
die();
}?>

<input type="button" onclick="jMail()" value="Send Mail"/>        
<script>
function jMail()
{
alert("Inside Javascript Function");
var xhr=new XMLHttpRequest();
xhr.open("POST","?");
var fd=new FormData();
fd.append("postMail","true");
fd.append("mailContent","fooooobar");
xhr.send(fd);
}
</script>

PHP is a server side scripting language which has already been interpreted by the server and then sent to client(ie browser) for interpretation of any client side script( ex JavaScript).
But if want a responsive webpage to be handled by your server try to use Form and inputs tags and their attributes to send your request back to server
But if you want a Quick way Try using AJAX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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