redirect after form submit - help please!

7 replies
Hey all...

I'm pretty decent at figuring out stuff, but this made me slightly annoyed earlier
this evening.

As the post headline states, I want to redirect to a custom page after a visitor
submits an email form. The way it initially, and still, works is to open a browser
window with only, "Thank you for your message." displayed. (Pretty lame and
embarrassing.)

Here are the details:

1. Simple html form on an html page with contact.php on server.

2. Googled all over for relevant info. Found lots of it, tried quite a few things to
no avail. Some of it was over me hurting head.

3. Grabbed a php redirect script. Here's a laff... wasn't sure WHERE it went. I
perhaps stupidly assumed it went on the contact.php page.

4. Kept getting syntax/format errors for this line:

header('Location: http://website.com');

5. Tried using a js code in the html page header area and
changing the form code from 'submit' to jump. Like this...

<input type="submit" onClick="jump()">

Header area code:

<script language="javascript">
function jump()
{

window.location = 'http://www.website.com';
}

</script>
</head>
<body>

Well... that didn't work.

So ok, I've read about putting the php header redirect before
any outputs, echo commands, etc.

I'm aware about avoiding blank lines and <? being on the first line
of the page.

Here's the original contact.php code:

<?
$subject="from ".$_GET['your_name'];
$headers= "From: ".$_GET['your_email']."\n";
$headers.='Content-type: text/html; charset=iso-8859-1';
mail("blahblah@gmail.com", $subject, "
<html>
<head>
<title></title>
</head>
<body>
<br>
".$_GET['message']."
</body>
</html>" , $headers);
echo ("Your message was successfully sent! ");
?>
<script>
resizeTo(800, 600)
//window.close()
</script>

Here's the php script I was trying to make work:

<?php
/* Redirect browser */
header("Location: http://blahblah.com");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Oh...

I also tried a meta refresh with 0 seconds in the stupid blank page
that tells people their message was sent.

Pretty sure I had that in the right place, but no worky. It laffed at
me and pointed it's finger whilst laffing.

I pains me to admit I'm redirect challenged.

Can anyone help?

I'm going to bed, now.

Thank you, and good night.
#form #redirect #submit
  • Profile picture of the author lisag
    I can never get the BB tags for code to work in my posts. Very frustrating. Anyway, check the attached file for the solution to your woes..
    Signature

    -- Lisa G

    {{ DiscussionBoard.errors[1558987].message }}
  • Profile picture of the author KenThompson
    Lisa,

    Thanks very much for the help, but I got some errors when
    I tried it.

    If you don't mind, may I have your help troubleshooting this?

    Ok, first I copy and pasted your code into my contact.php. Of
    course I edited for correct email address and url.

    Here's what was returned:

    Warning: mail() expects at least 3 parameters, 2 given in /home/*******/public_html/contact.php on line 5

    another testYour message was successfully sent!
    Warning: Cannot modify header information - headers already sent by (output started at /home/******/public_html/contact.php:5) in /home/******/public_html/contact.php on line 17

    Here's the contact.php file: (<?PHP is line 1 at the very top)

    <?PHP
    $subject="from ".$_GET['your_name'];
    $headers= "From: ".$_GET['your_email']."\n";
    $headers.='Content-type: text/html; charset=iso-8859-1';
    mail("*******@gmail.com", $subject) // <--< write this function correctly
    ?>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <br>
    <?PHP
    ob_start();
    echo $_GET['message'];
    echo ("Your message was successfully sent! ");
    header("Location: http://www.******.com/*****.html");
    ob_flush();
    exit;
    ?>
    </body>
    </html>

    I don't quite understand the first error. There are only 3 fields
    in the contact form: name, email, message. I entered info in
    all 3 fields. So I'm not sure I understand the thing about a
    field entry missing. Of course that assumes I understand what
    it's referencing.

    I've read about the issue with headers being sent prior to
    redirecting. But I don't know how to address that problem.

    So, what do you think?

    Thank you.

    Edit: Does the header redirect need to be before 'echo' commands?

    echo $_GET['message'];
    echo ("Your message was successfully sent! ");
    header("Location: http://www.******.com/*****.html");
    {{ DiscussionBoard.errors[1560056].message }}
    • Profile picture of the author lisag
      Actually, I only addressed the header issue. I should have addressed the entire logic of the script, but then I would have had to rewrite it for you and you wouldn't learn anything :-)

      There is a fundamental flaw in your logic. You want to display the message content and the "Thank you, your message has been sent" message and then do a redirect. The main logic issue is that neither the content or the message will be seen because (once you get the script working), the redirect will occur too fast for the message to be seen.
      this:

      1. Send the mail.
      2. Display the content
      3. Display the "Thank You...."
      4. Provide a link for them to click

      Try the attached file.

      And would someone tell me why I cant seem to use the code or php bb tags without it throwing an error when I preview the post???

      There may be some syntax errors; I didn't test the code. But they should be easy to whack. let me know...
      Signature

      -- Lisa G

      {{ DiscussionBoard.errors[1560234].message }}
  • Profile picture of the author lisag
    Ken,

    I had posted a comment in the example code saying you needed to fix the mail function code. I thought you had just threw in some sample stuff there.

    You have the headers variable wrong and the format. read this article and get mail working first. Once you have that working, get back to me and we'll chase down while the ob_start isn't giving you the expected result.

    How to Send Email from a PHP Script - About Email
    Signature

    -- Lisa G

    {{ DiscussionBoard.errors[1560103].message }}
    • Profile picture of the author KenThompson
      Originally Posted by lisag View Post

      Ken,

      I had posted a comment in the example code saying you needed to fix the mail function code. I thought you had just threw in some sample stuff there.

      You have the headers variable wrong and the format. read this article and get mail working first. Once you have that working, get back to me and we'll chase down while the ob_start isn't giving you the expected result.

      How to Send Email from a PHP Script - About Email
      Oh. lol.

      Sorry about that. I thought it was just a warning to not screw up
      the code. I guess it was, but I didn't understand it was a marker
      that it was not coded/formatted correctly.

      Will do as suggested and thank you.
      {{ DiscussionBoard.errors[1560123].message }}
  • Profile picture of the author KenThompson
    Lisa...

    Programming success!

    I fixed the email issues, works fine.

    As for the second error about headers already been sent. I
    moved the 'ob start' command below the 'header(email... )
    bit and above the <html> command.

    Original:

    $headers.='Content-type: text/html; charset=iso-8859-1';
    mail("*******@gmail.com", $subject) // <--< write this function correctly
    ?>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <br>
    <?PHP
    ob_start();
    echo $_GET['message'];
    echo ("Your message was successfully sent! ");
    header("Location: http://www.******.com/*****.html");
    ob_flush();


    Final version that works:

    $headers.='Content-type: text/html; charset=iso-8859-1';
    mail("*******@gmail.com", $subject); (this line is fixed.)
    ob_start()
    ?>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <br>
    <?PHP
    echo $_GET['message'];
    echo ("Your message was successfully sent! ");
    header("Location: http://www.******.com/*****.html");
    ob_flush();

    I read about the 'ob' command and was able to better
    understand what to try.

    And it works like a charm! Redirects after hitting submit,
    nothing flashes and redirects, and it goes to the correct
    page.

    So...

    Thank you very much for all your help. I really appreciate it.

    PS - Happy New Year!
    {{ DiscussionBoard.errors[1560396].message }}
  • Profile picture of the author lisag
    Excellent!
    Signature

    -- Lisa G

    {{ DiscussionBoard.errors[1560623].message }}

Trending Topics