在数据库中发布数据而不刷新PHP页面

时间:2016-09-06 07:57:29

标签: php

我正在使用两个php文件。第一个文件包含一个搜索引擎,在第二个php文件中输出结果。第二个php文件显示在第一个php文件的搜索引擎下面的iframe中。此文件还包含需要使用post提交的数据。我可以提交表单并将其重定向到第3个php文件。但我不想离开当前页面。到目前为止,我能做的是在同一页面上调用isset函数并将insert命令激发到我的表。但它刷新了第二个php文件。刷新将重置从第一个php文件传递的搜索值,这是不好的。请帮忙

这是我的第二个php文件:

    <!DOCTYPE html>
    <html>
      <head>
        <title>RFC System</title>
        <script src="jquery-1.12.4.js"></script>
        <link rel="stylesheet" type="text/css" href="style2.css">
        <link rel="stylesheet" type="text/css" href="modal.css">
        </head>
        <body>
        <?php 
            require ("../funcs.php"); site_security(); 
            set_error_handler ("my_error_handler");
            set_time_limit(0);
            date_default_timezone_set ("Asia/Manila");
            require("../connmysql.php");
            $session_user_id = $_SESSION["USERCID"];
            $session_branch = $_SESSION["BRANCHCODE"];
            $branchname=$_POST['branchname'];
            $empbio=$_POST['empbio'];
            $dtrdate_from=$_POST['dtrdate_from'];
            $dtrdate_to=$_POST['dtrdate_to'];

            ##########HELP HERE!
            if(isset($_POST['reason'])){
                #save to database without refreshing the page
            }
            #####################
            if((empty($branchname)) || (empty($empbio)) || (empty($dtrdate_from)) || (empty($dtrdate_to))){
                echo "<font color='#FF0000'><b>Please fill-in all fields to start search</b></font>";
            }else{

                $startDate = DateTime::createFromFormat("Y/m/d","$dtrdate_from",new DateTimeZone("Asia/Manila"));
                $endDate = DateTime::createFromFormat("Y/m/d","$dtrdate_to",new DateTimeZone("Asia/Manila"));
                $periodInterval = new DateInterval( "P1D" ); // 1-day, though can be more sophisticated rule
                $period = new DatePeriod( $startDate, $periodInterval, $endDate );      
                $queryname=mysqli_query($conn,"select a.nsemp_fname,a.nsemp_mname,a.nsemp_lname,a.nsemp_sufixname,a.nsemp_empid,a.nsemp_bionum,a.nsemp_brcode from rfc_prf_nsemp a where  a.nsemp_bionum=12476")or die();
                while($row=mysqli_fetch_array($queryname)){
                    $fname=$row[0];
                    $mname=$row[1];
                    $lname=$row[2];
                    $suffix=$row[3];
                    $empid=$row[4];
                    $bionum=$row[5];
                    $brcode=$row[6];
                }
                echo"<hr/>
                <h3><b>Name:</b>$fname $mname[0] $lname $suffix<br/>
                <b>Employee No:</b> $empid<br/>
                <b>Branch:</b> $brcode<br/></h3>
                <table class='x'><tr>";
                    foreach($period as $date){
                        $x = $date->format("Y-m-d");
                        $day = date('l', strtotime($x));
                        if($day=="Sunday"){$background="#00e673";}else if($day=="Saturday"){$background="#ffff99";}else{$background="#ffffff";}
                        echo"<td height='150px' style='border-style: solid; background:$background'>
                        <form method='post' action='#'>
                        <b><font color='#1a1aff'>$x<br/>$day</font></b><br/><br/>";
                        $query=mysqli_query($conn,"select dtr_time,(CASE in_or_out WHEN 0 THEN 'IN' WHEN 1 THEN 'OUT' END) AS status,datacid from rfc_bio_dtr where dtr_date='$x' and empbio='12476';")or die(mysqli_error());
                        $check="";
                        while($row=mysqli_fetch_array($query)){
                            echo "$row[0] $row[1] <br/>";
                            $check .=$row[1];
                        }
                        if(preg_match('(IN)', $check) && preg_match('(OUT)', $check)) { echo" ";}else{
                            $queryReason=mysqli_query($conn,"select tk_reason_id,tk_reason_desc from rfc_timekeeping_param;")or die();
                            echo "<br/><br/>Remarks:<select name='' id=''>";
                            while($row=mysqli_fetch_array($queryReason)){
                                echo "<option value='$row[0]'>$row[1]</option>";
                            }
                            echo"</select><br/>Notes:<br/><textarea></textarea><input type='submit' value='save' name='reason' id='reason' class='totobutton'>";
                        }
                        echo"</form></td>";

                    }
            }
        ?>
    </body>

