Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Why does my contact form redirect?

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #181898
    Keenan
    Participant

    Hi! (And thank you in advance for your help!)

    I am trying to put a few finishing touches on a website I have been working on for a while and need a bit of help. For some reason, my contact form (which I am using from a template) will always redirect to a new page once submitted. I have a very limited knowledge of PHP but I cannot see any reason why this is happening.

    Please help!
    Keenan

    `
    <?php
    /* Set e-mail recipient */
    $myemail = “***@gmail.com”;

    /* Check all form inputs using check_input function */
    $name = check_input($_POST[‘inputName’], “Your Name”);
    $email = check_input($_POST[‘inputEmail’], “Your E-mail Address”);
    $subject = check_input($_POST[‘inputSubject’], “Message Subject”);
    $message = check_input($_POST[‘inputMessage’], “Your Message”);

    /* If e-mail is not valid show error message /
    if (!preg_match(“/([\w-]+\@[\w-]+.[\w-]+)/”, $email))
    {
    show_error(“Invalid e-mail address”);
    }
    /
    Let’s prepare the message for the e-mail */

    $subject = “$name has sent you a message”;

    $header = “From: “. “$username” . ” <” . $email . “>\r\n”;

    $message = ”

    Name: $name
    Email: $email

    Message:
    $message

    “;

    /* Send the message using mail() function */
    mail($myemail, $subject, $message, $header);

    /* Functions we used */
    function check_input($data, $problem=”)
    {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
    show_error($problem);
    }
    return $data;
    }

    function show_error($myError)
    {
    ?>
    <html>
    <body>

    <p>Please correct the following error:</p>
    <strong><?php echo $myError; ?></strong>
    <p>Hit the back button and try again</p>

    </body>
    </html>
    <?php
    exit();
    }
    ?>

    `

    #181900
    __
    Participant

    What page is it redirecting to?

    Is this the full code? Also, you might want to use a service like pastebin or make a gist on github to share large amounts of code. It’s easier to read that way.

    #181988
    Keenan
    Participant

    Thank you for your response! Sorry that it took me so long to get back to you. Thank you for the pastebin tip as well! My code can be found at http://pastebin.com/zLEg3rrj

    I posted the PHP for the contact form and the form coding. Everything works great except it is redirecting to php/mailer.php.

    I assume it has something to do with the line in the form that says “action”?

    #181989
    chrisburton
    Participant

    I assume it has something to do with the line in the form that says “action”?

    Yep! You could use AJAX.

    #181990
    Keenan
    Participant

    I am not sure what you mean by that.

    #181992
    __
    Participant

    Everything works great except it is redirecting to php/mailer.php.

    Well, that’s not really what is commonly thought of as a “redirect.” The form is just submitting to the action url (php/mailer.php), which is entirely normal. That’s how forms work.

    AJAX is a method which allows the browser to make a request to your server (like a form submission) and receive the response without reloading the page, which is what it sounds like you want.

    #181993
    Keenan
    Participant

    Thanks a lot! I will look into AJAX and see what I can come up with!

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.