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 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 就很莫名其妙,本来正常的Excel,突然变成了这种一格一页
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?