0

How to redirect to a different page after user clicks Add: I am trying to redirect the user to a different page called (Thank_you.php) after the user fills out the form fields and clicks Add to submit the form.

   //include the header
   $page_title = 'Add Company';
   include ('includes/header.html');
   echo '<h1>Add Company</h1>';

   require_once ('../mysqli_connect.php');
   if ($_POST['submitted']){
     $company_name=$_POST['company_name'];
     $product_type=$_POST['product_type'];
     $city=$_POST['city'];
   $state=$_POST['state'];
     $query="INSERT INTO Company (Company_Name, Product_Type, City, State)
        Values ('$company_name', '$product_type', '$city','$state')";
     $result=@mysqli_query ($dbc, $query);
     if ($result){

         //echo "<center><p><b>Thank you.</b></p>";
         echo "<a href=Thank_you.php>The company has been added</a></center>";            // echo "<a href=index.php>Show All URLs</a></center>";
       }else {

                                //  echo "<p>The record could not be added due      to a system error" . mysqli_error() . "</p>";
     }
   } // only if submitted by the form
   mysqli_close($dbc);
   ?>
   <form action="<? echo $PHP_SELF;?>" method="post"><p>        
   Company Name:<br> <input name="company_name" size=30><p>
   Product Type:<br> <input name="product_type" size=30><p>
   City:<br> <input name="city" size=30><p>
   State:<br> <input name="state" size=30><p>

  <input type=submit value=Add>
  <input type=reset value=Clear>
  <input type=hidden name=submitted value=true>
  </form>
  <?
  //include the footer
  include ("includes/footer.html");

  ?>
user7067590
  • 13
  • 1
  • 3
  • 2
    You can use `header("Location: Thank_you.php");` in php. Otherwise you can also use `javascript`. **WARNING:** You are also open for `sql injection` see an interesting post about it [here](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Nytrix Dec 06 '16 at 23:31
  • Where would I include: header("Location: Thank_you.php"); in my code? – user7067590 Dec 06 '16 at 23:39
  • 2
    Possible duplicate of [How to make a redirect in PHP?](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – leepowers Dec 06 '16 at 23:41
  • At the point you want to redirect the person? Looks like after the `if($result){`. – Nytrix Dec 06 '16 at 23:41

2 Answers2

0

this should work

   $page_title = 'Add Company';
   include ('includes/header.html');
   echo '<h1>Add Company</h1>';

   require_once ('../mysqli_connect.php');
   if ($_POST['submitted']){
     $company_name=$_POST['company_name'];
     $product_type=$_POST['product_type'];
     $city=$_POST['city'];
   $state=$_POST['state'];
     $query="INSERT INTO Company (Company_Name, Product_Type, City, State)
        Values ('$company_name', '$product_type', '$city','$state')";
     $result=@mysqli_query ($dbc, $query);
     if ($result){

        header("Location: Thank_you.php");
       }else {

                                //  echo "<p>The record could not be added due      to a system error" . mysqli_error() . "</p>";
     }
   } // only if submitted by the form
   mysqli_close($dbc);
   ?>
   <form action="<? echo $PHP_SELF;?>" method="post"><p>        
   Company Name:<br> <input name="company_name" size=30><p>
   Product Type:<br> <input name="product_type" size=30><p>
   City:<br> <input name="city" size=30><p>
   State:<br> <input name="state" size=30><p>

  <input type=submit value=Add>
  <input type=reset value=Clear>
  <input type=hidden name=submitted value=true>
  </form>
  <?
  //include the footer
  include ("includes/footer.html");

  ?>
codebro
  • 11
  • 6
  • also this is a good source for learing about sql injection and it explain how to do it the right way – codebro Dec 06 '16 at 23:44
  • It's still displays an error message: Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/htdocs/includes/header.html:4) in /home/public_html/htdocs/add_company.php on line 19. It does like the: header("Location: Thank_you.php"); }else { – user7067590 Dec 07 '16 at 03:41
0

I think header("Location: Thank_you.php"); should work . Updated code will be :

$

page_title = 'Add Company';
   include ('includes/header.html');
   echo '<h1>Add Company</h1>';

   require_once ('../mysqli_connect.php');
   if ($_POST['submitted']){
     $company_name=$_POST['company_name'];
     $product_type=$_POST['product_type'];
     $city=$_POST['city'];
   $state=$_POST['state'];
     $query="INSERT INTO Company (Company_Name, Product_Type, City, State)
        Values ('$company_name', '$product_type', '$city','$state')";
     $result=@mysqli_query ($dbc, $query);
     if ($result){

        header("Location: Thank_you.php");
       }else {

                                //  echo "<p>The record could not be added due      to a system error" . mysqli_error() . "</p>";
     }
   } // only if submitted by the form
   mysqli_close($dbc);
   ?>
   <form action="<? echo $PHP_SELF;?>" method="post"><p>        
   Company Name:<br> <input name="company_name" size=30><p>
   Product Type:<br> <input name="product_type" size=30><p>
   City:<br> <input name="city" size=30><p>
   State:<br> <input name="state" size=30><p>

  <input type=submit value=Add>
  <input type=reset value=Clear>
  <input type=hidden name=submitted value=true>
  </form>
  <?
  //include the footer
  include ("includes/footer.html");

  ?>