Forums

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

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

  • This topic is empty.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #33278
    rubistarr
    Member

    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:

    
    if(isset($_POST) && empty($_POST))
    {
    require 'email-validator.php';
    $validator = new EmailAddressValidator();

    $errors = array();

    $input_email = strip_tags($_POST);
    $input_subject = strip_tags($_POST);
    $input_message = strip_tags($_POST);

    $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('[email protected]', "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('
    ', $errors);
    }
    }
    else
    {
    die('You cannot access this page directly.');
    }

    here is what I tried:

    if(empty($errors))
    {
    if(mail('[email protected]', "Message from $input_name - $input_subject", $input_message, "From: $input_email"))
    header("Location: http://url....../thankyou-2"); /* Redirect browser */

    else
    {
    echo 'There was an issue when sending your email. Please try again.';
    }
    }
    else
    {
    echo implode('
    ', $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

    #82374
    ddliu
    Member

    There should be an “exit();” after your redirect.

    Read more on php.net

    #82382
    rubistarr
    Member

    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.

    #82385
    ddliu
    Member

    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.

    #82414
    rubistarr
    Member

    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;
    });

    });
    #82418
    rubistarr
    Member

    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

    #82435
    rubistarr
    Member

    still need some advice on this one. please.

    #82416
    rubistarr
    Member

    where would I find what data equals when the form is submitted successfully. Wouldnt it be in the code I posted earlier?

    #82413
    rubistarr
    Member

    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/"
    }
    else
    {
    response_text.html(data);
    }

    });
    return false;
    });

    });

    sigh. thanks for trying to help.

    #82461
    rubistarr
    Member

    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/"
    }
    else

    and

    if(empty($errors))
    {
    if(mail('[email protected]', "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?

    #82462
    rubistarr
    Member

    hidden field is the missing word above

    #82476
    rubistarr
    Member

    still not solved. anyone?

    #82521
    rubistarr
    Member

    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 :)

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