Dar*_*jda 43 php arrays variables checkbox
我在这里看了几个例子,但是很多例子要么太先进以至于我对PHP的掌握还是他们的例子对于他们自己的项目来说太具体了.我目前正在努力学习PHP表单的一个非常基本的部分.
我正在尝试创建一个带有几个复选框的表单,每个复选框都分配了不同的值,我希望将它们发送到一个变量(数组?),我可以稍后回显/使用,在我的情况下,我将发送检查的值一封电邮.
到目前为止,我尝试了一些变化,但我最接近它的是......
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar' value='Option Three'>3
</td>
</tr>
</table>
<input type='submit' class='buttons'>
</form>
<?php
$checkboxvar[] = $_REQUEST['checkboxvar'];
?>
Run Code Online (Sandbox Code Playgroud)
在哪里我将$ checkboxvar []发送到我的电子邮件中.我完全错了吗?我的另一个想法是使用很多if语句.
cry*_*c ツ 74
<form method='post' id='userform' action='thisform.php'> <tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>3
</td> </tr> </table> <input type='submit' class='buttons'> </form>
<?php
if (isset($_POST['checkboxvar']))
{
print_r($_POST['checkboxvar']);
}
?>
Run Code Online (Sandbox Code Playgroud)
您将表单名称作为数组传递,然后您可以使用var本身访问所有选中的复选框,然后它将是一个数组.
要将选中的选项回显到您的电子邮件中,您可以这样做:
echo implode(',', $_POST['checkboxvar']); // change the comma to whatever separator you want
Run Code Online (Sandbox Code Playgroud)
请记住,您应该根据需要始终清理您的输入.
为了记录,存在关于此的官方文档:http://php.net/manual/en/faq.html.php#faq.html.arrays
小智 20
[]
在输入标记中添加属性的名称
<form action="" name="frm" method="post">
<input type="checkbox" name="hobby[]" value="coding"> coding  
<input type="checkbox" name="hobby[]" value="database"> database  
<input type="checkbox" name="hobby[]" value="software engineer"> soft Engineering <br>
<input type="submit" name="submit" value="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
对于PHP代码:
<?php
if(isset($_POST['submit']){
$hobby = $_POST['hobby'];
foreach ($hobby as $hobys=>$value) {
echo "Hobby : ".$value."<br />";
}
}
?>
Run Code Online (Sandbox Code Playgroud)
试试这个,通过 for 循环
<form method="post">
<?php
for ($i=1; $i <5 ; $i++)
{
echo'<input type="checkbox" value="'.$i.'" name="checkbox[]"/>';
}
?>
<input type="submit" name="submit" class="form-control" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$check=implode(", ", $_POST['checkbox']);
print_r($check);
}
?>
Run Code Online (Sandbox Code Playgroud)