无法从html表单submit访问post值

thtygnil  于 2021-06-18  发布在  Mysql
关注(0)|答案(4)|浏览(332)

这个问题在这里已经有答案了

禁用的表单字段不提交数据[重复](4个答案)
两年前关门了。
我在尝试提交表单表时收到此错误(4次):
注意:未定义的索引:php/chartofcountsinclude.php中的accountid位于>第106行
我的代码是106行

$account_id = $_POST["accountid"][$i];

我正试图从本地数据库加载表数据。然后我允许用户访问某些字段来更改值。一旦他们完成,他们按下按钮提交,这是当我得到上述错误。数据库中目前有四个帐户,这意味着每次我的代码通过post值循环时,它都无法检索accountid。我已将相关代码粘贴到下面:
用户页chartofcounts.php

<?php
    include("view/SiteHeader.php");
    include('php/ChartOfAccountsInclude.php');
?>

<html>
    <head>
        <title>Chart of Accounts</title>
    </head>

    <body>
        <br/>
        <h2>Chart of Accounts</h2>
        <form action="" method="post">
            <div>
                <?php retrieveChart() ;?>
            </div>

            <div class="container">
                <button type="submit" name="submitMods" class="btn btn-outline-secondary">Submit Account Modifications</button>
            </div>
        </form>
        <?php if($submit_err) : ?>
            <?php generateError('warning', 'Account could not be updated.') ?>
        <?php endif; ?>
    </body>
</html>

php包括chartofcountsinclude.php

<?php
    include_once('php/session.php');
    $sql = "SELECT * FROM Accounts";
    $result = mysqli_query($db,$sql);
    $count = mysqli_num_rows($result);
    $submit_err = FALSE;
    $submitaccount_err = FALSE;

    function retrieveChart() {
        global $db;
        $funcSQL = "SELECT * FROM Accounts";
        $funcResult = mysqli_query($db,$funcSQL);

        echo '<table border="1" class="table table-bordered table-hover">
        <thead>
        <tr>
            <th>Account ID</th>
            <th>Account Name</th>
            <th>Date Created</th>
            <th>Type</th>
            <th>Term</th>
            <th>Status</th>
            <th>Created By</th>
        </tr>
        </thead>';

        while($row = mysqli_fetch_array($funcResult))
        {
            $current = $longterm = $active = $inactive = $debi = $credit = $asset = $expense = $liability = $equity = $revenue = "";
            $curusr = $row['user_id'];
            $getUsernameSQL = "SELECT username FROM User_accounts WHERE user_id = $curusr";
            $usernameResult = mysqli_query($db,$getUsernameSQL);
            $usernameRow = mysqli_fetch_array($usernameResult);

            switch ($row['term']) {
                case 'Current':
                    $current = "selected = \"selected\"";
                    break;
                case 'Long Term':
                    $longterm = "selected = \"selected\"";
                    break;
            }
            switch ($row['account_status']) {
                case 'Active':
                    $active = "selected = \"selected\"";
                    break;
                case 'Inactive':
                    $inactive = "selected = \"selected\"";
                    break;
            }
            switch ($row['type']) {
                case 'Asset':
                    $asset = "selected = \"selected\"";
                    break;
                case 'Expense':
                    $expense = "selected = \"selected\"";
                    break;
                case 'Liability':
                    $liability = "selected = \"selected\"";
                    break;
                case 'Equity':
                    $equity = "selected = \"selected\"";
                    break;
                case 'Revenue':
                    $revenue = "selected = \"selected\"";
                    break;
            }

            echo '<tr>';
                echo '<td> <input name="accountid[]" disabled value="' . $row['account_id'] . '"> </td>';
                echo '<td> <input name="accountname[]" value="' . $row['account_name'] . '"> </td>';
                echo '<td> <input name="datecreated[]" disabled value="' . $row['date_created'] . '"> </td>';
                echo '<td> 
                        <select name="type[]">
                            <option '.$asset.'>Asset</option>
                            <option '.$expense.'>Expense</option>
                            <option '.$liability.'>Liability</option>
                            <option '.$equity.'>Equity</option>
                            <option '.$revenue.'>Revenue</option>
                        </select>
                    </td>';
                echo '<td> 
                        <select name="term[]">
                            <option '.$current.'>Current</option>
                            <option '.$longterm.'>Long Term</option>
                        </select>
                    </td>';
                echo '<td> 
                        <select name="accountstatus[]" value="' . $row['account_status'] . '">
                            <option '.$active.'>Active</option>
                            <option '.$inactive.'>Inactive</option>
                        </select>
                    </td>';
                echo '<td>
                        <input name="user_id[]" disabled value="' . $usernameRow['username'] . '">
                    </td>';
            echo '</tr>';
        } 
        echo '</table>';
    }

    if( isset($_POST['submitMods']) ) {
        $i = 0;

        while($i < $count) {
            $account_id = $_POST["accountid"][$i];
            $account_name = mysqli_real_escape_string($db,$_POST['accountname'][$i]);
            $type = mysqli_real_escape_string($db,$_POST['type'][$i]);
            $term = mysqli_real_escape_string($db,$_POST['term'][$i]);
            $account_status = mysqli_real_escape_string($db,$_POST['accountstatus'][$i]);

            $updateAccountsql = "
                UPDATE Accounts
                SET
                    account_id = '$account_id',
                    account_name = '$account_name',
                    type = '$type',
                    term = '$term',
                    account_status = '$account_status'
                WHERE account_id = '$account_id'";

            if($account_name == "") {
                $submit_err = TRUE;
            } else {
                $updateResult = mysqli_query($db,$updateAccountsql);
            }
            $i++;
        }

        if(!$submit_err) {
            //header("Location: ChartOfAccounts.php");
        }
    }
