如何将表单发布到相同的php页面并拦截它?

时间:2015-06-17 08:49:03

标签: php forms post

我正在尝试将表单提交到同一页面,所以我想提交数据,然后保存到数据库中...然后用新数据更新我的页面......

我是这样做的:

 <?php
                     if (isset($_POST["matricola"]) && isset($_POST["pc"])){
                         echo "QUI";
                        $conn = dbConnect($USERDB, $PASSWORDDB, $NAMEDB); //seleziono il database e mi connetto

                        $matricola = $_POST['matricola'];
                        $pc = $_POST['pc'];

                        $queryGetUser = "select id from user where matricola = ".$matricola;
                        $user = dbQueryGetResult($queryGetUser);

                        $today = getdate();
                        $data = $today['mday']."/".$today['month']."/".$today['year']; 
                        $ora = $today['hours'].":".$today['minutes'].":".$today['seconds'];

                        $sql = "insert into utelenza (id_user, id_pc, dataEntrata, oraEntrata) values (".$user[0].", ".$pc.", ".$data.", ".$ora.")";
                        dbQueryInsert($sql);
                     }                    
                    //Prendo dal DB le note del laboratorio visualizzabili da tutti
                    $conn = dbConnect($USERDB, $PASSWORDDB, $NAMEDB);
                    $queryGetNoteLab = "select noteLaboratorio from laboratorio where id_laboratorio = " . $IDLAB;
                    $notaLab = dbQueryGetResult($queryGetNoteLab);

                    $queryGetInfoLab = "select info150ore from laboratorio where id_laboratorio = " . $IDLAB;
                    $infoLab = dbQueryGetResult($queryGetInfoLab);

                    $queryGetPcs = "select * from pc where id_laboratorio = " . $IDLAB;
                    $pcs = dbQueryReturnMatrix($queryGetPcs);

                    dbDisconnect($conn);

                    echo "<form action='home150.php' method='post'>"
                    . "<table><tr><td>Matricola</td><td><input id='matricola' type='text' ></td></tr>"
                    . "<tr><td>PC</td><td><select id='pc'><option value=''></option>";

                    for ($i = 0; $i < count($pcs); $i++) {
                        echo "<option value=" . $pcs[$i][0] . ">" . $pcs[$i][2] . "</option>";
                    }
                    echo "</select></td></tr></table>"
                    . "<input id='submitButton' type='submit' value='Inserisci'></form>";
                    ?>

表单已提交,但提交后我无法拦截数据发布..

为什么?我该如何解决?

谢谢!

2 个答案:

答案 0 :(得分:2)

如果您想在同一页面中提交表单,请更改您的表单,然后无需为form action提供提交按钮的名称,并在php代码中使用isset按钮提交< / p>

<form action='' method='post'>"
    <table>
        <tr>
            <td>Matricola</td>
            <td>
                <input id='matricola' type='text'>
            </td>
        </tr>
        <tr>
            <td>PC</td>
            <td>
                <select id='pc'>
                    <option value=''></option>

                    <?php for ($i=0 ; $i < count($pcs); $i++) {?>
                    <option value="<?php echo $pcs[$i][0]?>">
                        <?php echo $pcs[$i][2]?>
                    </option>
                    <?php }?>
                </select>
            </td>
        </tr>
    </table>
    <input id='submitButton' type='submit' value='Inserisci' name="submit">
</form>

<?php if(isset($_POST['submit']))
{
//your query for insert or whatever
}?>

答案 1 :(得分:1)

您应该为所有输入命名。例如提交按钮。

<input type='submit' name='submit' value='Inserisci'>

然后在你上面的代码中。

if(isset($_POST['submit'])){
  //check values of form
}

将动作更改为空,然后它将自行调用。

<form action='' method='post'>