如何从已填充的表单中获取信息

时间:2017-03-22 15:21:04

标签: php mysql

我正在尝试使用提交按钮更新数据库记录,但是从中获取信息的表单本身已由另一个提交按钮填充。

PHP如下:

if (isset($_POST['edit']))
{
    $editUser = User::locate($_POST['userSelect']);
    $setCompany = Company::findCompany();
    $exCompanyId = $editUser->user_company_id;
    $isAdmin = $editUser->user_admin;
    $eUserFirstname = $editUser->user_firstname;
    $eUserLastname = $editUser->user_lastname;
    $eUserUsername = $editUser->user_username;
    $eUserEmail = $editUser->user_email;
    $eUserCompany = $editUser->user_company;
    $adminArray = array("Yes","No");
    if ($exCompanyId > NULL)
    {
        $exCompany = Company::locateId($exCompanyId);
        $companyValue = $exCompany->company_name;
    }
    else
    {
        $companyValue = "";
    }
    if ($isAdmin > 0)
    {
        $userAdmin = $adminArray[0];

    }
    else
    {
        $userAdmin = $adminArray[1];
    }
}
if (isset($_POST['confirm']))
{
    $updateUserRecord = User::locate($eUserUsername);
    $newCompanyValue = $_POST['setCompany'];
    $isAdmin = $_POST['userAdmin'];
    $newCompany = Company::locate($newCompanyValue);
    $companyId = $newCompany->company_id;

    $updateUserRecord->user_company_id = $companyId;
    $updateUserRecord->user_admin = $isAdmin;

    $editUser->updateUserAdmin();

    $message = "You have successfully updated the user account";
    echo "<script type='text/javascript'>alert('$message');</script>";
}

HTML代码如下:

<form action="" method="post">
            <p>
                <label>First Name:</label>
                <label><?php echo $eUserFirstname ?></label>
            </p>
            <p>
                <label>Last Name:</label>
                <label><?php echo $eUserLastname ?></label>
            </p>
            <p>
                <label>Username:</label>
                <label><?php echo $eUserUsername ?></label>
            </p>
            <p>
                <label>Email Address:</label>
                <label><?php echo $eUserEmail ?></label>
            </p>
            <p>
                <label>User Company:</label>
                <label><?php echo $eUserCompany ?></label>
            </p>
            <p>
                <label>Set Company:</label>
                <select name="setCompany">
                    <option><?php echo $companyValue ?></option>
                    <?php
                    foreach ($setCompany as $srow)
                    {
                        ?>
                        <option id="<?=
                        $srow->company_id
                        ?>">
                            <?=
                            $srow->company_name
                            ?>
                        </option>
                        <?php
                    }
                    ?>
                </select>
            </p>
            <p>
                <label>Administrator:</label>
                <select name="userAdmin">
                    <option><?php echo $userAdmin ?></option>
                    <?php
                    foreach ($adminArray as $arow)
                    {
                        ?>
                        <option>
                            <?=
                            $arow
                            ?>
                        </option>
                        <?php
                    }
                    ?>
                </select>
            </p>
            <input type="submit" name="cancel" value="Cancel">
            <input type="submit" name="confirm" value="Confirm">
            </br>
        </form>

从我的调查到目前为止,变量都没有转移到第二个if语句,我不知道如何使它们可用。

1 个答案:

答案 0 :(得分:1)

将第一个表单的提交存储在echo语句旁边的隐藏输入字段中。例如:<input type="hidden" name="field_name" value="<?php echo htmlspecialchars($field); ?>">

或者,也可以在首次表单发布后更新数据库。