用户通过回发表单提交时的PHP更新页面

时间:2013-12-05 17:56:23

标签: php forms postback

所以我有一个带有回发表单的页面,用户可以从数据库中删除记录(显示为复选框)。当您单击“提交”时,在同一页面上会显示一条确认消息“已成功删除记录”。

问题是复选框仍然存在。只有在刷新页面后,复选框才会消失。如何在用户点击“提交”后立即将其删除?

这是我的代码:

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<?php
//Connect to the database
require_once("dboconn.php");
$conn=ConnectionFactory::connect();

$query = "SELECT * FROM programmer";
$resultset = $conn->query($query);
while ($row = $resultset->fetch())
{
        echo "<p class='col'>";
        echo "<input type='checkbox' name='programmers[]' id='programmer".$row['programmer_id']."' value='".$row['programmer_id']."'>";
        echo "<a href='' class='help tip-below' data-tip='".projects($row['programmer_id'])."'><label for='programmer".$row['programmer_id']."'> ".$row['programmer_name']." ".$row['programmer_surname']."</label></a>";
        echo "</p>";
}?>
<input type="submit" value="Delete Programmer(s)" class="sub-del submit-btn">
</form>
<?php
if (isset($_POST['programmers'])){
    require_once("dboconn.php");
    $conn=ConnectionFactory::connect();

    $query="DELETE FROM programmer WHERE programmer_id=:programmer_id";
    $query2="DELETE FROM project WHERE programmer_id=:programmer_id";
    $query3="SELECT programmer_name, programmer_surname FROM programmer WHERE programmer_id=:programmer_id";
    $pr_stmt=$conn->prepare($query);
    $pr_stmt2=$conn->prepare($query2);
    $pr_stmt3=$conn->prepare($query3);
    $affected_rows=0;
    $notWorking=false; // set this variable to check if a programmer is working or not on any project
    $surnameDeletedProgs=array();
    $nameDeletedProgs=array();

    foreach($_POST['programmers'] as $programmer_id){
        $pr_stmt->bindValue(':programmer_id',$programmer_id);
        $pr_stmt2->bindValue(':programmer_id',$programmer_id);
        $pr_stmt3->bindValue(':programmer_id',$programmer_id);
        $pr_stmt3->execute();

        //Get the names and surnames of the programmers who were deleted from the database and store them in arrays
        $result=$pr_stmt3->fetch();
        array_push($nameDeletedProgs,$result['programmer_name']);
        array_push($surnameDeletedProgs,$result['programmer_surname']);

        //Delete the programmer from the database
        $affected_rows+=$pr_stmt->execute();

        //If they were working on a project, delete the project also from the 'project' table
        if(projects($programmer_id)!="Working on no projects at the moment"){
            $pr_stmt2->execute();
            echo "Also deleted the project they were working on";
            }
        else $notWorking=true;
        }

        //If they are not working on any project display this particular message
        if ($notWorking){
            echo "Hopefully, they were not working on any project at the moment so we just ";
        }

        //if there were no checkboxes selectes, display a message to tell people to select at least one programmer
        if ($affected_rows==0){
            echo "No programmers to delete. Please select at least one.";
            exit;
        }
        //display how many programmers were deleted from the 'programmers' table and also what are their names
        else{
            echo "deleted ".$affected_rows." programmer(s)";
            echo ". Successfully deleted:<ul>";
            for($i=0;$i<count($nameDeletedProgs);$i++){
                echo "<li>".$nameDeletedProgs[$i]." ".$surnameDeletedProgs[$i]."</li>";
            }
            echo "</ul>";
        }

        $conn=NULL; 
}

非常感谢你的帮助! 拉卢卡

1 个答案:

答案 0 :(得分:0)

目前,当您点击提交时,您使用旧数据构建HTML输出,然后更新数据库。您需要先更新数据库,然后获取数据以构建UI。您更新的代码应如下所示:

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<?php
//Connect to the database
require_once("dboconn.php");
$conn=ConnectionFactory::connect();

if (isset($_POST['programmers'])){
    require_once("dboconn.php");
    $conn=ConnectionFactory::connect();

    $query="DELETE FROM programmer WHERE programmer_id=:programmer_id";
    $query2="DELETE FROM project WHERE programmer_id=:programmer_id";
    $query3="SELECT programmer_name, programmer_surname FROM programmer WHERE programmer_id=:programmer_id";
    $pr_stmt=$conn->prepare($query);
    $pr_stmt2=$conn->prepare($query2);
    $pr_stmt3=$conn->prepare($query3);
    $affected_rows=0;
    $notWorking=false; // set this variable to check if a programmer is working or not on any project
    $surnameDeletedProgs=array();
    $nameDeletedProgs=array();

    foreach($_POST['programmers'] as $programmer_id){
        $pr_stmt->bindValue(':programmer_id',$programmer_id);
        $pr_stmt2->bindValue(':programmer_id',$programmer_id);
        $pr_stmt3->bindValue(':programmer_id',$programmer_id);
        $pr_stmt3->execute();

        //Get the names and surnames of the programmers who were deleted from the database and store them in arrays
        $result=$pr_stmt3->fetch();
        array_push($nameDeletedProgs,$result['programmer_name']);
        array_push($surnameDeletedProgs,$result['programmer_surname']);

        //Delete the programmer from the database
        $affected_rows+=$pr_stmt->execute();

        //If they were working on a project, delete the project also from the 'project' table
        if(projects($programmer_id)!="Working on no projects at the moment"){
            $pr_stmt2->execute();
            echo "Also deleted the project they were working on";
            }
        else $notWorking=true;
        }

        //If they are not working on any project display this particular message
        if ($notWorking){
            echo "Hopefully, they were not working on any project at the moment so we just ";
        }

        //if there were no checkboxes selectes, display a message to tell people to select at least one programmer
        if ($affected_rows==0){
            echo "No programmers to delete. Please select at least one.";
            exit;
        }
        //display how many programmers were deleted from the 'programmers' table and also what are their names
        else{
            echo "deleted ".$affected_rows." programmer(s)";
            echo ". Successfully deleted:<ul>";
            for($i=0;$i<count($nameDeletedProgs);$i++){
                echo "<li>".$nameDeletedProgs[$i]." ".$surnameDeletedProgs[$i]."</li>";
            }
            echo "</ul>";
        }
    }
    $query = "SELECT * FROM programmer";
    $resultset = $conn->query($query);
    while ($row = $resultset->fetch())
    {
            echo "<p class='col'>";
            echo "<input type='checkbox' name='programmers[]' id='programmer".$row['programmer_id']."' value='".$row['programmer_id']."'>";
            echo "<a href='' class='help tip-below' data-tip='".projects($row['programmer_id'])."'><label for='programmer".$row['programmer_id']."'> ".$row['programmer_name']." ".$row['programmer_surname']."</label></a>";
            echo "</p>";
    }
?>
<input type="submit" value="Delete Programmer(s)" class="sub-del submit-btn">
    </form>