【问题标题】:Checkbox Table VALUE , NAME and ID in PHPPHP中的复选框表VALUE,NAME和ID
【发布时间】:2016-08-18 12:06:02
【问题描述】:

我有一个复选框表,我想将它们保存在我的数据库中,我需要将它们保存在每个人的名称和值中,并作为记录:

 <tr><td><input class="perm"  name="feature[]"  type="checkbox" value="1" id="feat1"/>test1 </td>
    <td><input  class="perm"  name="feature[]" type="checkbox" value="1"  id="feat9"/>test2 Monats </td>
    <td><input  class="perm"  name="feature[]" type="checkbox" value="1"  id="feat17"/>test3m</td>
    <td><input  class="perm"  name="feature[]" type="checkbox" value="1"  id="feat25"/>test4</td>
</tr>
<tr class="visible-xs" aria-hidden="true">
  <td>&nbsp;</td>
</tr>
<tr><td> <input class="perm" name="feature[]"  type="checkbox" value="1"  id="feat2"/>test5</td>
    <td><input  class="perm"  name="feature[]" type="checkbox" value="1"  id="feat10"/>test6n</td>
    <td><input  class="perm" name="feature[]"  type="checkbox"  value="1" id="feat18"/>test7</td>
    <td><input  class="perm"  name="feature[]" type="checkbox"  value="1" id="test8</td>
</tr>

还有我的 PHP 代码:

$checkBox[] =  implode(',',$_GET['feature']);


for($i=0; $i<sizeof($checkBox ); $i++){

$tsql="INSERT INTO [dbo].[_PARAM](Value) VALUES ('" . $checkBox[$i] . "')";     

$stmt = sqlsrv_query( $conn, $tsql);
}

但我的数据库有 2 列 Name , Value,例如。如果 test1 和 test2 检查了

我需要桌子像

Name      Value

test1      1
test2      1
test3     null
test4     null
 .....    ....

我需要所有的测试,只有值 1 用于检查的测试。

【问题讨论】:

  • test1 , test2 是值吗?如果是,那么你为什么要使用 1, 1, 1, ?
  • @devpro 不,它们是名称,它们可能是其他任何东西,我只需要知道复选框是否选中,因此它可能是 1 或 X 或其他任何东西
  • 并且复选框已修复?总是静态的?
  • @G.Margaritis ya 它可以工作,但在数据库中我不仅得到 1 值,还得到选定复选框的 0。所以每个复选框都有 2 个值,第一个是 1,它被选中,另一个是隐藏值 0。

标签: php html sql-server


【解决方案1】:

为每个值为“0”(零)的复选框保留一个隐藏字段,如下所示:

<tr><td><input class="perm"  name="feature[]"  type="hidden" value="0" /><input class="perm"  name="feature[]"  type="checkbox" value="1" id="feat1"/>test1 </td>
<td><input class="perm"  name="feature[]"  type="hidden" value="0" /><input  class="perm"  name="feature[]" type="checkbox" value="1"  id="feat9"/>test2 Monats </td>
<td><input class="perm"  name="feature[]"  type="hidden" value="0" /><input  class="perm"  name="feature[]" type="checkbox" value="1"  id="feat17"/>test3m</td>
<td><input class="perm"  name="feature[]"  type="hidden" value="0" /><input  class="perm"  name="feature[]" type="checkbox" value="1"  id="feat25"/>test4</td>

$checkBox[] =  implode(',',$_GET['feature']);

for($i=0; $i<sizeof($checkBox ); $i++){
    if ($checkBox[$i] == 0) {
        $checkBox[$i] = null;
    }
 $name = "test".($i+1);
$tsql="INSERT INTO [dbo].[_PARAM](Name, Value) VALUES ('".$name."', '" . $checkBox[$i] . "')";     

$stmt = sqlsrv_query( $conn, $tsql);
}

