如果bool为true,则向下拉列表添加选项

时间:2014-11-02 21:40:58

标签: php

如果bool为true,我想在下拉列表中添加一个选项,例如,如果ownsuv = true,它会将其添加到下拉列表中,包括他/她拥有的其他车辆。 编辑:添加代码

<?
$showgender = true;
if($showgender = true){
<html>
<p>
What is your Gender?
<select name="formGender">
  <option value="">Select...</option>
  <option value="M">Male</option>
  <option value="F">Female</option>
</select>
</p>
</html>
}
 ?>

3 个答案:

答案 0 :(得分:0)

您可以将select元素放在变量中,如下所示:

$select_element = <<<HTML
<p>
What is your Gender?
<select name="formGender">
  <option value="">Select...</option>
  <option value="M">Male</option>
  <option value="F">Female</option>
</select>
</p>
HTML;

现在,如果$showgender为真,则显示$select_element,如下所示:

<html>
    <body>
        <h1>Welcome to my wonderful form</h1>
        <form>
<?php
if ($showgender) {
    echo $select_element;
}
?>
        </form>
    </body>
</html>

答案 1 :(得分:0)

<?php

$showgender = true;
if ($showgender) {
  echo '
  <html>
  <p>
  What is your Gender?
  <select name="formGender">
    <option value="">Select...</option>
    <option value="M">Male</option>
    <option value="F">Female</option>
  </select>
  </p>
  </html>
  ';
}
 ?>

答案 2 :(得分:-1)

首先,如果你没有添加结束,你会遇到PHP错误吗?&gt;在你的if子句之后:

<?php
$showgender = true;
if($showgender == true){
?>
<html>
<p>
What is your Gender?
<select name="formGender">
  <option value="">Select...</option>
  <option value="M">Male</option>
  <option value="F">Female</option>
</select>
</p>
</html>
<?php
}
?>

如果你想添加一个选项,那就这样做:

<?php
$showgender = true;
if($showgender == true){
?>
<html>
<p>
What is your Gender?
<select name="formGender">
  <option value="">Select...</option>
  <option value="M">Male</option>
  <option value="F">Female</option>
  <?php if($yourboolean == true) { ?>
  <option value="X">Feel Free</option>
  <?php } ?>
</select>
</p>
</html>
<?php
}
?>