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

redirect to thankyou page with self posting php email form. Pretty please :)

  • Form post to self. I like the error messages posting to the same page as the form but would love to redirect to a separate thankyou page when successfully submitted. Any help would be so wonderful.

    here is the code:

    <?php
    if(isset($_POST['name']) && empty($_POST['spam_check']))
    {
    require 'email-validator.php';
    $validator = new EmailAddressValidator();

    $errors = array();

    $input_email = strip_tags($_POST['email']);
    $input_subject = strip_tags($_POST['subject']);
    $input_message = strip_tags($_POST['message']);

    $required = array('Name field' => 'name', 'Email field' => 'email', 'Message field' => 'message');

    foreach($required as $key=>$value)
    {
    if(isset($_POST[$value]) && $_POST[$value] !== '')
    {
    continue;
    }
    else {
    $errors[] = $key . ' cannot be left blank';
    }
    }

    if (!$validator->check_email_address($input_email)) {
    $errors[] = 'Enter a valid email address.';
    }

    if(empty($errors))
    {
    if(mail('X@x.com', "Message from $input_name - $input_subject", $input_message, "From: $input_email"))
    {
    echo 'Thank You - your email has been sent.';
    }
    else
    {
    echo 'There was an issue when sending your email. Please try again.';
    }
    }
    else
    {
    echo implode('<br />', $errors);
    }
    }
    else
    {
    die('You cannot access this page directly.');
    }

    here is what I tried:

    if(empty($errors))
    {
    if(mail('X@x.com', "Message from $input_name - $input_subject", $input_message, "From: $input_email"))
    header("Location: http://url....../thankyou-2&quot;); /* Redirect browser */

    else
    {
    echo 'There was an issue when sending your email. Please try again.';
    }
    }
    else
    {
    echo implode('<br />', $errors);
    }
    }

    Any advice for how to get this working would be oh so great.

    check out the form at http://getgraphixed.com/RSD_new/contact
  • There should be an "exit();" after your redirect.

    Read more on php.net
  • thankyou. I put the exit(); into place but alas it did not solve the dilema. Basically, when I hit send the form sends perfectly but as is I am getting a blank spot above the form where the "thankyou message sent" should be instead of redirecting to a new page. You can see what I mean if you go to the contact page link above and send a message.

    If I have to, I could settle for having the thankyou message post to the same page but need the form to clear at least. This is not ideal but could do if I cant solve the above.

    Any help is greatly appreciated.
  • Sorry I can't open the webpage before.

    You are using Ajax to send message? Then PHP redirect won't work, you have to redirect client side using Javascript.

  • I am new to the world of php java and Ajaxing so if you could give me a hint as to how to use the java script to redirect that would be great. To be honest, I am just starting to figure out php and am not really sure if I am using ajax or not. There are three files in a scripts folder that work the contact form. One is a js form with the below code and the other two are php files and then there is the main contact php page that actually shows the form.

    $(function(){
    $('#form_submit').click(function(){

    var input_name = $('#form_name').val(),
    input_email = $('#form_email').val(),
    input_subject = $('#form_subject').val(),
    input_message = $('#form_message').val(),
    response_text = $('#response');

    response_text.hide();
    response_text.html('Loading...').show();

    $.post('http://theme/scripts/contact-process.php', {name: input_name, email: input_email, subject: input_subject, message: input_message}, function(data){
    response_text.html(data);
    });
    return false;
    });

    });
  • Yeah, the $.post is an AJAX call to send the form data to the browser. Where the response_text.html(data) is, this is your callback function which executes when you get a response from the page contact-process.php. You would need to add something in this function to check the response and if it is valid then load a new page
  • any chance you could give me the actual code to add and where to add it exactly. Thanks so much for helping. It is really appreciated. I have put about 10 hrs of time into this and have learned so much about php and forms but I still have so much to learn :P
  • still need some advice on this one. please.
  • I've removed your other post as it's getting a bit confusing running two asking the same question.

    It's difficult to give you the exact code that works as I don't know what the variable data is set to if the form submission was successful. However it will be something like the following. Replace your post with this, and work out what data equals when the form is submitted successfully


    $.post('http://theme/scripts/contact-process.php', {name: input_name, email: input_email, subject: input_subject, message: input_message}, function(data){
    if ( data === 'someText' )
    {
    window.location = "http://www.google.com/&quot;
    }
    else
    {
    response_text.html(data);
    }

    });

  • where would I find what data equals when the form is submitted successfully. Wouldnt it be in the code I posted earlier?
  • well, i think I may be about to admit that I have been defeated. I have been trying to figure this out for about 2 days now and just cant seem to get it working. here is my new code with a random guess of what to put in the data spot.
    $(function(){
    $('#form_submit').click(function(){

    var input_name = $('#form_name').val(),
    input_email = $('#form_email').val(),
    input_subject = $('#form_subject').val(),
    input_message = $('#form_message').val(),
    response_text = $('#response');

    response_text.hide();
    response_text.html('Loading...').show();


    $.post('http://getgraphixed.com/RSD_new/wp-content/themes/fullscreen_child/scripts/contact-process.php', {name: input_name, email: input_email, subject: input_subject, message: input_message}, function(data){
    if ( data === '#form_submit' )
    {
    window.location = "http://www.google.com/&quot;
    }
    else
    {
    response_text.html(data);
    }

    });
    return false;
    });

    });



    sigh. thanks for trying to help.
  • Ah yes, well think about what the form is doing. You're just guessing and that is going to get you nowhere because you won't learn anything. If successful, data *should* equal 'Thank You - your email has been sent.' You can change that to echo something shorter like 'Success' as it's easier to get a match against this.

    So you'd have


    if(empty($errors))
    {
    if(mail('X@x.com', "Message from $input_name - $input_subject", $input_message, "From: $input_email"))
    {
    echo 'Success';
    }
    else
    {
    echo 'There was an issue when sending your email. Please try again.';
    }
    }


    and

    $.post('http://getgraphixed.com/RSD_new/wp-content/themes/fullscreen_child/scripts/contact-process.php', {name: input_name, email: input_email, subject: input_subject, message: input_message}, function(data){
    if ( data === 'Success' )
    {
    window.location = "http://www.google.com/&quot;
    }
    else
    {
    response_text.html(data);
    }
  • sadly, that just makes the form say the word success above the form when it is successfully sent. Do you think it is something where the code above is overriding the code below? so the form will not redirect to the separate page? I agree guessing is not the best approach but I combine it with lots of internet research and then try things in the hopes it will work. It has gotten me this far but It appears I may have met my match :P


    $.post('http://getgraphixed.com/RSD_new/wp-content/themes/fullscreen_child/scripts/contact-process.php', {name: input_name, email: input_email, subject: input_subject, message: input_message}, function(data){
    if ( data === 'success' )
    {
    window.location = "http://www.google.com/&quot;
    }
    else


    and

    if(empty($errors))
    {
    if(mail('x@x.com', "Message from $input_name - $input_subject", $input_message, "From: $input_email"))
    {
    echo 'success';
    }
    else
    {
    echo 'There was an issue when sending your email. Please try again.';
    }


    Do I need some sort of on the actual contact form page perhaps?
  • hidden field is the missing word above
  • still not solved. anyone?
  • I suggest you drop the AJAX form submissions and all JavaScript from the page and just post the form off to the contact-process.php page. When you can get that working, then add in some JavaScript validation. If you're redirecting after posting an AJAX form then you may as well not bother with the AJAX!
  • hmm, but wont that defeat the point of having the error messages be on the same page as the form? I already can get it to work with the error messages and thankyou messages being on a new page. The challenge is to get it working with the errors on the same page and the thankyou on a new page. It can be done right :)