单击按钮,保存数据并转到下一个php页面

单击按钮,保存数据并转到下一个php页面

问题描述:

I don't know what is the problem. After i click the button, it only the data into database but will not go to next php page. Help me find out what is problems. Thank you.

if(isset($_POST['btnSubmit'])){
$AddMCQ = "INSERT INTO tblmc(Name,FromDate,ToDate,Reason) VALUES('".strtoupper($_POST['txtName'])."','".$_POST['txtFrom']."','".$_POST['txtTo']."','".strtoupper($_POST['txtReason'])."')";
$AddMCResult = mysql_query($AddMCQ,$link);
header('Location: mcreport.php');

if($AddMCResult)
        echo "<script>alert('Record Added.');</script>";

}
//button
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit"/>

Try this

<?php
     if(isset($_POST['btnSubmit']))
{

        $txtName=$_POST['txtName'];
        $txtFrom=$_POST['txtFrom'];
        $txtTo=$_POST['txtTo'];
        $txtReason=$_POST['txtReason'];
        $AddMCQ = "INSERT INTO tblmc(Name,FromDate,ToDate,Reason) VALUES('$txtName','$txtFrom','$txtTo','$txtReason')";
        $AddMCResult = mysql_query($AddMCQ,$link);
        if($AddMCResult)
        {
               echo "<script language=\"JavaScript\">
";
        echo "alert('Record Added.');
";
        echo "window.location='mcreport.php'";
        echo "</script>";
        }
        }
?>

After your header put die() like

header('Location: mcreport.php');
die();

And better you use Absolute urls.Also you can use exit() instead of die().

Your "Problem" is the result in $AddMCResult

After your have use header('Location: mcreport.php');

Your Script redirect to the given url and the result in $AddMCResult is not given any more

So a quick and dirty solution could be

if(isset($_POST['btnSubmit'])){
$AddMCQ = "INSERT INTO tblmc(Name,FromDate,ToDate,Reason) VALUES('".strtoupper($_POST['txtName'])."','".$_POST['txtFrom']."','".$_POST['txtTo']."','".strtoupper($_POST['txtReason'])."')";
$AddMCResult = mysql_query($AddMCQ,$link);
$_SESSION['AddMCResult'] = $AddMCResult;
header('Location: mcreport.php');


}

AND on mcreport.php

if(isset($_SESSION['AddMCResult']) && $AddMCResult)

        echo "<script>alert('Record Added.');</script>";

...

But check, that session_start() was called on both files ...

Check Carefully Table Name and Passing Parameters - Through one to another page - see get and post method-

<?php
include 'config.php';   
$submit="submit"; 
$page = $_SERVER['PHP_SELF'];
$sl_no=$_POST['sl-no'];
$f_name=$_POST['f_name'];
$l_name=$_POST['l_name'];

if($submit)
{
$sql = "INSERT INTO table_name(sl_no,f_name,l_name) values('$sl_no','$f_name','$l_name')";
$result = mysql_query($sql);
echo "Thank you! Information entered.
";

}
else
{
echo "There Is Something Going Wrong While Insertion";
header('Location: error.php');
}