4

I want to make the contact form to submit the responses when the user hits the submit button. Upon the user hitting the submit button, it should also redirect them to thankyou.html, which I have already accomplished. But the responses should be emailed to me as well. I am stuck on that part and cannot figure it out in the PHP file.

PHP file

$errors = '';
$myemail = 'm.hussainomer03@gmail.com'; // Put Your email address here.
if (empty($_POST['name']) ||
    empty($_POST['email']) ||
    empty($_POST['message'])) {
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
    $email_address)) {
    $errors .= "\n Error: Invalid email address";
}

if (empty($errors)) {
    $to = $myemail;
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. " .
        " Here are the details:\n Name: $name \n " .
        "Email: $email_address\n Message \n $message";
    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";
    mail($to, $email_subject, $email_body, $headers);
    
    // Redirect to the 'thank you' page.
    header('Location: contact-form-thank-you.html');
}

Rest of the code

input[type=text], [type=email], select, textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid #ccc;
    margin-top: 6px;
    margin-bottom: 16px;
    resize: vertical;
}

input[type=submit] {
    background-color: rgb(62, 3, 179);
    color: white;
    padding: 12px 20px;
    border: none;
    cursor: pointer;
}

input[type=submit]:hover {
    background-color: deeppink;
}

.contactform {
    position: relative;
    border-radius: 50px;
    background-color: #f2f2f2;
    padding: 5px;
    z-index: 2;
    display: block;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: auto;
    margin-top: 1%;
    width: 100%;
    animation-name: gradient;
    animation-duration: 3s;
    animation-iteration-count: infinite;
}

.contactform:hover {
    animation-name: gradient;
    animation-duration: 15s;
    animation-iteration-count: infinite;
}


.column {
    float: center;
    width: 50%;
    margin-top: 6px;
    padding: 20px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 40%;

}

.row:after {
    content: "";
    display: table;
    clear: both;
}

@media screen and (max-width: 600px) {
    .column, input[type=submit] {
        width: auto;
        margin-top: 0;
    }
}
<section id="contact">
  <div class="container" data-aos="fade-up">
    <div class="contactform">
      <div style="text-align:center">
        <div class="section-title">
          <h2><br/>Get In Touch</h2>
        </div>
        <p>Feel Free To Reach Out To Me Through This Form! </p>
      </div>
      <div class="row">
        <div class="column">
          <form name="myform" action="contact.php" method="POST">
            <label for="firstname">First Name</label>
            <input type="text" id="firstname" name="firstname" placeholder="Your name.." required>

            <label for="lastname">Last Name</label>
            <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>

            <label for="email">Email:</label>
            <input type="email" id="email" name="email" placeholder="Your Email.." required>

            <label for="subject">Subject</label>
            <textarea id="subject" name="subject" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
            <input type="submit" value="Submit">
          </form>
        </div>
      </div>
    </div>
  </div>
</section>

I tried to rename the classes/objects in the php file before, but that did not do the trick either.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95

6 Answers6

3

Change the action="thankyou.html" to action="yourMail.php" so that the form POST data is sent to your php file containing the mail() function not to your thankyou.html. Also note that you need an SMTP server to send mails from your localhost. I would recommend you to use PHPMailer.

HTML

<form name="myform" action="contact.php" method="POST">
            <label for="name">First Name</label>
            <input type="text" id="name" name="firstname" placeholder="Your name.." required>

       <!--     <label for="lastname">Last Name</label>
            <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required> -->

            <label for="email">Email:</label>
            <input type="email" id="email" name="email" placeholder="Your Email.." required>

            <label for="message">Subject</label>
            <textarea id="message" name="message" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
            <input type="submit" value="Submit">
          </form>
Emmanuel
  • 161
  • 5
  • But like I said, I also need to redirect the user to thankyou.HTML along with the responses being sent to my email. –  Feb 03 '21 at 23:02
  • action tag is used to specify where to send the form data and not where to redirect after submitting. The ```header('Location: contact-form-thank-you.html');``` in your .php file which after sending the mail will redirect to contact-form-thank-you.html. – Emmanuel Feb 03 '21 at 23:06
  • are you executing this on a local system? – Emmanuel Feb 03 '21 at 23:13
  • Ok so in `repl.it` there are two options to choose to write code in: `HTML/CSS/JS` and `PHP` but is there no way I can use the PHP in the `HTML/CSS/JS` Server because that's where my website is on –  Feb 03 '21 at 23:15
0

