Jump to content

Redirect url after contact form submission and include function


Megan

Recommended Posts

I have created the below code for my website www.pearsonkoutcherlaw.com:

<?php
                if (isset($_POST['formSubmit'])) {
        header("location: http://www.pearsonkoutcherlaw.com/abl-form-submit/"); exit;
                    print "<p style=\"font-weight:bold;\">Form has been sent!</p>";
                    print '<p>Thank you for your submission. </p>';
                    print "<p>What You Sent</p>";

                    print "<b>Name:</b><br/>".$name."<br/><br/>";
                    print "<b>Email:</b><br/>".$email."<br/><br/>";
                    print "<b>Phone:</b><br/>".$phone."<br/><br/>";
                    print "<b>Message:</b><br/>".$message."<br/><br/>";

                } else {      ?>``

So, after submission, the user is redirected to http://www.pearsonkoutcherlaw.com/abl-form-submit/. However, I also want the print functions, "Form has been sent", "Thank you for your submission." to appear on the redirect page?

How do I do that?

Thanks,

Megan

Link to comment
Share on other sites

Thank you for your reply, but i am very new at this and do not think that I really understand?  I replaced, $_POST with $_SESSION and now when I fill out the contact form, instead of redirecting, it just refreshed to the original URL?  Could you please explain what you mean by "more those vars"  Thank you for your time. 

Link to comment
Share on other sites

Thank you for your reply, but i am very new at this and do not think that I really understand?  I replaced, $_POST with $_SESSION and now when I fill out the contact form, instead of redirecting, it just refreshed to the original URL?  Could you please explain what you mean by "more those vars"  Thank you for your time. 

 

If you prefer to stick with using a header() redirect, you could save the form information into SESSION variables as ginerjm suggested.

<?php
if (isset($_POST['formSubmit'])) {
    //START A SESSION
    session_start();
 
    //CREATE SESSION VARIABLES
    $_SESSION['name'] = $name;
    //...
 
    //REDIRECT VISITOR
    header("location: http://www.pearsonkoutcherlaw.com/abl-form-submit/");
    exit;
} 
?>
 
Then on the confirmation page, you would just need to open the session again to get the variables.
<?php
//START A SESSION
session_start();
 
//DISPLAY CONFIRMATION
print "<p style=\"font-weight:bold;\">Form has been sent!</p>";
print '<p>Thank you for your submission. </p>';
print "<p>What You Sent</p>";
 
print "<b>Name:</b><br/>" . htmlspecialchars($_SESSION['name']) ."<br/><br/>";
//...
?>

 

More information about sessions can be found in the manual:

http://php.net/manual/en/session.examples.basic.php

Link to comment
Share on other sites

Thank you for the information and the article.  It was very helpful.  Unfortuately, I have played around with this for a few days and am still getting an error using the code below.  Any advise? 

<?php
if (isset($_POST['formSubmit'])) {
    //START A SESSION
    session_start();
 
    //CREATE SESSION VARIABLES
    $_SESSION['name'] = $name;
    $_SESSION['email'] = $email;
    $_SESSION['phone'] = $phone;
    $_SESSION['message'] = $message;
 //
    //REDIRECT VISITOR
    header("location: http://www.pearsonkoutcherlaw.com/abl-form-submit/");
    exit;
} 
?>

Thank you. 

Link to comment
Share on other sites

You really need to do some reading to learn a little bit about php and html processing.  Your code shows that you have no clue.

 

Your code shows you placing some values into some session vars.  Can I ask you where those values are coming from?  Do you know?  To us, looking at your script sample, those are undefined values.  If you php error checking turned on you would be seeing error messages.

Link to comment
Share on other sites

As ginerjm suggested, the variables like $name are probably undefined. Since these variables seems to be coming from a form using the POST method, I would imagine this

$_SESSION['name'] = $name;

Should be this

$_SESSION['name'] = $_POST['name'];

Note that I recommend taking this one step at a time. It makes debugging easier. Before worrying about the header() redirect and the page which processes the form information, I would make sure the SESSION variables are getting the values they need.

 

Try commenting out the header() function and just output the SESSION variables. If they contain what you need, you can move to the next step.

Edited by cyberRobot
Link to comment
Share on other sites

Normally you'd put session_start(); at the very top of your script, on every page.

 

Just to clarify, it only needs to be added to pages which use SESSION variables. While I agree that session_start() should usually be at the top of the script, this case may be an exception.

 

This script appears to have at least two stages. One to show the form and one that's activated after the form is submitted. Assuming that the SESSION variables are only used in the latter stage, the session doesn't need to be opened until the form is submitted.

 

With that said, session_start() needs to be called before outputting anything. More information can be found here:

http://php.net/manual/en/function.session-start.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.