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

时间:2012-06-21 05:25:39

标签: php

我希望在将数据成功提交到数据库后将用户重定向到下一页。我正在使用像这样的重定向功能

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


            }

但它没有重定向到任何地方,但它成功提交数据..请建议我在哪里出错。

4 个答案:

答案 0 :(得分:1)

在向浏览器发送任何内容后,您无法使用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> 

答案 1 :(得分:0)

你可以这样做,把它包含在开头:

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

希望这有帮助! :)

答案 2 :(得分:0)

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

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

答案 3 :(得分:0)

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

<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 -->