如果选择了第二个选项,我想在同一页面上显示一个复选框,请帮我解决这个问题。
这是我目前的代码:
<form action="form1.php" method="post">
<?php
$company = array (1 => 'One', 'Two', 'Three');
echo '<fieldset>
<select name="companys">';
foreach ($company as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';
if ($company == 2) {
echo'<p><input type="checkbox" name="tandc" value="terms" />I accept the terms and conditions
</p>';
} else {
echo 'OK';
};
echo '</fieldset>';
?>
</form>
答案 0 :(得分:1)
假设你在某处发布了表单,当前页面是form1.php。
<form action="form1.php" method="post">
<?php
$company = array(1 => 'One', 2 => 'Two', 3 => 'Three');
echo '<fieldset>
<select name="companys">';
foreach ($company as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';
if (isset($_POST['companys']) && $_POST['companys'] == 2) {
echo'<p><input type="checkbox" name="tandc" value="terms" />I accept the terms and conditions</p>';
} else {
echo 'OK';
}
echo '</fieldset>';
?>
</form>
答案 1 :(得分:0)
***if ($company == 2) {***
$ company被定义为数组但你已经提到了字符串。请检查。
答案 2 :(得分:0)
试试这个:
<form action="#" method="post">
<?php
$company = array (1 => 'One', 'Two', 'Three');
echo '<fieldset>
<select name="companys">';
foreach ($company as $key => $value) {
echo "<option value=\"$key\" onclick='this.form.submit()'>$value</option>\n";
}
echo '</select>';
if ($_POST['companys'] == 2) {
echo'<p><input type="checkbox" name="tandc" value="terms" onclick="this.form.submit()"/>I accept the terms and conditions
</p>';
} else {
echo 'OK';
};
echo '</fieldset>';
?>
</form>
答案 3 :(得分:0)
如果下拉值匹配条件,则无需提交表单即可显示元素只需使用javascript函数即可查看下面的示例代码
<form action="form1.php" method="post">
<?php
$company = array(1 => 'One', 2 => 'Two', 3 => 'Three');
echo '<fieldset>
<select name="companys" onchange="check_option(this.value);">';
foreach ($company as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';
echo'<p><input type="checkbox" id="terms" name="tandc" value="terms" style="display:none;"/>I accept the terms and conditions</p>';
echo '</fieldset>';
?>
</form>
<script>
function check_option(val)
{
if(val=='Two')
{
document.getElementById('terms').style.display='block';
}
}
</script>