dtpoius74857
2014-10-20 11:09 浏览 13
已采纳

单击按钮,保存数据并转到下一个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"/>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答 默认 最新

  • 已采纳
    dongzhu6900 dongzhu6900 2014-10-20 11:33

    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>";
            }
            }
    ?>
    
    点赞 评论 复制链接分享
  • douguanya4248 douguanya4248 2014-10-20 11:12

    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().

    点赞 评论 复制链接分享
  • dongzhouzhang8696 dongzhouzhang8696 2014-10-20 11:14

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

    点赞 评论 复制链接分享
  • dongying6659 dongying6659 2014-10-20 11:19

    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');
    }
    
    点赞 评论 复制链接分享

相关推荐