duan47676379 2018-10-15 06:21
浏览 79
已采纳

PHP - 无法从HTML表单提交POST值提交[重复]

This question already has an answer here:

I am receiving this error (4 times) when attempting to submit a form table:

Notice: Undefined index: accountid in php/ChartOfAccountsInclude.php on >line 106

This is line 106 in my code

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

I am attempting to load in table data from a local database. Then I allow the user access to certain fields to change the values. Once they are done they press the button to submit and that is when I get the above error. There are currently four accounts in the database which means each time my code is looping through the POST value it is failing to retrieve the accountid. I have pasted the relevant code below:

User Page ChartOfAccounts.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 Include ChartOfAccountsInclude.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");
        }
    }
?>
</div>
  • 写回答

4条回答 默认 最新

  • dtsc14683 2018-10-15 06:51
    关注

    In HTML Elements with Disabled attribute are not posted or you can say their values are not submitted.

    Your input element is as below so it will not post any value.

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

    Disabled controls Limitations:

    • it will not receive focus.

    • it will be skipped in tabbing navigation.

    • It cannot be successfully posted.

    So if you dont want let user edit that input and if it is not neccessary to show that input to users then you can try input type="hidden" as below:

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

    Or If you want to show the id to user but not want to let them edit then you can use readonly attribute in your case, by readonly attribute you will be able to post your field's data, see below for example:

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

    Read-only elements

    • can receive focus but cannot be modified by the user.
    • it will included in tabbing navigation.
    • it will be successfully posted.
    • doesn't work on checkboxes and select tags

    Reference answer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 主要进行描述非满管状态下,管路的摩阻系数是怎么变化的,在管路长度方向上是怎么分布的(标签-matlab)
  • ¥15 multisim设计求解如图
  • ¥15 动态多目标优化,求解定义的问题的POF和POS公式
  • ¥30 实验 :UML2.0 行为建模
  • ¥15 设计一种优化调度算法并结合论文中的案例给出调度计划
  • ¥15 py文件打包exe报错TypeError:expected str,bytes or os.PathLike object,not_io.BytesIO
  • ¥15 Java编写学籍信息管理系统
  • ¥15 IPXE网刻客户机找不到服务端
  • ¥15 小贝360-4 配二个 华772S 设置WⅰFi5G 连接
  • ¥15 vs2022的QT报错,好像是缺少winextras