首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用PHP解析HTML表单数据

使用PHP解析HTML表单数据
EN

Stack Overflow用户
提问于 2018-10-24 06:19:24
回答 1查看 651关注 0票数 0

下面是我一直在运行的一个测试的代码。

<!DOCTYPE HTML">
<html>
<head>
      <title>Test Page</title>
</head>
<body>
      <form class="form-horizontal" method = "POST">
            <fieldset>
                   <table  class = "table table-striped table-sm">                 
                        <thead>
                            <tr> 
                                 <th style="width: 2%"> </th>
                                 <th style="width: 18%">Name</th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                            </tr>
                        </thead>
                        <tbody>       
                               <tr>
                                   <td><input type="hidden" name="memberid[]" value="1063" /></td> 
                                   <td>Charlie Farnsworth</td> 
                                   <td align="center"><input type="checkbox" name="test[]" value = "0"  ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "1" ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "2" ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "3" ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "4" ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "5" ></td>  
                               </tr>

                        </tbody>       
                   </table>
            </fieldset>
            <div align="center">
                <br /><br />
                <input type="submit" id= "submit" name = "submit" value="Save changes">
                <br /><br />
            </div>
      </form>
</body>
</html>

PHP代码是

<?php 
if (isset($_POST)) {
   $return = '000000';
   if (!empty($_POST['test'])) { 
     for ($x = 0; $x < 6; $x++) {
        if(IsChecked('test',$x)){
          $return=substr_replace($return,"1",$x,1);          } 
        }
     } 
     $return = implode(',',str_split($return));
     echo $_POST['memberid'].'<br/>';
     echo $return.'<br/><br/>';
 }
 unset($_POST);
}

function IsChecked($chkname,$value) {
    if(!empty($_POST[$chkname])){
        foreach($_POST[$chkname] as $chkval){
            if($chkval == $value){
                return true;
            }
        }
    }
    return false;
}

?>                 

这是为了允许我基于复选框状态构建一个1和0组成的字符串。这将用于更新具有隐藏输入值的id字段的SQL表记录。

这对于单行数据可以很好地工作,但是如果有多行数据,我就不知道如何做了。

我本以为foreach构造可能会起作用,但到目前为止还没有成功。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52958591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档