简体   繁体   中英

How to redirect to another page after submit in php

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");

  ?>

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");

  ?>

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");

  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM