treehouse : what would you like to learn today?
Web Design Web Development iOS Development

PHP form redirect

  • I am using Chris's simple form on a wordpress site for one of my clients. Normally in the contactengine.php file I just put a file like index.html for the redirect. For the wordpress site I cannot do that. I have tried putting in the directory (ie, /contact/) as well as the full url, but after a form is submitted wordpress pulls up a 404 Not Found page.

    Here is the contactengine.php code:

    <?php

    $EmailFrom = \"site@trading-strategy.com\";
    $EmailTo = \"jdtrader21@yahoo.com\";
    $Subject = \"Some one has signed up for your newsletter\";
    $Email = Trim(stripslashes($_POST['Email']));

    // validation
    $validationOK=true;
    if (!$validationOK) {
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">\";
    exit;
    }

    // prepare email body text
    $Body .= \"Email: \";
    $Body .= $Email;
    $Body .= \"\n\";

    // send email
    $success = mail($EmailTo, $Subject, $Body, \"From: <$EmailFrom>\");

    // redirect to success page
    if ($success){
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://trading-strategy.com/\">\";
    }
    else{
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">\";
    }
    ?>


    Any help would be appreciated, not a PHP wizard. In case you need it the website is trading-strategy.com, and it is currently down right now because said client made a boo-boo.
  • try this..not a php expert either. might work


    <?php

    $EmailFrom = \"site@trading-strategy.com\";
    $EmailTo = \"jdtrader21@yahoo.com\";
    $Subject = \"Some one has signed up for your newsletter\";
    $Email = Trim(stripslashes($_POST['Email']));

    // validation
    $validationOK=true;
    if (!$validationOK) {
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">\";
    exit;
    }

    // prepare email body text
    $Body .= \"Email: \";
    $Body .= $Email;
    $Body .= \"\n\";

    // send email
    $success = mail($EmailTo, $Subject, $Body, \"From: <$EmailFrom>\");

    // redirect to success page
    if ($success){ ?>
    <meta http-equiv=\\"refresh\\" content=\\"0;URL=http://trading-strategy.com/\">
    <?php }
    else { ?>
    <meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">
    <?php }
    ?>

  • What you want to use is:

    // redirect to success page 
    if ($success){
    header(\"location: http://trading-strategy.com/\");
    }
    else{
    header(\"location: error.htm\");
    }
  • This is a little wordpress quirk. Basically, except for the styles, referencing static files in your html doesn't work properly when they reside in the theme directory. So instead, what you have to do is use this template tag
    <?php bloginfo(’template_directory’); ?>
    to denote the root of the theme directory, and then write the rest of your url from there.

    *Edit* I re-read your post and initially misunderstood. Robski is right, you'll want to use header redirects.