-2

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!";
        }
    }  
?>
Aravind D
  • 32
  • 7
  • And it won't, because your message in another `if-else` branch. – u_mulder Jul 26 '17 at 06:33
  • Use HTML code to show msg on a specific position. Example - `echo "
    Message
    ";`
    – Ashiqur Rahman Jul 26 '17 at 06:36
  • @AshiqurRahman That doesn't help at all. His `echo "Email sent!";` would work too, there is another error in this code. – Twinfriends Jul 26 '17 at 06:39
  • @AravindDevaraj When you submit the form, do you get any message, or just a blank page? I just tested your code and I got the "Error!" Message, means that the `mail()` function failed... actually I don't know if its due to my server I'm testing on, not sure if mailfunction is activated. Do you get any mails when you try to submit the form? It would be nice if you can tell us what exactly happens after you submit. Blank page / Error message? – Twinfriends Jul 26 '17 at 06:40
  • Send message is showing but the form fields are deleted – Aravind D Jul 26 '17 at 06:42
  • @AravindDevaraj Wait I don't get it. In your post you said that the "Email sent!" message is not displaying, now you say it is. So what now? Also you said that you want to delete the form fields after submitting... now you say you won't. Please tell us what you want. And since your english isn't the best (sorry), take you some time to write. Read it again, otherwise you'll just confuse us more and more and we won't help you at all.... Really looking forward to solve your problem with you. – Twinfriends Jul 26 '17 at 06:44
  • sorry for the trouble my english is not good lets me explain clearly 1. while i filled up the details in conact form and i click submit the display message only displaying the contact form name, email address etc.,, fields are not showing in the page – Aravind D Jul 26 '17 at 06:51
  • @Twinfriends From what he's saying it appears he wants to make an Ajax call do fields don't disappear and returned variable from the php page to be displayed besde his button. – Adrian Jul 26 '17 at 06:52
  • @Adriani6 I don't get it. No idea how you understand what he want. For me its totally weird what he's saying, in my opinion he didin't say the same in his startpost and his comments at all. – Twinfriends Jul 26 '17 at 06:59
  • Before Submitting the Form Look up the image https://i.stack.imgur.com/hNpQL.png After Submitting the form look up this image https://i.stack.imgur.com/u54dN.png – Aravind D Jul 26 '17 at 08:41

2 Answers2

0

You can redirect the page after sent email or also can set a variable like $msg='email sent'; and call this variable before your submit button.

    $action=$_REQUEST['action'];

    if ($action!="")    /* display the contact form */
        {
$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==""))
            {
            header('location: yoururl?msg=error');
            //$msg   =  "All fields are required, please fill <a href=\"\">the form</a> again.";
            }
        else{       
            @mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

            header('location: yoururl?msg=succss');
            //$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" >

          <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>
    <?php if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='success'){echo "Email sent!";}if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='error'){echo "All fields are required, please fill <a href=\"\">the form</a> again.";} ?>
            <?php echo $msg; ?>
            <input type="submit" value="Submit">
            </form>
Zaid Bin Khalid
  • 748
  • 8
  • 25
0

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>
Zedex7
  • 1,624
  • 1
  • 12
  • 21
  • No need to predefine `$msg` here. Variables defined inside if/else are also accessible outside of it in php. http://php.net/manual/de/language.variables.scope.php – Twinfriends Jul 26 '17 at 06:47
  • @ZaheerAttar Email sent is not displaying bro – Aravind D Jul 26 '17 at 09:16
  • @ZaheerAttar thankq bro works fine im not familiar with php my another request after submitting the form when i refresh the page email sent message is still displaying i doesn't need like that when i refreshed the page i need to fill the form from intial – Aravind D Jul 26 '17 at 09:55
  • It is happening because when you refresh page after submitting once, it is submitting request again, so by nature it is showing `Email Sent`. You can try some javascript to detect page refresh & reset the form. – Zedex7 Jul 26 '17 at 10:02
  • @ZaheerAttar bro thnkx right now im facing a issue that email is not receiving to recipient email id kindly look up the code bro – Aravind D Jul 27 '17 at 06:24
  • @AravindDevaraj Mail is issue related with mail server configuration. You need to see `1)` https://stackoverflow.com/questions/5342822/php-mail-function-on-localhost , `2)` https://stackoverflow.com/a/23905054/5350773 , `3)` http://php.net/manual/en/function.mail.php – Zedex7 Jul 27 '17 at 06:50
  • @ZaheerAttar its working fine bro thnkx i need javascript for "when i refresh the page ""Email sent"" message not to be show also for when i click go back – Aravind D Jul 27 '17 at 11:30
  • @ZaheerAttar Bro need java script for "when i refresh the page ""Email sent"" message not to be show also for when i click go back" – Aravind D Jul 28 '17 at 06:00
  • See this .. https://stackoverflow.com/a/8013450/5350773 , https://stackoverflow.com/a/17628669/5350773 – Zedex7 Jul 28 '17 at 07:47
  • @ZaheerAttar while i refresh the page after submitting the form the error message that "Confirm Form Resubmission" is showing could you say how to prevent from tat error .,,, thnkx – Aravind D Jul 28 '17 at 09:12