我有一个表格,分为不同的日子,从周一到周四。
大多数日子包含两个班级,每个班级有 2 个不同的可用时间(有些只包含 1 个)?
您可以使用多维数组来做到这一点:
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
<form method="POST" action="">
<fieldset style="border:#00FFFF 3px solid">
<legend>Monday</legend>
Yoga Class: (Becc)<br>
<input type="checkbox" name="classtimes[Yoga][monday][]" value="9am-11am">9am-11am<br>
<input type="checkbox" name="classtimes[Yoga][monday][]" value="12pm-3pm">12pm-3pm<br>
<br>
Muay Thai Fitness: (Royce)<br>
<input type="checkbox" name="classtimes[Muay Thai][monday][]" value="8am-10am">8am-10am<br>
<input type="checkbox" name="classtimes[Muay Thai][monday][]" value="3pm-6pm">3pm-6pm<br>
</fieldset>
<fieldset style="border:#00FFFF 3px solid">
<legend>Tuesday</legend>
Yoga Class: (Becc)<br>
<input type="checkbox" name="classtimes[Yoga][tuesday][]" value="9am-11am">9am-11am<br>
<input type="checkbox" name="classtimes[Yoga][tuesday][]" value="12pm-3pm">12pm-3pm<br>
<br>
Muay Thai Fitness: (Royce)<br>
<input type="checkbox" name="classtimes[Muay Thai][tuesday][]" value="8am-10am">8am-10am<br>
<input type="checkbox" name="classtimes[Muay Thai][tuesday][]" value="3pm-6pm">3pm-6pm<br>
</fieldset>
<input type="submit">
</form>
输出将是:
Array
(
[classtimes] => Array
(
[Yoga] => Array
(
[monday] => Array
(
[0] => 9am-11am
)
[tuesday] => Array
(
[0] => 9am-11am
)
)
[Muay Thai] => Array
(
[monday] => Array
(
[0] => 8am-10am
[1] => 3pm-6pm
)
[tuesday] => Array
(
[0] => 8am-10am
[1] => 3pm-6pm
)
)
)
)
<fieldset style="border:#00FFFF 3px solid">
<legend>Tuesday</legend>
Yoga Class: (Becc)<br>
<input type="checkbox" name="classtimes[yoga][]" value="9am-11am">9am-11am<br>
<input type="checkbox" name="classtimes[yoga][]" value="12pm-3pm">12pm-3pm<br>
<br>
Muay Thai Fitness: (Royce)<br>
<input type="checkbox" name="classtimes[muay][]" value="8am-10am">8am-10am<br>
<input type="checkbox" name="classtimes[muay][]" value="3pm-6pm">3pm-6pm<br>
</fieldset>
然后在你的 PHP 中:
foreach($_POST["classtimes"]["yoga"] as $classtimes){
echo $classtimes;
}
当我做这种事情时,我通常会在复选框名称中添加一些内容,以提供其他信息,例如:
Yoga Class: (Becc)<br>
<input type="checkbox" name="0classtimes0" value="9am-11am">9am-11am<br>
<input type="checkbox" name="0classtimes0" value="12pm-3pm">12pm-3pm<br>
<br>
Muay Thai Fitness: (Royce)<br>
<input type="checkbox" name="0classtimes1" value="8am-10am">8am-10am<br>
<input type="checkbox" name="0classtimes1" value="3pm-6pm">3pm-6pm<br>
这样你就有了:第一个数字是星期几的索引,第二个数字是所述日期的类的索引。
您可以使用以下方式捕获 JQuery 中的变量:
$("input:checked").each(function(){
var id=$(this).attr("name"); //this gives you the XcasstimesY
var value=$(this).val(); //this gives you the value(time) of the checked value
//here you can get this info into an array, or any other variable you want to use to submit it!
});
当您在 PHP 中收到此信息时,假设您使用的是 POST:
$x=0;
while($x<6) //6 days
$y=0;
while(isset($_POST[$x.'casstimes'.$y])){
echo "On day $x class $y the selected time was".$_POST[$x.'casstimes'.$y];
$y++;
}
$x++;
}