如何在Mysql上获取复选框值?

时间:2014-10-07 17:24:51

标签: php mysql

您好我将表单数据链接到我的mysql数据库服务器到目前为止这么好我在这里遇到一些问题

我的prcoes.php。代码:

$db_selected = mysql_select_db (DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' / mysql_error());
}

$value = $_POST['name'];
$value2 = $_POST['surname'];
$value3 = $_POST['email'];
$value4 = $_POST['phone'];
$value4 = $_POST['activity'];
$value5 = $_POST['ltype'];

使用复选框$_POST['ltype'];我在Mysql上只获得Array作为结果? 通过将表单数据发送到我的电子邮件,我可以使用此代码获得正确的值:

'LType : ' . implode(',', $_POST['ltype']). "\n" . 

提前感谢任何帮助  在这里更新我的chekbox代码:

                                 <div class="thumb1" >
                                        <label for="word"  ><img class="img" src="images/my1.jpg"  /></label>
                                        <input type="checkbox" class="chk"   name="ltype[]" id="word" value="word"  /><hr> <p><strong>Word Mark Logo</strong></p>
                                   </div>

                                   <div class="thumb1" >
                                         <label for="letter"><img class="img" src="images/my2.jpg"  /></label>
                                         <input type="checkbox" class="chk"   name="ltype[]"  id="letter" value="letter" /><hr> <p><strong>Letter Mark Logo</strong></p>
                                   </div>

                                   <div class="thumb1">
                                          <label for="emblerm"><img class="img" src="images/my3.jpg"  /></label>
                                          <input type="checkbox" class="chk"   name="ltype[]" id="emblerm" value="emblerm" /><hr> <p><strong>Emblerm Logo</strong></p>
                                   </div>

2 个答案:

答案 0 :(得分:1)

取决于您如何定义复选框

<form action='XXX.php' method='POST'>
  Football: <input type="checkbox" name="sports[]" value="football"  />
  Baseball: <input type="checkbox" name="sports[]" value="baseball"  />
</form>

在XXX.php上它以数组

返回
if ( $_POST['sports'] ) {
  $arySports = $_POST['sports'];
  foreach( $arySports AS $value ) {
    echo $value ."<br>";
  }
}

如果你做了

Subscribe Now <input type="checkbox" name="subscribe" />

在XXX.php上,它返回

如果你做了

Subscribe Now <input type="checkbox" name="subscribe" value="now" />

然后它返回字符串&#34;现在&#34;。所以试试这个,看看你想要的复选框

<form action="XXX.php" method="POST">
  <input type='checkbox' name='A'/>
  <input type='checkbox' name='B' value='this is array'/>
  <input type='checkbox' name='B' value='the second one'/>
  <input type='checkbox' name='C[]' value='A'/>
  <input type='checkbox' name='C[]' value='B'/>
  <input type='checkbox' name='C[]' value='C'/>
  <input type='checkbox' name='D' value='string'/>
</form>
XXX.php上的

print_r( $_POST['A'] );
echo "<br>";
print_r( $_POST['B'] );
echo "<br>";
print_r( $_POST['C'] );
echo "<br>";
print_r( $_POST['D'] );
echo "<br>";

答案 1 :(得分:1)

你为什么不:

$value5 = implode(',', $_POST['ltype']);

如果你想让数组返回只是爆炸数据库值。