我正在尝试打印多个复选框。我希望当用户选中一个或多个复选框时,该表单将显示用户选择的内容。例如,如果用户检查了鞋1,鞋2,鞋3,则将打印鞋1,鞋2,鞋3。如果用户检查shoe1和shoe2,它将打印shoe1和shoe2。如果用户选中shoe1,则只会打印shoe1,依此类推。
我曾尝试使用if,elseif,else语句,但我认为我的逻辑很混乱,因为每次我尝试运行它时它们都不起作用。
我尝试运行此代码,但未打印:
if(($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['nike'] == 'nike') && ($_POST['adidas'] == 'adidas') && ($_POST['rl'] == 'rl') && ($_POST['vans'] == 'vans') &&
($_POST['ck'] == 'ck') && ($_POST['kc'] == 'kc') && ($_POST['rband1'] == 'rband1') && ($_POST['rband2'] == 'rband2')) {
echo "<strong>Brands chosen:</strong> " . $_POST['nike'] . ", " . $_POST['adidas'] . ", " . $_POST['rl'] . ", " . $_POST['vans'] . ", " . $_POST['ck'] . ", " . $_POST['kc'] . ", " . $_POST['rband1'] . ", " . $_POST['rband2'] . "<br /> <br />";
}
这是我的代码:
What clothing brands do you like?<br />Nike<input type="checkbox" name="nike" value="nike"
<?php
if ($_POST['nike'] == 'nike') {
echo "checked";
}
?>
/>
Adidas<input type="checkbox" name="adidas" value="adidas"
<?php
if ($_POST['adidas'] == 'adidas') {
echo "checked";
}
?>
/>
Polo Ralph Lauren<input type="checkbox" name="rl" value="rl"
<?php
if ($_POST['rl'] == 'rl') {
echo "checked";
}
?>
/>
Vans<input type="checkbox" name="vans" value="vans"
<?php
if ($_POST['vans'] == 'vans') {
echo "checked";
}
?>
/>
<br />
Calvin Klein<input type="checkbox" name="ck" value="ck"
<?php
if ($_POST['ck'] == 'ck') {
echo "checked";
}
?>
/>
Kenneth Cole<input type="checkbox" name="kc" value="kc"
<?php
if ($_POST['kc'] == 'kc') {
echo "checked";
}
?>
/>
Rock Brand 1<input type="checkbox" name="rband1" value="rband1"
<?php
if ($_POST['rband1'] == 'rband1') {
echo "checked";
}
?>
/>
Rock Brand 2<input type="checkbox" name="rband2" value="rband2"
<?php
if ($_POST['rband2'] == 'rband2') {
echo "checked";
}
?>
/>
我希望用户看到他们检查过的内容,以便于用户使用。我也不太确定是否将所有这些变量都放在一个类中,这样会更容易。
答案 0 :(得分:0)
一个非常简单的演示-基于不同的方法(在上面的注释中采用的方法),该方法使用复选框组的单个名称,但使用数组语法命名(即:name[]
),以方便进行更简单的检查提供的数据。它还使用PHP通过一个简单的数组来确定布局,从而生成所有重要的html,这意味着可以很容易地添加新的部分和/或make。
<?php
$sections=array(
'premier' => array('nike','adidas','polo ralph lauren','vans'),
'excellent' => array('calvin klein','kenneth cole','rock brand 1','rock brand 2'),
/* easy to extend */
'good' => array('levi','wrangler','pierre cardin','north face','superdry'),
'average' => array('benetton','robe di kappa','leathernun','burton','animal')
);
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>form checkboxes</title>
<style>
body *{box-sizing:border-box;padding:0;margin:0;font-family:cursive;}
fieldset{width:80%;margin:0.25rem auto;padding:1rem;border:1px solid rgba(133,133,133,0.1)}
label{display:block;width:80%;padding:0.25rem;margin:auto;float:none;}
label:before{content:attr(for);text-transform:capitalize;}
[type='checkbox']{float:right}
legend{padding:0.5rem;width:20%;border:1px solid rgba(133,133,133,0.1); background:whitesmoke;text-transform:capitalize}
</style>
</head>
<body>
<form method='post'>
<h1>What clothing brands do you like?</h1>
<?php
/* iterate through the config array and generate a section for each array */
foreach( $sections as $key => $arr ){
echo "<fieldset id='$key'><legend>$key</legend>";
/* iterate through each child array of makes */
foreach( $arr as $name ){
/* was the checkbox checked? */
$checked = !empty( $_POST['make'] ) && in_array( $name, $_POST['make'] ) ? 'checked' : '';
/* print the label/checkbox */
printf(
'<label for="%s"><input type="checkbox" name="make[]" value="%s" %s/></label>',
$name,
$name,
$checked
);
}
echo "</fieldset>";
}
?>
<input type='submit' />
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' )printf('<pre>%s</pre>',print_r($_POST,true));
?>
</form>
</body>
</html>