?>
ctrmrzij

ctrmrzij1#

远离的 disabled 从输入字段
替换

echo '<td> <input name="accountid[]" disabled value="' . $row['account_id'] . '"> </td>';
echo '<td> <input name="accountname[]" value="' . $row['account_name'] . '"> </td>';
echo '<td> <input name="datecreated[]" disabled value="' . $row['date_created'] . '"> </td>';

echo '<td> <input name="accountid[]" value="' . $row['account_id'] . '"> </td>';
echo '<td> <input name="accountname[]" value="' . $row['account_name'] . '"> </td>';
echo '<td> <input name="datecreated[]" value="' . $row['date_created'] . '"> </td>';

如果你不允许人们编辑你可以使用的字段 readonly 代替 disabled .

wrrgggsh

wrrgggsh2#

在html中,具有禁用属性的元素不会被发布,或者您可以说它们的值不会被提交。
您的输入元素如下所示,因此它不会发布任何值。

<input name="accountid[]" value="' . $row['account_id'] . '" disabled />

禁用控件限制:
它不会被聚焦。
在选项卡导航中将跳过它。
无法成功过帐。
因此,如果您不想让用户编辑该输入,并且不需要向用户显示该输入,那么您可以尝试 input type="hidden" 具体如下:

<input type="hidden" name="accountid[]" value="' . $row['account_id'] . '" />

或者,如果您想向用户显示id,但不想让他们编辑,则可以在您的情况下使用readonly属性,通过readonly属性,您将能够发布字段的数据,例如,请参见下面的:

<input type="text" name="accountid[]" value="' . $row['account_id'] . '" readonly />

只读元素
可以接收焦点,但用户无法修改。
它将包含在选项卡导航中。
将成功发布。
不适用于复选框和选择标记
参考答案

jdzmm42g

jdzmm42g3#

因为accountid是禁用字段,所以它不会提交,也不存在于$\u post变量中,所以您需要用隐藏字段替换您的文本框

bvuwiixz

bvuwiixz4#

线下更改

<input name="user_id[]" disabled value="' . $usernameRow['username'] . '">

<input name="user_id[]" type="hidden" value="' . $usernameRow['username'] . '">

相关问题