dse3168 2018-05-23 13:21
浏览 62
已采纳

当选中该行的复选框时,在html表的特定行获取输入文本

I have a html table with check box on every row and when I click the checkboxes at a specific row, i am supposed to echo the input text using php. However, I can only get the input text for the first row. All the other rows give me a blank data. I dont understand whats wrong with my code

     <?php
     if(isset($_POST['submit']))
      {
         foreach($_POST['test'] as $key=>$value)
               { 
                  echo $_POST['tCode'][$key];
                 }
            }

        ?>
          <html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>


                <div style="width:48%; margin-left:440px">
    <table border="0px" align="center" class = "table" style="margin-right:530px;">
 <form name="MainForm" method="post" action=""  >
        <tr>
            <th style = "float:right">Tracking code:</th>
            <th>
               <td> <input type="textbox" name="tCode[]" id="1" ></td>
       <td> <input type="checkbox" name="test[]" id="name" value ="1" /> 
          </td>
            </th>
        </tr>
           <tr>
            <th style = "float:right" >Order Code: </th>
            <th>
                 <td> <input type="textbox" name="tCode[]" id="2" ></td>
          <td> <input type="checkbox" name="test[]" id="name" value ="2" /> 
           </td>
            </th>
            <th>
              </tr>
                 <tr>
                   <th style = "float:right" >product Code: </th>
               <th>
                      <td> <input type="textbox" name="tCode[]" id="3" > 
         </td>
             <td> <input type="checkbox" name="test[]" id="name1" value ="3"/></td>
            </th>
            <th>
                      </tr>
                <td></td>
             <td><input class="btn" type="submit" value="Submit" name = "submit" style="margin-left:50px" /></td>

When i tick the first checkbox, and type hello world in the first texbox, click submit, it is able to echo out "hello world", whereas for the other checkboxes, the inputted text is blank despite the textbox having value in it.

  • 写回答

1条回答 默认 最新

  • dongtan7351 2018-05-23 13:46
    关注

    OK, there's lots of things going on here that are worth addressing. Keep in mind this comes from the spirit of wanting to help you become a better coder. Not all of it applies to PHP, but all of the feedback will help you be a better coder in general.

    1. Do NOT use inline styles. They are frowned upon, they make the code messy / hard to diagnose, and they make maintenance very difficult.
    2. DO use a code editor that will hilight issues for you (I'm a huge fan of PHPStorm). The code had all kinds of mismatched html tags that I have cleaned up below.
    3. Do NOT use ids unnecessarily. On html inputs, there are only two purposes for using ids: (a) So you can address them with CSS styles, or (b) So you can access them easily with some javascript code. You were doing neither, so I stripped out all the ID's.
    4. DO format your code with proper spacing and indenting. Formatting makes it far easier to read and troubleshoot.
    5. DO put the PHP "key" inside the name of the inputs. This is one of the reasons you were having difficulties - the indexes did not match.
    6. DO use defensive coding (checking if something isset, is ! empty, or array_key_exists) before accessing an array element. Note I used array_key_exists below - that's because other methods can still return FALSE, even if it was set (but there was no value).

    The reason it didn't work is because checkboxes are only present in the $_POST superglobal if they are checked. Therefore, the indexes did not match like you expected them to. For example, checking ONLY the second box resulted in a $key of 1, NOT 2 as you would expect.

    I've updated the inputs, as well as the PHP, to work.

    The below code has been updated, and tested, and proven to work:

    <?php
    if( isset( $_POST[ 'submit' ] ) ) {
        // this is for debugging / testing.  Comment / remove as necessary
        var_dump( $_POST );
    
        // load ALL of the text inputs for convenient access in the loop
        $codes = $_POST['tCode'];    
    
        // test first to be sure ANY checkboxes were checked, to prevent notices
        if ( isset( $_POST[ 'test' ] ) ) {
            // loop over all posted checkboxes
             foreach( $_POST[ 'test' ] as $key => $on ) { 
                 // isset or ! empty can return FALSE if present, but empty value
                 if ( array_key_exists( $key, $codes ) ) {
                     echo '<br>The input for row ' . $key . ' is: ' . $codes[ $key ];   
                 }
             }
         }
    }
    ?>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>
            </title>
        </head>
        <body>
            <form name="MainForm" method="post" action="" >
                <table>
                    <tr>
                        <th>
                            Tracking code:
                        </th>
                        <td>
                            <input type="textbox" name="tCode[1]" >
                        </td>
                        <td>
                            <input type="checkbox" name="test[1]" />
                        </td>
                    </tr>
                    <tr>
                        <th style = "float:right" >
                            Order Code: 
                        </th>
                        <td>
                            <input type="textbox" name="tCode[2]" >
                        </td>
                        <td>
                            <input type="checkbox" name="test[2]" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            product Code: 
                        </th>
                        <td>
                            <input type="textbox" name="tCode[3]" >
                        </td>
                        <td>
                            <input type="checkbox" name="test[3]" />
                        </td>
                    </tr>
                </table>
                <input class="btn" type="submit" value="Submit" name="submit" />
            </form>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c