Redirect after running PHP script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CalvinSmith
    New Member
    • Apr 2010
    • 15

    Redirect after running PHP script

    Ok this is the script that handles the form submission and input's it to the SQL database. It works 100%.
    Code:
    <?php
    $con = mysql_connect("*****","*****","*****");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("ccsdesig_first", $con);
    $sql="INSERT INTO ***** (Name,Surname,Number,Email,MovingFrom,MovingTo,Date) 
    		VALUES ('$_POST[Name]','$_POST[Surname]','$_POST[Number]','$_POST[Email]','$_POST[MovingFrom]','$_POST[MovingTo]','$_POST[Date]')";
    		
    $sql="INSERT INTO destination (CurrentPremises,CurrentFloor,DestinationPremises,DestinationFloor,WrapEverything,Unpack,Hangerpack,SendMaterials,InsureEverything) 
    		VALUES ('$_POST[CurrentPremises]','$_POST[CurrentFloor]','$_POST[DestinationPremises]','$_POST[CurrentFloor]','$_POST[WrapEverything]','$_POST[Unpack]','$_POST[Hangerpack]','$_POST[SendMaterials]','$_POST[InsureEverything]')";
    	
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    mysql_close($con)
      echo "Done";
    ?>
    What I would now like the script to do is to send the user to another page - what do I use to automatically redirect?
    I've tried:
    Code:
    header("Location:http://www.xxxxxxx.com/viewguestbook2.php");
    and
    Code:
    <META HTTP-EQUIV="refresh" content="0;URL=index.php">
    And neither works - could someone point me in the right direction please?

    My thanks in anticipation
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    you can use a JS to redirect user

    Regards

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hey.

      What happened when you tried the header() function? It should have worked.
      (Try turning on the error messages to see what really happened.)

      You can do three things to redirect a user:
      1. Send a Location header, using the header function. - This is the preferred method, as it is the "shortest" way to send the browser the request to relocate.
      2. Add a <meta> "refresh" header. - This is a close second, only because it requires the browser to parse the HTML before actually getting the request.
      3. Send a JavaScript "location.h ref" value. - This should be avoided, because JavaScript can not be counted on in all cases. Users can turn off JavaScript, and some browser (and I use that term loosely) don't support it.

      Comment

      • CalvinSmith
        New Member
        • Apr 2010
        • 15

        #4
        somehow it posted twice

        Comment

        • CalvinSmith
          New Member
          • Apr 2010
          • 15

          #5
          Hi,

          I get the following error:
          Parse error: syntax error, unexpected T_STRING in /home/****/public_html/insert.php on line 21
          When using the following:
          Code:
          <?php
          error_reporting(E_ALL);
          ini_set('display_errors', true);
          $con = mysql_connect("localhost","*****","*****");
          if (!$con)
            {
            die('Could not connect: ' . mysql_error());
            }
          mysql_select_db("*****", $con);
          $sql="INSERT INTO contactdetails (Name,Surname,Number,Email,MovingFrom,MovingTo,Date) 
          		VALUES ('$_POST[Name]','$_POST[Surname]','$_POST[Number]','$_POST[Email]','$_POST[MovingFrom]','$_POST[MovingTo]','$_POST[Date]')";
          		
          $sql="INSERT INTO destination (CurrentPremises,CurrentFloor,DestinationPremises,DestinationFloor,WrapEverything,Unpack,Hangerpack,SendMaterials,InsureEverything) 
          		VALUES ('$_POST[CurrentPremises]','$_POST[CurrentFloor]','$_POST[DestinationPremises]','$_POST[CurrentFloor]','$_POST[WrapEverything]','$_POST[Unpack]','$_POST[Hangerpack]','$_POST[SendMaterials]','$_POST[InsureEverything]')";
          	
          if (!mysql_query($sql,$con))
            {
            die('Error: ' . mysql_error());
            }
          mysql_close($con)
          header("Location: http://www.xxx.com/email.php");
          exit;
          ?>

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            You are missing the semi-colon (;) from line #20. All lines must end in a semi-colon.

            Comment

            • CalvinSmith
              New Member
              • Apr 2010
              • 15

              #7
              Originally posted by Atli
              You are missing the semi-colon (;) from line #20. All lines must end in a semi-colon.
              Warning: Cannot modify header information - headers already sent by (output started at /home/****/public_html/insert.php:1) in /home/****/public_html/insert.php on line 21

              Comment

              • chathura86
                New Member
                • May 2007
                • 227

                #8
                you cannot have any echo statements or none php codes before the header

                ie. you cannot send any output to the browser before header()

                make sure that (even a white space could case an error)

                Regards

                Comment

                • CalvinSmith
                  New Member
                  • Apr 2010
                  • 15

                  #9
                  Thanks for your reply.
                  I checked through the code and dont think anything is posting to the browser at all. Perhaps I have missed something though?
                  Code:
                  <?php
                  error_reporting(E_ALL);
                  ini_set('display_errors', true);
                  $con = mysql_connect("*","*","*");
                  if (!$con)
                    {
                    die('Could not connect: ' . mysql_error());
                    }
                  mysql_select_db("*", $con);
                  $sql="INSERT INTO contactdetails (Name,Surname,Number,Email,MovingFrom,MovingTo,Date) 
                  		VALUES ('$_POST[Name]','$_POST[Surname]','$_POST[Number]','$_POST[Email]','$_POST[MovingFrom]','$_POST[MovingTo]','$_POST[Date]')";
                  $sql="INSERT INTO destination (CurrentPremises,CurrentFloor,DestinationPremises,DestinationFloor,WrapEverything,Unpack,Hangerpack,SendMaterials,InsureEverything) 
                  		VALUES ('$_POST[CurrentPremises]','$_POST[CurrentFloor]','$_POST[DestinationPremises]','$_POST[CurrentFloor]','$_POST[WrapEverything]','$_POST[Unpack]','$_POST[Hangerpack]','$_POST[SendMaterials]','$_POST[InsureEverything]')";
                  if (!mysql_query($sql,$con))
                    {
                    die('Error: ' . mysql_error());
                    }
                  mysql_close($con);
                  header("Location: http://www.*.co.za/email.php");
                  exit;
                  ?>

                  Comment

                  • CalvinSmith
                    New Member
                    • Apr 2010
                    • 15

                    #10
                    Warning: Cannot modify header information - headers already sent by (output started at /home/*/public_html/insert.php:1) in /home/*/public_html/insert.php on line 19

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      If you are getting that error, something is being printed before the header() call.

                      This is usually caused by white-spaces or other junk before the <?php block, BOM chars in Unicode documents or random echos in the code. - Check the source in the browser. That should include the junk chars that are causing this. (Post it here if you want us to take a look at it.)

                      You can also read this article for more details about this error, and how to fix it.

                      Comment

                      • CalvinSmith
                        New Member
                        • Apr 2010
                        • 15

                        #12
                        I read the article - thank you.
                        As far as I can see I have removed all the white spaces. Below is the code - Pls can you check where I have gone wrong:
                        Code:
                        <?php
                        $con = mysql_connect("*","*","*");
                        if (!$con)
                        {
                        die('Could not connect: ' . mysql_error());
                        }
                        mysql_select_db("*", $con);
                        $sql="INSERT INTO contactdetails (Name,Surname,Number,Email,MovingFrom,MovingTo,Date)
                        VALUES ('$_POST[Name]','$_POST[Surname]','$_POST[Number]','$_POST[Email]','$_POST[MovingFrom]','$_POST[MovingTo]','$_POST[Date]')";
                        $sql="INSERT INTO destination (CurrentPremises,CurrentFloor,DestinationPremises,DestinationFloor,WrapEverything,Unpack,Hangerpack,SendMaterials,InsureEverything) 
                        VALUES ('$_POST[CurrentPremises]','$_POST[CurrentFloor]','$_POST[DestinationPremises]','$_POST[CurrentFloor]','$_POST[WrapEverything]','$_POST[Unpack]','$_POST[Hangerpack]','$_POST[SendMaterials]','$_POST[InsureEverything]')";
                        if (!mysql_query($sql,$con))
                        {
                        die('Error: ' . mysql_error());
                        }
                        mysql_close($con);
                        header("Location: http://www.*.co.za/email.php");
                        exit;
                        ?>

                        Comment

                        • Atli
                          Recognized Expert Expert
                          • Nov 2006
                          • 5062

                          #13
                          We aren't talking about white-spaces inside the <?php .. ?> blocks, but outside them. White-spaces inside them don't matter.

                          [code=php] <?php
                          // There are three white-spaces before the <?php tag
                          // above this comment. This will cause the error.

                          $variable = '
                          something
                          something else.';
                          // The white-spaces in the above variable,
                          // or the string, have no effect.
                          ?>[/code]

                          Did you check out the source of the page in your browser? How did it look like?

                          Comment

                          • CalvinSmith
                            New Member
                            • Apr 2010
                            • 15

                            #14
                            Understood and as you can see there are no white spaces in the code I posted. I dont know how to view a PHP files source without it actually processing the script first. Once it processes the script the source is the following:
                            Code:
                            <br /> 
                            <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/ccsdesig/public_html/insert.php:1) in <b>/home/ccsdesig/public_html/insert.php</b> on line <b>17</b><br />
                            If I open the PHP with notepad the source looks exactly the same as it does in expression web and as I posted above.

                            Comment

                            • Atli
                              Recognized Expert Expert
                              • Nov 2006
                              • 5062

                              #15
                              Ok. The error still points to line #1 of the file, saying the output is starting there.

                              I would guess that your IDE is adding a BOM to your document; a Unicode special character that is meant to tell how the Unicode chars in the document are structured.

                              To check and fix that, you need an editor that can re-encode the file. I've used Notepad++ for that in the past. It's a very small and simple editor. A sort of advanced version of Notepad.

                              You can open your document in there, select "Encoding > Encode in UTF-8 without BOM", and then save the file.

                              That might fix the error.

                              Comment

                              Working...