As mentioned in the above answer, you should be changing the form POST action to your mailTo.php file as it will send the mail. After that you can do is on the success of your mail sent, you can redirect to thankYou.html through PHP server, and on failing you can stay on the same page, show respective errors found/occurred. So it will be,

# on success
header('Location: http://your-website/contact-form-thank-you.html');
exit();
Neel Shah
  • 171
  • 5
0

Change the action="thankyou.html" to action="yourMail.php" so the form data will be sent to PHP file containing mail() function.

<form name="myform" action="contact.php" method="POST">
            <label for="name">First Name</label>
            <input type="text" id="name" name="firstname" placeholder="Your name.." required>

       <!--     <label for="lastname">Last Name</label>
            <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required> -->

            <label for="email">Email:</label>
            <input type="email" id="email" name="email" placeholder="Your Email.." required>

            <label for="message">Subject</label>
            <textarea id="message" name="message" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
            <input type="submit" value="Submit">
          </form>

Maybe this will help you :)

0

hello i wrote the code bellow and edited it if it didn't worked or you faced any problems let me know in the comments i hope you find this answer helpful html page :

<form name="myform" action="contact.php" method="POST">
 <label for="firstname">First Name</label>
 <input type="text" id="firstname" name="firstname" placeholder="Your name.." required>

 <label for="lastname">Last Name</label>
 <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>

 <label for="email">Email:</label>
 <input type="email" id="email" name="email" placeholder="Your Email.." required>

 <label for="subject">Subject</label>
 <textarea id="subject" name="subject" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
 <input type="submit" value="Submit">
</form>

php page :

$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$email_address = $_POST['email'];
$message = $_POST['subject'];
if(empty($fname) || 
empty($lname) || 
empty($email_address) || 
empty($message)){
    $errors .= "\n Error: all fields are required";
}else{
//some other code 
$to = $myemail;
$email_subject = "Contact form submission:" . $name;
$email_body = "You have received a new message. ".
" Here are the details:\n Name: " . $name . "\n ".
"Email: $email_address\n Message \n " . $message;
$headers = "From:" . $myemail . "\n";
$headers .= "Reply-To:" . $email_address;
mail($to,$email_subject,$email_body,$headers);

header('Location: thank-you.html');
}
  • you should explain why or how your solution fixes the issue. it will be really useful for others to understand. – Sakibul Alam Jul 04 '21 at 15:36
  • our friends wrote $_POST values wrongly and some of mail function content wrongly also so when the post name doesn't match the posted values it will not work – Abdurrahman Alsallakh Jul 04 '21 at 16:09
0

As you mentioned you already accomplished redirecting to thankyou.html, but the email is not being sent to you. The issue might lie with mail($to,$email_subject,$email_body,$headers);.

You see, mail doesn't really send the mail by itself, rather it invokes sendmail in the host, which in turn queues the message to be sent through the local exchange. So

  • PHP may fail to invoke sendmail (in this case, mail should return false)
  • sendmail may fail (to check try https://stackoverflow.com/a/13390926/1626321)
  • in many cases, your hosting may have a dummy sendmail
  • your email may be blocked/discarded by the exchange (mark it as spam/suspicious, many hosting also block mail to promote their own email product)

The best solution is to switch to using SMTP, using something like PHPMailer (https://github.com/PHPMailer/PHPMailer). There should be plenty of tutorials out there on how to use it. You can use SMTP server from your hosting or maybe even use Gmail.

More comprehensive answer can be found here by @John Conde https://stackoverflow.com/a/24644450/1626321

Sakibul Alam
  • 1,731
  • 2
  • 21
  • 40
0
     <section id="contact">
        <div class="container" data-aos="fade-up">
            <div class="contactform">
                <div style="text-align:center">
                    <div class="section-title">
                        <h2><br/>Get In Touch</h2>
                    </div>
                    <p>Feel Free To Reach Out To Me Through This Form! </p>
                </div>
                <div class="row">
                    <div class="column">
                        <form name="myform" action="mailto:youraddr@domain.tld" method="POST">
                        <label for="firstname">First Name</label>
                        <input type="text" id="firstname" name="firstname" placeholder="Your name.." required>

                        <label for="lastname">Last Name</label>
                        <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>

                        <label for="email">Email:</label>
                        <input type="email" id="email" name="email" placeholder="Your Email.." required>

                        <label for="subject">Subject</label>
                        <textarea id="subject" name="subject" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
                        <input type="submit" value="Submit">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>

In HTML you can specify a mailto: address in the element's [action] attribute. What this will do is allow the user's email client to create an email prepopulated with the fields in the .