form submission reverts to index.php template

I have created a contact us form which sends an email when submitted. While the email triggers successfully, the submiss

I have created a contact us form which sends an email when submitted. While the email triggers successfully, the submission leads to a page with my index.php template without any change in the URL. It might have to do with the template hierarchy, but I've tried all other methods mentioned, and none of them have worked. Here's my code for the contact us page with the form.

<?php /* Template Name: Contact Us */ ?> 
<?php get_header(); ?> 
<?php
  $name = $_POST['name']; 
  $from = $_POST['email']; 
  $message = $_POST['message']; 
  $subject = "Message for Aria Event Planners from '$name' "; 
  $to = "[email protected]"; 
  $headers = "MIME-Version : 1.0" . "\r\n"; 
  $headerx .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
  $headers = "From: <$from> \r\n"; 
?> 
<div class="container">
  <div class="header for-text-layout">Contact Us </div>
  <div class="for-text-layout p-3">
    <div></div>
    <form method="POST">
      <p>
        <label>Name</label>
        <input type="text" name="name" placeholder="Your Name" class="input" id="name" required>
      </p>
      <p>
        <label>E-Mail Address  </label>
        <input type="email" name="email" placeholder="Your E-Mail" class="input" required>
      </p>
      <p class="message">
        <label>Message  </label>
        <textarea rows="3" cols="30" class="input" name="message" placeholder="Your Message Here" required></textarea>
      </p>
      <p class="submit">
        <button type="submit" name="submit" id="submit">Send Message</button>
      </p>
      <div>
        <?php
          if(isset($_POST['submit'])){          
            if(mail($to, $subject, $message, $headers)){
              get_site_url('/email-success/');          
            } else {
              get_site_url('/email-failure/');          
            }       
          }             
        ?>      
      </div>
      <div></div>   
    </form> 
  </div> 
</div>
<?php get_footer(); ?>

I have created a contact us form which sends an email when submitted. While the email triggers successfully, the submission leads to a page with my index.php template without any change in the URL. It might have to do with the template hierarchy, but I've tried all other methods mentioned, and none of them have worked. Here's my code for the contact us page with the form.

<?php /* Template Name: Contact Us */ ?> 
<?php get_header(); ?> 
<?php
  $name = $_POST['name']; 
  $from = $_POST['email']; 
  $message = $_POST['message']; 
  $subject = "Message for Aria Event Planners from '$name' "; 
  $to = "[email protected]"; 
  $headers = "MIME-Version : 1.0" . "\r\n"; 
  $headerx .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
  $headers = "From: <$from> \r\n"; 
?> 
<div class="container">
  <div class="header for-text-layout">Contact Us </div>
  <div class="for-text-layout p-3">
    <div></div>
    <form method="POST">
      <p>
        <label>Name</label>
        <input type="text" name="name" placeholder="Your Name" class="input" id="name" required>
      </p>
      <p>
        <label>E-Mail Address  </label>
        <input type="email" name="email" placeholder="Your E-Mail" class="input" required>
      </p>
      <p class="message">
        <label>Message  </label>
        <textarea rows="3" cols="30" class="input" name="message" placeholder="Your Message Here" required></textarea>
      </p>
      <p class="submit">
        <button type="submit" name="submit" id="submit">Send Message</button>
      </p>
      <div>
        <?php
          if(isset($_POST['submit'])){          
            if(mail($to, $subject, $message, $headers)){
              get_site_url('/email-success/');          
            } else {
              get_site_url('/email-failure/');          
            }       
          }             
        ?>      
      </div>
      <div></div>   
    </form> 
  </div> 
</div>
<?php get_footer(); ?>
Share Improve this question edited Aug 26, 2019 at 9:28 Antti Koskinen 6,0538 gold badges15 silver badges26 bronze badges asked Aug 24, 2019 at 23:48 Divya M.P.Divya M.P. 1
Add a comment  | 

1 Answer 1

Reset to default 0

I did some testing locally with your code. I think the problem is your form input names clashing with names reserved by WP. Also redirecting to success/error page should happen before get_header() to avoid any headers sent errors.

So, add some prefix/suffix to your input names, like so

<input type="text" name="my-name" placeholder="Your Name" class="input" id="name" required>

And move the submitting routine up,

if(isset($_POST['my-submit'])){
    if(mail($to, $subject, $message, $headers)){
      wp_redirect( home_url( '/email-success/' ) );
      exit;
    } else {
      wp_redirect( home_url( '/email-failure/' ) );
      exit;
    }
  }

get_header(); ?>
<!-- form html -->

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745206742a4616628.html

相关推荐

  • form submission reverts to index.php template

    I have created a contact us form which sends an email when submitted. While the email triggers successfully, the submiss

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信