【讨论】:

    【解决方案2】:

    您需要更改值,否则 $_GET['feature'] 将返回一个 1 的数组

     <tr><td><input class="perm"  name="feature[]"  type="checkbox" value="1" id="feat1"/>test1 </td>
    <td><input  class="perm"  name="feature[]" type="checkbox" value="2"  id="feat9"/>test2 Monats </td>
    <td><input  class="perm"  name="feature[]" type="checkbox" value="3"  id="feat17"/>test3m</td>
    <td><input  class="perm"  name="feature[]" type="checkbox" value="4"  id="feat25"/>test4</td>
    

    编辑

    input type=hidden value="1,2,3,4,..,n" name="available">
    

    比在php中

        $available=explode($_GET['available']); 
       foreach($available as $element) { 
       $values[$element]=in_array($element,$_GET['feature']) ? 1 : 0; 
       }
    

    【讨论】:

    • 如果复选框被选中,我需要值为 1
    • 如果您需要在数据库中为所有选项保存 1 和 0,而不是还需要在隐藏元素中发送所有选项,例如: 而不是在 php 中执行类似的操作: $available=explode($_GET['available']); foreach($available as $element) { $values[$element]=in_array($element,$_GET['feature']) ? 1:0; } 而不是基于值插入
    【解决方案3】:

    如果您的复选框是静态的,那么您可以使用包含复选框值的数组。

    示例:

    HTML:

    <form method="post" action="">
     <tr><td><input class="perm"  name="feature[]"  type="checkbox" value="test1" id="feat1"/>test1</td>
        <td><input  class="perm"  name="feature[]" type="checkbox" value="test2"  id="feat9"/>test2</td>
        <td><input  class="perm"  name="feature[]" type="checkbox" value="test3"  id="feat17"/>test3</td>
        <td><input  class="perm"  name="feature[]" type="checkbox" value="test4"  id="feat25"/>test4</td>
    </tr>
    <input type="submit" name="submti">
    </form>
    

    PHP:

    <?php
    if(isset($_POST['submti'])){
        // all checkbox values
        $checkboxArr = array('test1','test2','test3','test4');
    
        if(is_array($_POST['feature'])){
            foreach ($checkboxArr as $value) {
                // check if value exist in post
                if(in_array($value, $_POST['feature'])){
                    $newArr[$value] = 1; // if exist
                }
                else{
                    $newArr[$value] = 0; // if not exist
                }
            }
        }
    
        echo "<pre>";
        print_r($newArr);
    }
    ?>
    

    结果:

    Array
    (
        [test1] => 1
        [test2] => 1
        [test3] => 0
        [test4] => 0
    )
    

    现在您可以根据需要将此数组用于查询。

    【讨论】:

      【解决方案4】:

      首先,您必须将 td 元素(例如 test1、test2)的内容设置为复选框的值。您还需要每个复选框的隐藏输入字段和值字段中的指示符(0 或1)关于复选框是否被选中,它将作为其余 value 属性的前缀。当复选框未选中时,表单将作为隐藏字段的值传递,而当复选框被选中时,表单将作为值传递复选框的值,覆盖隐藏字段的值。

      <tr>
      <td><input type="hidden" name="feature[0]" value="0test1" /><input class="perm"  name="feature[0]"  type="checkbox" value="1test1" id="feat1"/>test1 </td>
          <td><input type="hidden" name="feature[1]" value="0test2  Monats" /><input  class="perm"  name="feature[1]" type="checkbox" value="1test2 Monats"  id="feat9"/>test2 Monats </td>
          <td><input type="hidden" name="feature[2]" value="0test3m" /><input  class="perm"  name="feature[2]" type="checkbox" value="1test3m"  id="feat17"/>test3m</td>
          <td><input type="hidden" name="feature[3]" value="0test4" /><input  class="perm"  name="feature[3]" type="checkbox" value="1test4"  id="feat25"/>test4</td>
      </tr>
      <tr class="visible-xs" aria-hidden="true">
        <td>&nbsp;</td>
      </tr>
      <tr><td><input type="hidden" name="feature[4]" value="0test5" /><input class="perm" name="feature[4]"  type="checkbox" value="1test5"  id="feat2"/>test5</td>
          <td><input type="hidden" name="feature[5]" value="0test6n" /><input  class="perm"  name="feature[5]" type="checkbox" value="1test6n"  id="feat10"/>test6n</td>
          <td><input type="hidden" name="feature[6]" value="0test7" /><input  class="perm" name="feature[6]"  type="checkbox"  value="1test7" id="feat18"/>test7</td>
          <td><input type="hidden" name="feature[7]" value="0test8" /><input  class="perm"  name="feature[7]" type="checkbox"  value="1test8" id="feat8"/>test8</td>
      </tr>
      

      你的 php 代码应该是这样的:

      foreach($_GET["feature"] as $field)
      {
          $name = substr($field, 1);
          $value = substr($field, 0,1) == 1 ? 1 : null;
          $tsql="INSERT INTO [dbo].[_PARAM](Name,Value) VALUES ('$name','$value')";     
          $stmt = sqlsrv_query( $conn, $tsql);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-28
        • 1970-01-01
        • 1970-01-01
        • 2011-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多