How to redirect after form submit?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    #1

    How to redirect after form submit?

    I have a basic feedback form with a submit button.

    After the "send" button is clicked, I want the user to be redirected to a
    different page that says "Your message has been sent."

    How do I do this?

    I've tried using:

    header("Locatio n:http://www.mysite.com/send-confirm.php");

    But here's the error I get:

    Warning: Cannot add header information - headers already sent by (output started
    at ...

    Here's the code:

    <form action= "
    <?php
    $message = trim($_REQUEST['message']);
    $email_address = trim($_REQUEST['email_address']);
    if (empty($message ) || empty($email_ad dress))
    {
    $valid_message = false;
    }
    else
    {
    mail( "my email address", "feedback form", $message, "from:
    $email_address" );
    $valid_message = true;
    }
    ?>"
    <input name="email_add ress" type="text" size="30"/><br />
    <textarea name="message" rows="12" cols="23"></textarea><br />
    <input type="submit" value="Send">
    </form>
    <?php
    if ($valid_message )
    {
    echo "valid message";
    header("Locatio n:http://www.mysite.com/send-confirm.php");
    exit();
    }
    else
    {
    echo "invalid message";
    }
    ?>

    How do I redirect after the user clicks the send button?

    Thanks in advance.


  • Paul Lautman

    #2
    Re: How to redirect after form submit?

    deko wrote:[color=blue]
    > I have a basic feedback form with a submit button.
    >
    > After the "send" button is clicked, I want the user to be redirected
    > to a different page that says "Your message has been sent."
    >
    > How do I do this?
    >
    > I've tried using:
    >
    > header("Locatio n:http://www.mysite.com/send-confirm.php");
    >
    > But here's the error I get:
    >
    > Warning: Cannot add header information - headers already sent by
    > (output started at ...
    >
    > Here's the code:
    >
    > <form action= "
    > <?php
    > $message = trim($_REQUEST['message']);
    > $email_address = trim($_REQUEST['email_address']);
    > if (empty($message ) || empty($email_ad dress))
    > {
    > $valid_message = false;
    > }
    > else
    > {
    > mail( "my email address", "feedback form", $message, "from:
    > $email_address" );
    > $valid_message = true;
    > }[color=green]
    >> "[/color]
    > <input name="email_add ress" type="text" size="30"/><br />
    > <textarea name="message" rows="12" cols="23"></textarea><br />
    > <input type="submit" value="Send">
    > </form>
    > <?php
    > if ($valid_message )
    > {
    > echo "valid message";
    > header("Locatio n:http://www.mysite.com/send-confirm.php");
    > exit();
    > }
    > else
    > {
    > echo "invalid message";
    > }[color=green]
    >>[/color]
    >
    > How do I redirect after the user clicks the send button?
    >
    > Thanks in advance.[/color]

    Put all the stuff that does output ("<form...", "<input..." ) after the
    header() call.


    Comment

    • deko

      #3
      Re: How to redirect after form submit?

      > Put all the stuff that does output ("<form...", "<input..." ) after the[color=blue]
      > header() call.[/color]

      Okay, here's the header call:

      <?php if ($valid_message )
      {
      header("Locatio n:http://www.mysite.com/send-confirm.php");
      exit();
      }
      ?>

      And here's the form:

      <form action="
      <?php
      $message = trim($_REQUEST['message']);
      $email_address = trim($_REQUEST['email_address']);
      if (empty($message ) || empty($email_ad dress))
      {
      $valid_message = false;
      }
      else
      {
      mail( "my email address", "feedback form", $message, "from:
      $email_address" );
      $valid_message = true;
      }
      ?>" method="POST" name="feedback" style="font-size:12px" >
      <input name="email_add ress" type="text" size="30"/><br />
      <textarea name="message" rows="12" cols="23"></textarea><br />
      <input type="submit" value="Send">
      </form>

      I no longer get the error, but the page does not redirect.

      I'm sure I must be missing something simple...


      Comment

      • deko

        #4
        Re: How to redirect after form submit?

        I tried this:

        <?php

        $valid_message = true;

        if ($valid_message )
        {
        header("Locatio n:http://bubba/send-confirm.php");
        exit();
        }
        ?>

        And still got this:

        Warning: Cannot add header information - headers already sent by (output started
        ....

        Can I redirect another way?

        The headers, probably, are being sent in the header section of the page.

        Can this block of code be put above the DOCTYPE statement?

        But then, how would the value of the $valid_message variable get set?


        Comment

        • Paul Lautman

          #5
          Re: How to redirect after form submit?

          You must ensure that there is no blank space or empty lines, outside of the
          <?php ?> braces. Any such empty space will be sent and will cause headers to
          already be sent.

          Make sure that <?php is the very first thing on the very first line of the
          file.

          deko wrote:[color=blue]
          > I tried this:
          >
          > <?php
          >
          > $valid_message = true;
          >
          > if ($valid_message )
          > {
          > header("Locatio n:http://bubba/send-confirm.php");
          > exit();
          > }[color=green]
          >>[/color]
          >
          > And still got this:
          >
          > Warning: Cannot add header information - headers already sent by
          > (output started ...
          >
          > Can I redirect another way?
          >
          > The headers, probably, are being sent in the header section of the
          > page.
          > Can this block of code be put above the DOCTYPE statement?
          >
          > But then, how would the value of the $valid_message variable get set?[/color]



          Comment

          • deko

            #6
            Re: How to redirect after form submit?

            > You must ensure that there is no blank space or empty lines, outside of the[color=blue]
            > <?php ?> braces. Any such empty space will be sent and will cause headers to
            > already be sent.
            >
            > Make sure that <?php is the very first thing on the very first line of the
            > file.[/color]

            but what about all the html?

            the page looks like this:

            DOCTYPE ...
            <html>
            <head>
            ....
            javascript...
            </head>
            <body>
            <html>
            all kinds of html...
            <form>
            <?php
            ....
            ?>
            ....
            </form>
            more html...
            </html>
            </body>

            I want the redirect to happen only if the form is filled out correctly, so the
            redirect should, I think, go just after the form code.


            Comment

            • David Haynes

              #7
              Re: How to redirect after form submit?

              deko wrote:[color=blue][color=green]
              >> You must ensure that there is no blank space or empty lines, outside
              >> of the <?php ?> braces. Any such empty space will be sent and will
              >> cause headers to already be sent.
              >>
              >> Make sure that <?php is the very first thing on the very first line of
              >> the file.[/color]
              >
              > but what about all the html?
              >
              > the page looks like this:
              >
              > DOCTYPE ...
              > <html>
              > <head>
              > ...
              > javascript...
              > </head>
              > <body>
              > <html>
              > all kinds of html...
              > <form>
              > <?php
              > ...
              > ?>
              > ...
              > </form>
              > more html...
              > </html>
              > </body>
              >
              > I want the redirect to happen only if the form is filled out correctly,
              > so the redirect should, I think, go just after the form code.
              >
              >[/color]
              The form processing should be the *first* thing in the file.
              Something like:

              DOCTYPE ...
              <?php
              if( isset($_POST['foo'] ) {
              // do form processing

              if( $isValid ) {
              header();
              exit;
              }
              }
              ?>
              <form action="..." method="POST">
              ....
              <input type="text" name="foo" size="10" maxlength="255" >
              <input type="submit">
              </form>

              -david-

              Comment

              • Paul Lautman

                #8
                Re: How to redirect after form submit?

                deko wrote:[color=blue][color=green]
                >> You must ensure that there is no blank space or empty lines, outside
                >> of the <?php ?> braces. Any such empty space will be sent and will
                >> cause headers to already be sent.
                >>
                >> Make sure that <?php is the very first thing on the very first line
                >> of the file.[/color]
                >
                > but what about all the html?[/color]
                The html will be in the page that it is redirected to.

                If you are redirecting there is no need to send any html for THIS page
                because you are redirecting to a different page.

                [color=blue]
                >
                > the page looks like this:
                >
                > DOCTYPE ...
                > <html>
                > <head>
                > ...
                > javascript...
                > </head>
                > <body>
                > <html>
                > all kinds of html...
                > <form>
                > <?php
                > ...[color=green]
                >>[/color]
                > ...
                > </form>
                > more html...
                > </html>
                > </body>
                >
                > I want the redirect to happen only if the form is filled out
                > correctly, so the redirect should, I think, go just after the form
                > code.[/color]



                Comment

                • deko

                  #9
                  Re: How to redirect after form submit?

                  > The form processing should be the *first* thing in the file.[color=blue]
                  > Something like:
                  >
                  > DOCTYPE ...
                  > <?php
                  > if( isset($_POST['foo'] ) {
                  > // do form processing
                  >
                  > if( $isValid ) {
                  > header();
                  > exit;
                  > }
                  > }
                  > ?>
                  > <form action="..." method="POST">
                  > ...
                  > <input type="text" name="foo" size="10" maxlength="255" >
                  > <input type="submit">
                  > </form>[/color]

                  Thanks, that helped. I am getting the correct redirect behavior now.

                  But I still have a couple of questions...

                  Here's how it looks now:

                  ------------------------
                  <?php
                  $message = $_POST['message'];
                  $email = $_POST['email_address'];
                  if (!empty($messag e) && !empty($email))
                  {
                  header("Locatio n:http://bubba/send-confirm.php");
                  exit;
                  }
                  ?>
                  DOCTYPE
                  <html>
                  ....
                  ....
                  <form action=<?php include "myscript.p hp" ?> method="POST">
                  <input name="email_add ress" type="text" size="10" maxlength="255" >
                  <textarea name'"message" rows="12" cols="22"> </textarea>
                  <input type="submit" value="Send">
                  </form>
                  ....
                  ....
                  </html>
                  --------------------------

                  Does my form look correct?

                  What form attributes are required (which are optional) - name, type, method... ?

                  Am I properly referencing "myscript.p hp"? Do I need the include statement?

                  Thanks again for you help!


                  Comment

                  • David Haynes

                    #10
                    Re: How to redirect after form submit?

                    deko wrote:[color=blue][color=green]
                    >> The form processing should be the *first* thing in the file.
                    >> Something like:
                    >>
                    >> DOCTYPE ...
                    >> <?php
                    >> if( isset($_POST['foo'] ) {
                    >> // do form processing
                    >>
                    >> if( $isValid ) {
                    >> header();
                    >> exit;
                    >> }
                    >> }
                    >> ?>
                    >> <form action="..." method="POST">
                    >> ...
                    >> <input type="text" name="foo" size="10" maxlength="255" >
                    >> <input type="submit">
                    >> </form>[/color]
                    >
                    > Thanks, that helped. I am getting the correct redirect behavior now.
                    >
                    > But I still have a couple of questions...
                    >
                    > Here's how it looks now:
                    >
                    > ------------------------
                    > <?php
                    > $message = $_POST['message'];
                    > $email = $_POST['email_address'];
                    > if (!empty($messag e) && !empty($email))
                    > {
                    > header("Locatio n:http://bubba/send-confirm.php");
                    > exit;
                    > }
                    > ?>
                    > DOCTYPE
                    > <html>
                    > ...
                    > ...
                    > <form action=<?php include "myscript.p hp" ?> method="POST">
                    > <input name="email_add ress" type="text" size="10" maxlength="255" >
                    > <textarea name'"message" rows="12" cols="22"> </textarea>
                    > <input type="submit" value="Send">
                    > </form>
                    > ...
                    > ...
                    > </html>
                    > --------------------------
                    >
                    > Does my form look correct?
                    >
                    > What form attributes are required (which are optional) - name, type,
                    > method... ?
                    >
                    > Am I properly referencing "myscript.p hp"? Do I need the include statement?
                    >
                    > Thanks again for you help!
                    >
                    >[/color]
                    Assuming that all this is saved in a file named 'myscript.php', then
                    your form should look like this:

                    <form action="myscrip t.php" method="POST">
                    <input type="text" name="email_add ress" value="<?php echo
                    $_POST['email_address'];?>" size="10" maxlength="255" >
                    <textarea name="message" rows="12" cols="22">
                    <?php echo $_POST['message'];?>
                    </textarea>
                    <input type="submit" value="Send">
                    </form>

                    The <?php echo bits cause the form to redisplay what was entered since
                    the only times the user will see the form are a) if this is the first
                    time or b) if there was some error in processing the values supplied to
                    the form.

                    The include statement is used to read in code from another file and
                    merge it into the current image and then interpret it. In a form action,
                    you are just specifying the name of the php file that the browser should
                    call when the submit action of the form is triggered.

                    The typical minimum set of attributes on <input> tags are type and name.

                    -david-

                    Comment

                    • deko

                      #11
                      Re: How to redirect after form submit?

                      making progress...
                      [color=blue]
                      > The <?php echo bits cause the form to redisplay what was entered since the
                      > only times the user will see the form are a) if this is the first time or b)
                      > if there was some error in processing the values supplied to the form.[/color]

                      I was just trying to put the form processing code into a separate file (not echo
                      it). That's more of a managability thing - for troubleshooting , I'll put the
                      code in-line.

                      I'm still having trouble getting the form action working:

                      <form action="<?php
                      //write to file for testing - production code will send email
                      $message_file = "/home/username/public_html/cgi-bin/messages.txt";
                      $message = $_POST['message'];
                      $fp_msg = fopen($message_ file,"a");
                      fwrite($fp_msg, $message."\n");
                      fclose($fp_msg) ;
                      ?>" method="post" name="feedback" >
                      <textarea name="message" rows="12" cols="22" maxlength="4096 "></textarea></p>
                      <input type="submit" value="Send">
                      </form>

                      The problem is nothing gets written to messages.txt!

                      The redirect is working, now that I have this code at the very top of the page:

                      <?php
                      $message = $_POST['message'];
                      $email = $_POST['email_address'];
                      if (!empty($messag e) && !empty($email))
                      {
                      header("Locatio n:http://www.mysite.com/send-confirm.php");
                      exit;
                      }
                      ?>

                      But does this prevent the form's in-line code in the from running?

                      Should I put the form action code in just above the redirect?

                      A couple of other questions:

                      Is "4096" an acceptable value for maxlength? That's about a full page of text
                      (counting spaces).

                      Should I use the $_REQUEST['message'] variable? I've seen this in a couple of
                      examples - what is it for?

                      Thanks!

                      Comment

                      • David Haynes

                        #12
                        Re: How to redirect after form submit?

                        deko wrote:[color=blue]
                        > making progress...
                        >[color=green]
                        >> The <?php echo bits cause the form to redisplay what was entered since
                        >> the only times the user will see the form are a) if this is the first
                        >> time or b) if there was some error in processing the values supplied
                        >> to the form.[/color]
                        >
                        > I was just trying to put the form processing code into a separate file
                        > (not echo it). That's more of a managability thing - for
                        > troubleshooting , I'll put the code in-line.
                        >
                        > I'm still having trouble getting the form action working:
                        >
                        > <form action="<?php
                        > //write to file for testing - production code will send email
                        > $message_file = "/home/username/public_html/cgi-bin/messages.txt";
                        > $message = $_POST['message'];
                        > $fp_msg = fopen($message_ file,"a");
                        > fwrite($fp_msg, $message."\n");
                        > fclose($fp_msg) ;
                        > ?>" method="post" name="feedback" >
                        > <textarea name="message" rows="12" cols="22"
                        > maxlength="4096 "></textarea></p>
                        > <input type="submit" value="Send">
                        > </form>[/color]

                        I think you are confused about what the 'action' attribute is about. The
                        'action' is used by the browser to process the 'submit'. As such, it is
                        executed on the browser system not the server system.[color=blue]
                        >
                        > The problem is nothing gets written to messages.txt!
                        >
                        > The redirect is working, now that I have this code at the very top of
                        > the page:
                        >
                        > <?php
                        > $message = $_POST['message'];
                        > $email = $_POST['email_address'];
                        > if (!empty($messag e) && !empty($email))
                        > {
                        > header("Locatio n:http://www.mysite.com/send-confirm.php");
                        > exit;
                        > }
                        > ?>
                        >
                        > But does this prevent the form's in-line code in the from running?
                        >
                        > Should I put the form action code in just above the redirect?[/color]
                        (see below: There is a required separation of the processing of the data
                        and the capture of the data you need to understand.)[color=blue]
                        >
                        > A couple of other questions:
                        >
                        > Is "4096" an acceptable value for maxlength? That's about a full page
                        > of text (counting spaces).[/color]
                        Yes. maxlength is more typically used in conjunction with databases
                        where the text fields have pre-defined maximum sizes.[color=blue]
                        >
                        > Should I use the $_REQUEST['message'] variable? I've seen this in a
                        > couple of examples - what is it for?[/color]
                        $_REQUEST is used if you don't know if the form will be sending data via
                        the POST or GET methods. REQUEST shows both. Personally, I tend to stick
                        with POST unless I am moving between web portals since POST obscures
                        the data being transmitted between the browser and the server a bit more.[color=blue]
                        >
                        > Thanks![/color]

                        Here's the deal.
                        1. break your code into two pieces. I'll call them email.form.html and
                        email.ctrl.php (ctrl for controller).
                        2. set up your form as follows:
                        DOCTYPE ...
                        <html>
                        <head>
                        </head>
                        <body>
                        <form action="email.c trl.php" method="POST">
                        <input type="text" ... (as per previous email)
                        </form>
                        </body>
                        </html>

                        Notice that this file is pure HTML.

                        3. Now set up your controller as:

                        <?php
                        if( isset($_POST['message']) {
                        //write to file for testing - production code will send email
                        $message_file = "/home/username/public_html/cgi-bin/messages.txt";
                        $message = $_POST['message'];
                        $fp_msg = fopen($message_ file,"a");
                        fwrite($fp_msg, $message."\n");
                        fclose($fp_msg) ;
                        }
                        header("locatio n: email.form.html ");
                        ?>

                        4. Now execute your form - the message should be stored on your browser.

                        5. In depth:
                        a) the HTML code in email.form.html paints the form
                        b) the user fills out the form and clicks the submit button
                        c) the browser puts the form data into a POST action and sends it the
                        email.ctrl.php program.
                        d) the email.ctrl.php sees if there is any POST data and, if so,
                        saves it to the file
                        e) it then redirects the browser by telling the browser to load the
                        email.form.html page again.
                        f) we are back at a)

                        This is a rudimentary MVC model where the form is the View and the ctrl
                        is the Controller.

                        Now, some people combine the ctrl and the form into one file. In this
                        case, you would place the ctrl code before the form code in the file. I
                        tend not to do this because I like to separate the business/validation
                        logic from the presentation code, but others argue that its nice to have
                        all the code in one place. Either will work.

                        Once this is working, you can flesh it out with integrity checks and
                        other input types (email address, subject, CC, BCC, etc.)

                        -david-

                        Comment

                        • deko

                          #13
                          Re: How to redirect after form submit?

                          This seems to be working...

                          This goes at the top of the page, before any other header info is sent:

                          $message = $_POST['message'];
                          $email = $_POST['email_address'];
                          if (!empty($messag e) && !empty($email))
                          {
                          header("Locatio n:http://www.mysite.com/send-confirm.php");
                          mail("me @ mysite.com", "Feedback Form", $message, "From: $email");
                          exit;
                          }

                          This form can go anywhere in the page:

                          <form action="" method="post" name="feedback" >
                          <input name="email_add ress" type="text" size="30" maxlength="255" /><br />
                          <textarea name="message" rows="12" cols="22" maxlength="4096 "></textarea>
                          <input type="submit" value="Send">
                          </form>

                          Note that the form action is: ""

                          I have not tested this fully yet - it works writing the message to a file (as in
                          my previous example), but I don't know if it will send the message as an email.

                          The goal of this exersize was simply to redirect the user to a confirmation page
                          after successfully sending a message via an in-page feedback form on my web
                          site.


                          Comment

                          • gauri
                            New Member
                            • Jun 2006
                            • 3

                            #14
                            set the action of the form to the page u want to redirect it to.
                            example:
                            <form name="form1" action="yourpag e.php" ... >

                            If you are not submitting the page then use the function header();
                            the warning "Warning: Cannot add header information - headers already sent by (output started at ..." is b'coz output buffering is not initialised.
                            use @ob_start() as the first line of you code.

                            hope this helps ..

                            After the "send" button is clicked, I want the user to be redirected to a
                            different page that says "Your message has been sent."

                            How do I do this?

                            I've tried using:

                            header("Locatio n:http://www.mysite.com/send-confirm.php");

                            But here's the error I get:

                            Warning: Cannot add header information - headers already sent by (output started
                            at ...

                            Here's the code:

                            <form action= "
                            <?php
                            $message = trim($_REQUEST['message']);
                            $email_address = trim($_REQUEST['email_address']);
                            if (empty($message ) || empty($email_ad dress))
                            {
                            $valid_message = false;
                            }
                            else
                            {
                            mail( "my email address", "feedback form", $message, "from:
                            $email_address" );
                            $valid_message = true;
                            }
                            ?>"
                            <input name="email_add ress" type="text" size="30"/><br />
                            <textarea name="message" rows="12" cols="23"></textarea><br />
                            <input type="submit" value="Send">
                            </form>
                            <?php
                            if ($valid_message )
                            {
                            echo "valid message";
                            header("Locatio n:http://www.mysite.com/send-confirm.php");
                            exit();
                            }
                            else
                            {
                            echo "invalid message";
                            }
                            ?>

                            How do I redirect after the user clicks the send button?

                            Thanks in advance.[/QUOTE]

                            Comment

                            Working...