Aravind Devaraj Aravind Devaraj - 3 years ago 60
PHP Question

Php Contact Form issue

I'm having problems with this code. After submitting the filled form the display message (Email sent!) is not displaying next to the submit button. I need to display the Email sent message next to the submit button on the contact form. After submitting the form, I also need to clear the form fields even after a refresh or a back action. Kindly help me out with this code

Form Action Images:

Before Submitting the Form

After Submitting the form

<?php

$action=$_REQUEST['action'];

if ($action=="") /* display the contact form */
{
?>

<form name="contactform" method="post" enctype="multipart/form-data">

<input type="hidden" name="action" value="submit">

<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >

<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required>

<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >

<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>

<input type="submit" value="Submit">
</form>



<?php
}

else /* send the submitted data */

{

$your_name = $_REQUEST['your_name'];

$email = $_REQUEST['email'];

$mobile_number = $_REQUEST['mobile_number'];

$message = $_REQUEST['message'];

$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";

$recipient = "name@email.com";

$subject = "Contact Form";

$mailheader = "From: $email \r\n";

if (($your_name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill <a href=\"\">the form</a> again.";
}
else{
@mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

echo "Email sent!";
}
}
?>

Answer Source

Your code should be like this. Notice isset($_POST['submit'], $msg , value="".

<?php

    if(isset($_POST['submit']))
    {

    $your_name = $_REQUEST['your_name'];

    $email = $_REQUEST['email'];

    $mobile_number = $_REQUEST['mobile_number'];

    $message = $_REQUEST['message'];

    $formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";

    $recipient = "name@email.com";

    $subject = "Contact Form";

    $mailheader = "From: $email \r\n";

        if (($your_name=="")||($email=="")||($message==""))
            {
            $msg = "All fields are required";
            }
        else{       
            @mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

            $msg = "Email Sent";
            }
        }  
    ?>

        <form name="contactform" method="post" enctype="multipart/form-data">

            <input type="hidden" name="action" value="submit">

          <label for="your_name">Your Name <font color="red">*</font></label>
          <input  type="text" id="reset" name="your_name"  placeholder="Enter Your Name" maxlength="20" size="40" value="">

          <label for="email">Email Address <font color="red">*</font></label>
          <input  type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required value="">

          <label for="mobile_number">Mobile Number</label>
          <input  type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">

          <label for="message">Message <font color="red">*</font></label>
          <textarea  name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>

            <input type="submit" name="submit" value="Submit">

          <?php echo $msg; ?>
            </form>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download