将数据提交到数据库后重定向到下一页


redirecting to next page after submitting the data to database

我想将用户重定向到成功将他们的信息提交到数据库的下一页。我正在使用这样的重定向功能

<?php
    $plan = @$_GET['plan']; // this is from url
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1"  onsubmit="return Validate();">
                <input type="hidden" name="plan" value="<?php echo $plan ?>"/>
    Company Name: 
        <input type="text" name="CompanyName" style="width:230px; height:20px;" /><br /><br />
    Company E-mail : 
    <input type="text" name="CompanyEmail" style="width:230px; height:20px;" /><br /><br />
     Company Contact <input type="text" name="CompanyContact" style="width:230px; height:20px;" /><br /><br />
     Company Address: <input type="text" name="CompanyAddress" style="width:230px; height:20px;" /><br /><br />
    Select Your Registration Type : <br /><br />
Trail: <input type="radio" name="RegistrationType" value="Trail" /><br />
                                     Paid<input type="radio" name="RegistrationType" value="Paid" /><br /><br />
     <input type="hidden" name="form_submitted" value="1"/> 
     <input type="submit" value="REGISTER" name="submit" />

</form> 
 <?php
     include('connect.php');
            If (isset($_POST['submit']))
            {
            $CompanyName = $_POST['CompanyName'];
            $CompanyEmail = $_POST['CompanyEmail'];
            $CompanyContact = $_POST['CompanyContact'];
            $CompanyAddress = $_POST['CompanyAddress']; 
            $RegistrationType = $_POST['RegistrationType'];
            $Plans = $_POST['plan'];
            $Status = "Active";

    $sql = "INSERT INTO ApplicationRegister (CompanyName, CompanyEmail, CompanyContact, CompanyAddress, RegistrationType, ApplicationPlan, ApplicationStatus, CreatedDate) VALUES ('$CompanyName', '$CompanyEmail', '$CompanyContact', '$CompanyAddress', '$RegistrationType', '$Plans', '$Status', NOW() )";
$result = mysql_query($sql) or die(mysql_error());
if($plan == "trail")
{
header("Location: userRegister.php");
}
else
{
    header("Location : PaymentGateway.php");
}

            }

但它没有重定向到任何地方,但它成功地提交了数据。请告诉我哪里出错了。

在向

浏览器发送任何内容后,您不能使用header()。在这种情况下,您中间的所有 HTML 都会首先发送。将所有PHP移动到文件的顶部应该意味着它可以工作 - 还有一个更易于阅读的额外好处!

<?php
$plan = @$_GET['plan']; // this is from url
include('connect.php');
if (isset($_POST['submit'])) {
    $CompanyName = $_POST['CompanyName'];
    $CompanyEmail = $_POST['CompanyEmail'];
    $CompanyContact = $_POST['CompanyContact'];
    $CompanyAddress = $_POST['CompanyAddress'];
    $RegistrationType = $_POST['RegistrationType'];
    $Plans = $_POST['plan'];
    $Status = "Active";

    $sql = "INSERT INTO ApplicationRegister 
    CompanyName, CompanyEmail, CompanyContact, CompanyAddress, RegistrationType, ApplicationPlan, ApplicationStatus, CreatedDate) 
    VALUES 
    ('$CompanyName', '$CompanyEmail', '$CompanyContact', '$CompanyAddress', '$RegistrationType', '$Plans', '$Status', NOW() )";
    $result = mysql_query($sql) or die(mysql_error());
    if ($plan == "trail") {
        header("Location: userRegister.php");
        exit();
    } else {
        header("Location : PaymentGateway.php");
        exit();
    }
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1"  onsubmit="return Validate();">
    <input type="hidden" name="plan" value="<?php echo $plan ?>"/>
    Company Name: 
    <input type="text" name="CompanyName" style="width:230px; height:20px;" /><br /><br />
    Company E-mail : 
    <input type="text" name="CompanyEmail" style="width:230px; height:20px;" /><br /><br />
    Company Contact <input type="text" name="CompanyContact" style="width:230px; height:20px;" /><br /><br />
    Company Address: <input type="text" name="CompanyAddress" style="width:230px; height:20px;" /><br /><br />
    Select Your Registration Type : <br /><br />
    Trail: <input type="radio" name="RegistrationType" value="Trail" /><br />
    Paid<input type="radio" name="RegistrationType" value="Paid" /><br /><br />
    <input type="hidden" name="form_submitted" value="1"/> 
    <input type="submit" value="REGISTER" name="submit" />
</form> 

你可以这样做,通过在开头包含以下内容:

<?php
    if(mysql_query($query))
        header('Location: http://example.com/newurl/');
    else
        echo "Error!";
?>

希望这有帮助! :)

只是为了一个约定,养成在header()后使用exit的习惯。

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

仅放置要重定向的页面路径:

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
                                ADD CONTENT
                            </button>
                            <!-- Modal -->
                            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title" id="myModalLabel">Please Add Content</h4>
                                        </div>
                                        <div class="modal-body">
                <form role="form" action="" method="POST">
      <!-- YOUR HTML FORM GOES HERE--->
<button type="submit" name="submit" class="btn btn-primary btn-lg"id="sub" onclick="SUBMISSION()" >Submit </button>
                            </fieldset>
             </form>
  <?php
if(isset($_POST['submit']))
{
    SUBMISSION();
    }
function SUBMISSION()
{
 // UR CONNECTION ESTABLISHMENT CODE GOES HERE
  // SQL QUERY FOR INSERTION IN FIELDS
 echo"<script type='"text/javascript'">
document.location.href='http://localhost/your file path/YOURPAGE.php'; // ur desired redirect page path where u want to go with recent submit data u can also in same page
</script>";
$conn->CLOSE();
}
?>
                                        </div>
                                    </div>
                                    <!-- /.modal-content -->
                                </div>
                                <!-- /.modal-dialog -->

                            <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->