publicknowledge
publicknowledge

Reputation: 644

Send an email during form submit

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.

Upvotes: 4

Views: 464

Answers (2)

Blue Rose
Blue Rose

Reputation: 533

<?php 

if(isset($_POST['submit'])){
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $toemail="[email protected]";
    $subject="Sending Mail";
    $message="Firstname:".$fname."<br/>";
    $message.="Lastname:".$lname;
    $headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";
    //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

Upvotes: 1

Related Questions