最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

form submission reverts to index.php template

programmeradmin0浏览0评论

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 -->
发布评论

评论列表(0)

  1. 暂无评论