-1

So I have the following set of forms in the like of "form-control" (from bootstrap) and you get 3 different forms to fill in: title, email and the message itself

                    <form class="main_form">
                        <div class="row">
                         
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                                <input class="form-control" placeholder="title" type="text" name="Title">
                            </div>
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                                <input class="form-control" placeholder="Email" type="text" name="Email">
                            </div>
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                                <textarea class="textarea" placeholder="message" type="text" name="Message"></textarea>
                            </div>
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                                <button class="send" >send</button>
                            </div>
                        </div>
                    </form>

My question is, how do you retrieve the info from the form-control's? And then, use that information to send this other script (pressing the send button):

<?php
function send_mail( $subject, $message, $headers){
    // https://stackoverflow.com/questions/5335273/how-can-i-send-an-email-using-php
    $to = "bordadoscreativos.02@gmail.com";
    mail($to, $subject, $message, $headers);
}
?>
ayy_chemixd
  • 113
  • 10
  • 2
    Any decent tutorial about forms will explain how the submission works. Depending on the method, the data is either in `$_GET` or `$_POST`. – El_Vanja May 20 '21 at 19:15

2 Answers2

1

Instead of using php to send the email i would use an online backend form submission service. There is one called formsubmit.io that will do all of the work for you and it's free. I have edited the code so it will send an email with the form details. Just replace the "put your email here" with your email and it should work.

HTML

<form class="main_form" action="https://formsubmit.io/send/put your email here" method="POST">>
                        <div class="row">
                         
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                                <input class="form-control" placeholder="title" type="text" name="Title">
                            </div>
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                                <input class="form-control" placeholder="Email" type="text" name="Email">
                            </div>
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                                <textarea class="textarea" placeholder="message" type="text" name="Message"></textarea>
                            </div>
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                                <button class="send" >send</button>
                            </div>
                        </div>
                    </form>

1

we can write send mail function in the same file where this form html code written but only for seperation purpose, we create a separate php file named as "sendmail.php". put the filename in the action method of form. $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post".

 <form class="main_form" method="post" action ="sendmail.php">
    <div class="row">          
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
        <input class="form-control" placeholder="title" type="text" name="Title">
    </div>
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
        <input class="form-control" placeholder="Email" type="text" name="Email">
    </div>
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
        <textarea class="textarea" placeholder="message" type="text" name="Message"></textarea>
    </div>
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
        <input class="send" type="submit" value="send" name="submitted_form">
    </div>
</div>
</form>

sendmail.php

<?php
  if (isset($_POST['submitted_form'])){
    $subject = $_POST['Title'];
    $message = "Email : $_POST['Email'] Message: $_POST['Message']";
    $headers = "From:abc@xyz.com";
    send_mail($subject,$message,$headers);
  }
function send_mail( $subject, $message, $headers){
    // https://stackoverflow.com/questions/5335273/how-can-i-send-an-email-using-php
    $to = "bordadoscreativos.02@gmail.com";
    if(mail($to, $subject, $message, $headers)){
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
}
?>
  • 1
    wow, what a great answer! thank you so much. talk about overdelivering. just that it throws me this error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number.... on line 4 of sendmail.php. I've tried tinkering with the commas and what have you but its not working – ayy_chemixd May 21 '21 at 01:00
  • EDIT: i got it working! just had to divide your $message string and concatenate it with email. – ayy_chemixd May 21 '21 at 01:12