4

Sorry if the title is a bit vague. What I mean to say is I have a code like below:

<?php 
    /* ...generate $to, $subject, $msg, $from variables here */

    if(isset($_POST['ccbutton_pressed'])) {
    if(mail($to, $subject, $msg, $from)){
        echo 'Order Sent';
    }else{
        echo 'Order Sending Error';
    }
}?>

<form method='post' action='SOME_URL_TO_POST_TO' >
    <!-- some visible input types -->
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    <!-- some hidden input types -->
    <input type='hidden' name='hiddenfield1' value='1' />
    <input type='hidden' name='hiddenfield2' value='2' />
    <input type='submit' value='Submit' />
    <input type='hidden' name='ccbutton_pressed' value='1'>
</form>

Is there some way I can send an email upon submission? While taking notes on the following points below:

  1. prefer to php because it is server side and information is kept private
  2. 'action' attribute needs to post and may not be changed (I need it to go to another page/different form)
  3. Open to using Javascript as long as the contents of the email can be hidden somehow (but doubtful)
  4. I already tried putting a php block that triggered when a hidden field got set in that form but it did not work in this case.

Update: Changed code block to explain more information. This is also not a duplicate because in Send email with PHP from html form on submit with the same script, the 'action' is left blank while I need mine to post to somewhere.

I'd also like to add that I have checked the above article and it doesn't work for me.

Community
  • 1
  • 1
publicknowledge
  • 634
  • 1
  • 9
  • 17

2 Answers2

1
<?php 

if(isset($_POST['submit'])){
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $toemail="abc@gmail.com";
    $subject="Sending Mail";
    $message="Firstname:".$fname."<br/>";
    $message.="Lastname:".$lname;
    $headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
    //sending simple mail
    mail($toemail, $subject, $message, $header);
}
?>

<form method='post' action='' >
    <!-- some visible input types -->
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    <!-- some hidden input types -->
    <input type='hidden' name='hiddenfield1' value='1' />
    <input type='hidden' name='hiddenfield2' value='2' />
    <input type='submit' value='Submit' id="submit" name="submit" />
</form>

Cheers

Blue Rose
  • 523
  • 1
  • 3
  • 14
  • then in action="a.php" and add php code to this a.php – Blue Rose Aug 10 '15 at 06:32
  • The problem is when I put the post location in the 'action', the code properly goes to the location but does not email. Not sure if this is because the post happens before anything else can occur so the emailing part gets ignored..? – publicknowledge Aug 10 '15 at 06:36
  • It shouldn't happen. Can you paste your php code in next post? So that I can debug. – Blue Rose Aug 10 '15 at 06:37
0

You can probably look at these links to help you.

Send email with PHP from html form on submit with the same script

How to create an email form that can send email using html

Thanks

Community
  • 1
  • 1
SSV
  • 61
  • 1