Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Php redirect issues


    So here is my code that I have that i am using to make a form to submit to an email address. I need to know how to correct it so that the form redirects to a thank you page. I have tried several times to correct this and can't figure it out. I have next to no experience with php but know this is the only way to do it. So if you could tell me exactly where to put the direct. I am pretty sure it has to be in the header of the php code but i cant even get that to work properly.

    THanks,

    Heres the code.

    PHP Code:

    <?php
    if(isset($_POST['email'])) {

    // CHANGE THE TWO LINES BELOW
    $email_to "email@email.com";

    $email_subject "website html form submissions";


    function 
    died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo 
    "These errors appear below.<br /><br />";
    echo 
    $error."<br /><br />";
    echo 
    "Please go back and fix these errors.<br /><br />";
    die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
    !isset(
    $_POST['last_name']) ||
    !isset(
    $_POST['email']) ||
    !isset(
    $_POST['telephone']) ||
    !isset(
    $_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $first_name $_POST['first_name']; // required
    $last_name $_POST['last_name']; // required
    $email_from $_POST['email']; // required
    $telephone $_POST['telephone']; // not required
    $comments $_POST['comments']; // required

    $error_message "";
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!
    preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    $string_exp "/^[A-Za-z .'-]+$/";
    if(!
    preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }
    if(!
    preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
    }
    if(
    strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }
    if(
    strlen($error_message) > 0) {
    died($error_message);
    }
    $email_message "Form details below.\n\n";

    function 
    clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return 
    str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";


    // create email headers
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers);
    ?>

    <!-- place your own success html below -->

    Thank you for contacting us. We will be in touch with you very soon.

    <?php
    }
    die();
    ?>
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    You can try this:
    PHP Code:
    // create email headers
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers);

    header("Location: some_page.php");
    die(); 
    You may want to check if the mail actually sent or not before sending them to a success page though.

    Hope it helps.

    Regards,

    NM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Thank you


    I am testing it to see if it works. However I do have another quick question. does the location of the file have to be a .php or can it be a .html file?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Okay I'm getting an error message of

    Parse error: syntax error, unexpected '<' in /home/kpwebdes/public_html/test/html_form_send.php on line 75

    Which line 75 is the comment
    <!-- place your own success html below -->

    so I dont know what to do.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    It sounds like you've not closed something properly. Can you post your whole code so we can debug it better.

    That's a HTML comment......is that within your PHP code? If it's inside your PHP code, close the PHP with '?>' and place it in the HTML section of your document.

    Regards,

    NM.
    Last edited by Nanomech; January 22nd, 2013 at 01:03 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    PHP Code:
    <?php
    if(isset($_POST['email'])) {

    // CHANGE THE TWO LINES BELOW
    $email_to "email@email.com";

    $email_subject "website html form submissions";


    function 
    died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo 
    "These errors appear below.<br /><br />";
    echo 
    $error."<br /><br />";
    echo 
    "Please go back and fix these errors.<br /><br />";
    die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
    !isset(
    $_POST['last_name']) ||
    !isset(
    $_POST['email']) ||
    !isset(
    $_POST['telephone']) ||
    !isset(
    $_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $first_name $_POST['first_name']; // required
    $last_name $_POST['last_name']; // required
    $email_from $_POST['email']; // required
    $telephone $_POST['telephone']; // not required
    $comments $_POST['comments']; // required

    $error_message "";
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!
    preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    $string_exp "/^[A-Za-z .'-]+$/";
    if(!
    preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }
    if(!
    preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
    }
    if(
    strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }
    if(
    strlen($error_message) > 0) {
    died($error_message);
    }
    $email_message "Form details below.\n\n";

    function 
    clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return 
    str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";


    // create email headers
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers);

    header("Location: some_page.php");
    die();  

    <!-- 
    place your own success html below -->

    Thank you for contacting usWe will be in touch with you very soon.

    <?
    php
    }
    die();
    ?>
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    Yep, that's the problem. you can't put a HTML comment inside PHP tags - well not directly at least.

    For a one line comment in PHP you can do this:
    PHP Code:
    //place your own success html below 
    Having said that, you are redirecting, so you're best off putting your message:"Thank you for contacting us. We will be in touch with you very soon." inside 'some_page.php' because you will never see that message. Any output after a header() redirect is ignored by the browser.

    Just to clarify also, you can redirect to a PHP or HTML page, it doesn't have to be a file with a .php extension.

    Regards,

    NM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Okay so i changed the address to the webpage i want it redirected to and the email address that i want. then i tested it and got this.

    Parse error: syntax error, unexpected on line 76

    line 76 is this element

    PHP Code:
    <?php
    }
    die();
    ?>
    sometimes i really hate this crap.
  16. #9
  17. Put a potato on it!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    304
    Rep Power
    971
    please remove this post, it was on the wrong thread.
    Last edited by Jyncka; January 22nd, 2013 at 02:01 PM. Reason: Wrong thread
    "Those who can make you believe absurdities can make you commit atrocities."
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Where should I put it then? This is the only php development thread.
  20. #11
  21. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    312
    Originally Posted by kpalmer29852
    Where should I put it then? This is the only php development thread.
    I think they were referring to their post, not this thread.

    Originally Posted by kpalmer29852
    I am testing it to see if it works. However I do have another quick question. does the location of the file have to be a .php or can it be a .html file?
    Yes, it can be whatever you want. It basically issues a 302 redirect header back to the browser, telling it which page to go to instead of this one. You can force it to issue a 301 if you want also.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    I am still getting an error message with this segment of code.

    Code:
     <?php
    }
    die();
    ?>
  24. #13
  25. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    ...and that error message is...?

    The new user guide contains tips on formatting a good question. For instance: we're not sitting with you at your desk. We're over here, where we cannot see your screen.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    I had already posted it hence the short reply sorry.

    Parse error: syntax error, unexpected on line 76

    line 76 is this element

    PHP Code:
    Code:
    <?php
    }
    die();
    ?>
  28. #15
  29. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    unexpected what on line 76?

    I assume it's an unexpected } and you've fixed your other parse errors. That } doesn't have a corresponding {. { and } are used to block off sections of code. This one closes where nothing was opened. You can probably get rid of that whole block, starting from the <?php and going down.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo