-1

I have a simple HTML contact form with some equally simply PHP to send the email. The PHP to send the email works fine, but I want to reset (or clear) the contact form after the email has been sent. However, no combination of tricks seems to work. My code is below.

<div class="col-sm-6 col-md-8">
   <form action="" id="contactForm" method="POST" autocomplete="off" enctype="multipart/form-data" target="stay_iframe">
      <input type="hidden" name="action" value="submit">
      <input type="text" style="display:none;" name="company" class="cinput" autocomplete="off" tabindex="-1">
      <div class="col-md-6"> <input name="yourName" id="yourName" value="" class="form-control" placeholder="Name" type="text" required> </div>
      <div class="col-md-6"> <input name="emailAddress" class="form-control" placeholder="Your email address" type="email" required> </div>
      <div class="col-md-12"> <input name="subject" class="form-control" placeholder="Subject" type="text" required> </div>
      <div class="col-md-12"> <textarea name="message" class="form-control" rows="6" placeholder="Message" type="text" required></textarea>
      </div>
      <div class="col-md-12"> <input value="SEND MESSAGE" class="form-control" type="submit"> </div>
      <div class="col-md-12"> <input value="RESET FORM" id="resetButton" class="form-control" type="reset"> </div>
   </form>

   <!-- Target iframe to prevent redirection -->
   <iframe name="stay_iframe" width="1" height="1" style="border:none; display:none;"></iframe>

    <script>
      function thanksAndReset()
      {
         const nameElement = document.getElementById("yourName");
         console.log(nameElement.name);
         console.log(nameElement.value);

         document.getElementById("contactForm").reset();

         document.getElementsByName("yourName").value = "";
         document.getElementsByName("emailAddress").value = "";
         document.getElementsByName("subject").value = "";
         document.getElementsByName("message").value = "";
         //alert("Thank you for your message.");
      }
   </script>

   <?php
   if (isset($_REQUEST['emailAddress']))
       {
         $yourName=$_REQUEST['yourName'];
         $emailAddress=$_REQUEST['emailAddress'];
         $subject=$_REQUEST['subject'];
         $message=$_REQUEST['message'];      
         $from="From: $yourName<$emailAddress>\r\nReturn-path: $emailAddress";
         mail("info@redacted.com", $subject, $message, $from);

         ?> <script> thanksAndReset(); </script> <?PHP
       }  
   ?>

 <button onClick="thanksAndReset();">Test</button>
</div>

The reset button with ID 'resetButton' works. The button at the bottom with 'Test' on it also works.

However, when calling the thanksAndReset() function from inside the PHP, neither the .reset() nor .value = "" lines do anything.

The alert() line works, as does the first bit with document.getElementById("yourName"), except for the fact that when I try to write its value to the console it writes <empty string>. At least I know the thanksAndReset() function is being called.

I get the same behaviour if I call that function using echo '<script type="text/JavaScript">thanksAndReset();</script>';.

I've done a lot of searching and pretty much everything I find relates to making a reset button work. I don't want a reset button, I want to reset automatically (partly for tidiness and partly to safeguard against accidental double submission).

So, how can I make it reset or clear the form after sending the email after clicking the submit button?

Edit: I meant to mention the target i-frame which I use as a trick to stop it reloading the entire page when you click the submit button. This is rather jarring for the user and should be avoided. This is, however, what leads to the need to reset the form.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Robot Head
  • 396
  • 3
  • 16
  • 2
    If it is in the iframe it would have to be `parent.thanksAndReset();` – epascarello May 09 '23 at 19:18
  • `document.getElementsByName("yourName").value` is never going to work. – epascarello May 09 '23 at 19:19
  • Oh, it's so obvious now you point that out! Thanks, @epascarello, it works nicely now. – Robot Head May 09 '23 at 19:23
  • It's disingenuous to downvote my question and flag it as a duplicate just because the two questions mentioned are about things I didn't know about. I wasn't asking about iframes, querySelectAll or getElementsBy. The fact that a _comment_ mentions one of those things does not mean my question is a duplicate. That's ridiculous. – Robot Head Jul 05 '23 at 11:49
  • You really complaining 2 months after you asked it? Second duplicate matches it perfectly. `document.getElementsByName("emailAddress").value` should have been `document.getElementsByName("emailAddress")[0].value` which is explained in detail in that question. – epascarello Jul 05 '23 at 12:25

1 Answers1

-2

You don't need the javascript to do what you are looking to do. Just submit the form and the php will execute and then it should render the page with the blank form.

The PHP is only executed on the server and so cannot be called by the javascript directly.

Schleis
  • 41,516
  • 7
  • 68
  • 87
  • The form is being submitted to an iframe, so it won't render a new page in the main viewport. – Quentin May 09 '23 at 19:10
  • Thanks, Quentin. I forgot to mention that! I've clarified in the main question. I do this to stop it reloading the entire page, which is jarring for the user. – Robot Head May 09 '23 at 19:13
  • 1
    I would call it "normal behaviour for the WWW" rather than "jarring for the user" – Quentin May 09 '23 at 19:14
  • 1
    @RobotHead The typical way to do that is via AJAX, and has been for like a decade now. Your life will be much easier if you learn it. – ceejayoz May 10 '23 at 12:25