drrw8672 2013-08-22 20:35
浏览 50
已采纳

如何使用PHP在同一页面上使用表单提交更新先前的HTML输出

I am working on a form submission page (POST) that displays all current users (as checkbox objects at the beginning of the form), using file_get_contents() of an htpasswd file, and allows the user to update these users with the exec() function. This all works correctly, except for the fact that the user list displayed on the page is not updated after the user submits their changes via the form submit button. I know that the reason this happens is because the code to update the results occurs after the echo of the results.

So my question is: Is there any way to modify/update the output (with PHP) that would occur before the actual functions are executed? I have included my basic code structure to help visualize my situation:

form.php:

<form method="post">

<?php
    //Code to read htpasswd file, then output results as form input checkboxes
?>

<select name="action">
    <option value=""></option>
    <option value="delete"></option>
    <option value="change"></option>
    <option value="create"></option>
</select>

<input type="submit">

<?php
    if($_POST['action'] !== '')
    {
        switch($_POST['action'])
        {
            case delete:
            //run exec() command to delete each user

            case change:
            //run exec() command to change pw for each user

            case create:
            //run exec() command to create a user
        }
    }
?>

If this is not possible with PHP, will Javascript do the job?

  • 写回答

1条回答 默认 最新

  • drsdvwsvo78320812 2013-08-23 13:19
    关注

    With the help of j08691 and Ariane, I was able to get the desired outcome by swapping the code as mentioned:

    <form method="post">
    
    <?php
        if(isset($_POST['action']) && $_POST['action'] !== '')
        {
            switch($_POST['action'])
            {
                case delete:
                //run exec() command to delete each user
    
                case change:
                //run exec() command to change pw for each user
    
                case create:
                //run exec() command to create a user
            }
        }
    ?>
    
    <?php
        //Code to read htpasswd file, then output results as form input checkboxes
    ?>
    
    <select name="action">
        <option value=""></option>
        <option value="delete"></option>
        <option value="change"></option>
        <option value="create"></option>
    </select>
    
    <input type="submit">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建