Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

maze
maze
17,027 Points

Why redirect to index.php file after a submit custom-contact form?

Hi! I have a custom contact form without plugin and I would like to know if is posible after submit remain in the same custom-page of contact. When I've submitted redirect to the same link but in the index.php file :(

I know that with AJAX is posible not reload the page but I want to know what the problem is, thanks a lot!

PD: Sorry if you see grammar errors :P

5 Answers

maze
maze
17,027 Points

The problem was the form's inputs attribute names were the same that the input's id. I don't know the reason, but when I changed them everything works :)

<form id="eform" role="form" method="POST" action="<?php the_permalink(); ?>">
        <h3>Todos los campos con un * son obligatorios.</h3>
        <div class="form-group">
          <h4><label for="name">Nombre*</label></h4>
          <input id="name" type="text" name="message_name" required>
          <span class="validation">Por favor introduce tu nombre</span>
        </div> 

        <div class="form-group">
          <h4><label for="email">Email*</label></h4>
          <input id="email" type="email" name="message_email" required>
          <span class="validation">Mejor si el email es el tuyo, gracias</span>
        </div>

        <div class="form-group">
          <h4><label for="subject">Asunto*</label></h4>
          <input id="subject" type="text" name="message_subject" required>
          <span class="validation">Necesitamos saber de que se trata</span>
        </div>

        <div class="form-group">
          <h4><label for="msg">Mensaje*</label></h4>
          <textarea id="msg" name="message_text" rows="10" required></textarea>
          <span class="validation">No lo dejes en blanco, escribe algo!</span>
        </div>

        <div class="form-group" style="display: none;">
          <!-- the field named address is used as a spam honeypot --> 
          <!-- it is hidden from users, and it must be left blank -->
          <h4><label for="address">Address</label></h4>
          <input type="text" name="message_address" id="address">
          <p>Humans: please leave this field blank.</p>     
        </div>

        <input id="submit" name="message_submit" type="submit" ></input>
        <input type="hidden" name="save" value="contact">

        <div id="form-messages"></div>

      </form>

Thanks Hugo for your help!!!

Hugo Paz
Hugo Paz
15,620 Points

You can try using header("Location: index.php");

maze
maze
17,027 Points

Thanks Hugo, but I don't want to redirect to index.php, I want to remain in the same custom-page contact form, maybe the title is a little confusing, sorry.

In the form's attribute "action" I have this code:

<?php the_permalink(); ?>

Hugo Paz
Hugo Paz
15,620 Points

So you want to remain on the form page?

maze
maze
17,027 Points

Yes!

Hugo Paz
Hugo Paz
15,620 Points

When you submit the form, is there a php file that process it?

maze
maze
17,027 Points

Is the same page that process it

Hugo Paz
Hugo Paz
15,620 Points

i see. Can you post the code here?

maze
maze
17,027 Points
<?php
//Setting the Content type. Basically telling the browser what type of content it is dealing with.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // get the posted data
        $name = trim($_POST["name"]);
        $email_address = trim($_POST["email"]);
        $subject = trim($_POST["subject"]);
        $message = trim($_POST["msg"]);

        // check that a name was entered
        if (empty ($name))
                $error = "You must enter your name.";
        // check that an email address was entered
        elseif (empty ($email_address)) 
                $error = "You must enter your email address.";
        // check for a valid email address
        elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email_address))
                $error = "You must enter a valid email address.";
        // check that a subject was entered
        elseif (empty ($subject))
                $error = "You must enter a subject.";
        // check that a message was entered
        elseif (empty ($message))
                $error = "You must enter a message.";
        // the field named address is used as a spam honeypot
        // it is hidden from users, and it must be left blank
        elseif (!isset($error) && $_POST["address"] != "") 
                $error = "Your form submission has an error.";
        // this code checks for malicious code attempting
        // to inject values into the email header
        elseif (!isset($error)) {
                foreach( $_POST as $value ){
                    if( stripos($value,'Content-Type:') !== FALSE ){
                        $error = "There was a problem with the information you entered.";
                    }
                }
        }       

        // check if an error was found - if there was, send the user back to the form
        if (isset($error)) {
             // Set a 400 (bad request) response code and exit.
             http_response_code(400);
             echo '<span class="icon-sad"></span> Oops! Hubo un problema con el envío. Por favor, completa el formulario y vuelve a intentarlo.';
             exit;
        }

        // Set the recipient email address.
        // FIXME: Update this to your desired email address.
        $recipient = "prueba@prueba.com";

        // Build the email content.
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email_address\n";
        $email_content .= "Message:\n\n$message";

         // Build the email headers.
     $email_headers = "From: $name <$email_address>";

        // Send the email.
        if (mail($recipient, $subject, $email_content, $email_headers)) {
                // Set a 200 (okay) response code.
                http_response_code(200);
                echo '<span class="icon-happy"></span> Gracias! Su mensaje ha sido enviado.';
        } else {
                // Set a 500 (internal server error) response code.
                http_response_code(500);
                echo '<span class="icon-sad"></span> Oops! Algo fué mal y no se pudo enviar tu mensaje.';
        }
} 
?>


<?php get_header(); ?>
</div> <!-- Header container tag -->

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php the_content(); ?> 

  <div class="page-section hide-mobile">
    <h1 class="title"> <?php the_title(); ?></h1>
    <h3>Envianos un correo electrónico, nos encantaría saber de ti</h3>
  </div>

  <div class="container contact-form">
    <div class="grid-8 form">
      <form id="eform" role="form" method="POST" action="<?php the_permalink(); ?>">
        <h3>Todos los campos con un * son obligatorios.</h3>
        <div class="form-group">
          <h4><label for="name">Nombre*</label></h4>
          <input id="name" type="text" name="name" required>
          <span class="validation">Por favor introduce tu nombre</span>
        </div> 

        <div class="form-group">
          <h4><label for="email">Email*</label></h4>
          <input id="email" type="email" name="email" required>
          <span class="validation">Mejor si el email es el tuyo, gracias</span>
        </div>

        <div class="form-group">
          <h4><label for="subject">Asunto*</label></h4>
          <input id="subject" type="text" name="subject" required>
          <span class="validation">Necesitamos saber de que se trata</span>
        </div>

        <div class="form-group">
          <h4><label for="msg">Mensaje*</label></h4>
          <textarea id="msg" name="msg" rows="10" required></textarea>
          <span class="validation">No lo dejes en blanco, escribe algo!</span>
        </div>

        <div class="form-group" style="display: none;">
          <!-- the field named address is used as a spam honeypot --> 
          <!-- it is hidden from users, and it must be left blank -->
          <h4><label for="address">Address</label></h4>
          <input type="text" name="address" id="address">
          <p>Humans: please leave this field blank.</p>     
        </div>

        <input id="submit" name="submit" type="submit" ></input>
        <input type="hidden" name="save" value="contact">

        <div id="form-messages"></div>

      </form>
<?php endwhile; endif;?>

<?php get_footer(); ?>
maze
maze
17,027 Points

I've noticed that after submit says "Page not found" and maybe is the reason that goes to index.php file, but why?

Hugo Paz
Hugo Paz
15,620 Points

Have you tried substituting the <?php the_permalink(); ?>" with the page url just to test it?

maze
maze
17,027 Points

Yes I did, also I tried without the form's attribute "action" but the same problem

Hugo Paz
Hugo Paz
15,620 Points

Must be something in wordpress. Maybe pop on their forums for help. Sorry i cant help you with this. I created a custom form in wordpress before and did not have this issue.

maze
maze
17,027 Points

Don't care, thanks a lot!!! :)