这是使用jquery / ajax的更新代码。下一个表数据存在问题,它们不会被提交。只提交了第一个表格数据:

    <!DOCTYPE html>
    <html>
      <head>
        <title>RFC System</title>
        <script src="jquery-1.12.4.js"></script>
        <link rel="stylesheet" type="text/css" href="style2.css">
        <link rel="stylesheet" type="text/css" href="modal.css">
        <script>
          $(function () {
            $('#myform').on('submit', function (e) {
              e.preventDefault();
              $.ajax({
                type: 'post',
                url: 'savetodb.php',
                data: $('#myform').serialize(),
                success: function () {
                  alert('form was submitted');
                }
              });
            });
          });
        </script>
        </head>
        <body>
        <?php 
            require ("../funcs.php"); site_security(); 
            set_error_handler ("my_error_handler");
            set_time_limit(0);
            date_default_timezone_set ("Asia/Manila");
            require("../connmysql.php");
            $session_user_id = $_SESSION["USERCID"];
            $session_branch = $_SESSION["BRANCHCODE"];
            $branchname=$_POST['branchname'];
            $empbio=$_POST['empbio'];
            $dtrdate_from=$_POST['dtrdate_from'];
            $dtrdate_to=$_POST['dtrdate_to'];
            if((empty($branchname)) || (empty($empbio)) || (empty($dtrdate_from)) || (empty($dtrdate_to))){
                echo "<font color='#FF0000'><b>Please fill-in all fields to start search</b></font>";
            }else{

                $startDate = DateTime::createFromFormat("Y/m/d","$dtrdate_from",new DateTimeZone("Asia/Manila"));
                $endDate = DateTime::createFromFormat("Y/m/d","$dtrdate_to",new DateTimeZone("Asia/Manila"));
                $periodInterval = new DateInterval( "P1D" ); // 1-day, though can be more sophisticated rule
                $period = new DatePeriod( $startDate, $periodInterval, $endDate );      
                $queryname=mysqli_query($conn,"select a.nsemp_fname,a.nsemp_mname,a.nsemp_lname,a.nsemp_sufixname,a.nsemp_empid,a.nsemp_bionum,a.nsemp_brcode from rfc_prf_nsemp a where  a.nsemp_bionum=12476")or die();
                while($row=mysqli_fetch_array($queryname)){
                    $fname=$row[0];
                    $mname=$row[1];
                    $lname=$row[2];
                    $suffix=$row[3];
                    $empid=$row[4];
                    $bionum=$row[5];
                    $brcode=$row[6];
                }
                echo"<hr/>
                <h3><b>Name:</b>$fname $mname[0] $lname $suffix<br/>
                <b>Employee No:</b> $empid<br/>
                <b>Branch:</b> $brcode<br/></h3>
                <table class='x'><tr>";
                    foreach($period as $date){
                        $x = $date->format("Y-m-d");
                        $day = date('l', strtotime($x));
                        if($day=="Sunday"){$background="#00e673";}else if($day=="Saturday"){$background="#ffff99";}else{$background="#ffffff";}
                        echo"<td height='150px' style='border-style: solid; background:$background'>
                        <b><font color='#1a1aff'>$x<br/>$day</font></b><br/><br/>";
                        $query=mysqli_query($conn,"select dtr_time,(CASE in_or_out WHEN 0 THEN 'IN' WHEN 1 THEN 'OUT' END) AS status,datacid from rfc_bio_dtr where dtr_date='$x' and empbio='12476';")or die(mysqli_error());
                        $check="";
                        while($row=mysqli_fetch_array($query)){
                            echo "$row[0] $row[1] <br/>";
                            $check .=$row[1];
                        }
                        if(preg_match('(IN)', $check) && preg_match('(OUT)', $check)) { echo" ";}else{
                            $queryReason=mysqli_query($conn,"select tk_reason_id,tk_reason_desc from rfc_timekeeping_param;")or die();
                            ?>
                            <form id="myform">
                                <select name="reason" value="reason">
                                <?php
                                    while($row=mysqli_fetch_array($queryReason)){
                                        echo "<option value='$row[0]'>$row[1]</option>";
                                    }
                                ?></select>
                                <textarea name="notes" value="notes"></textarea><br>
                                <input name="submit" type="submit" value="Submit">
                            </form><?php
                        }
                        echo"</td>";

                    }
            }
        ?>
    </body>

这是savetodb.php

<?php
    require("../connmysql.php");
    $fname=$_POST['reason'];
    $lname=$_POST['notes'];
        $queryx=mysqli_query($conn,"insert into test (fname,lname) values ('$fname','$lname');")or die(mysqli_error());
?>

1 个答案:

答案 0 :(得分:1)

你已经加载了jQuery,所以从一个onclick处理程序启动AJAX并将保存放在一个完全不同的PHP文件中(一个只输出“OK”或“ERROR:something。”请参阅http://pqxb.qr.ai了解相关内容教程。

相关问题