根据用户的性别显示特定的选择选项

时间:2015-10-27 07:34:17

标签: php html html-select

我正在尝试根据性别对select option设置限制,并在表单中进行相应显示。我是否允许以这种方式编写以将其与表单中的if else语句进行比较?输出结果仅显示第一个select option而不是其他人,无论其性别如何。

这是我的代码:

if ($row = mysqli_fetch_assoc($staffQuery)) {
    $sex = $row['gender'];
  ?>
    <div class="form-group">
        <label for="select" class="col-md-2 control-label">Type of leave </label>        
            <div class="col-md-2">
               <?php
                 if ($sex == "male") {?>
                    <select class="form-control" id="select" name="leave_option">
                       <option value="1">Annual Leave</option>
                       <option value="2">Sick Leave</option>
                       <option value="3">No Pay Leave</option>
                       <option value="4" disabled>Maternity Leave</option>
                       <option value="5">Adoption Leave</option>
                       <option value="6">Childcare Leave</option>
                       <option value="7">Paternity Leave</option>
                       <option value="8">Shared Parental Leave</option>
                       <option value="9">Infant Care Leave</option>
                       <option value="10">Reservist Leave</option>
                   </select>
                   <?php
                   }else {?>
                   <select class="form-control" id="select" name="leave_option">
                       <option value="1">Annual Leave</option>
                       <option value="2">Sick Leave</option>
                       <option value="3">No Pay Leave</option>
                       <option value="4">Maternity Leave</option>
                       <option value="5">Adoption Leave</option>
                       <option value="6">Childcare Leave</option>
                       <option value="7">Paternity Leave</option>
                       <option value="8">Shared Parental Leave</option>
                       <option value="9">Infant Care Leave</option>
                       <option value="10" disabled>Reservist Leave</option>
                   </select>
                   <?php } ?>
                </div>
            </div>
           <?php  } ?>

任何帮助将不胜感激。谢谢。

4 个答案:

答案 0 :(得分:1)

你只能在一种情况下实现,就像这样

<select class="form-control" id="select" name="leave_option">
      <option value="1">Annual Leave</option>
      <option value="2">Sick Leave</option>
      <option value="3">No Pay Leave</option>
      <option value="4" <?php if($sex == "male") echo "disabled"; ?>>Maternity Leave</option>
      <option value="5">Adoption Leave</option>
      <option value="6">Childcare Leave</option>
      <option value="7">Paternity Leave</option>
      <option value="8">Shared Parental Leave</option>
      <option value="9">Infant Care Leave</option>
      <option value="10" <?php if($sex != "male") echo "disabled"; ?> >Reservist Leave</option>
</select>

答案 1 :(得分:1)

是。你可以使用if else但是如果用这种方式写的话会更有效

if ($row = mysqli_fetch_assoc($staffQuery)) {
    $sex = $row['gender'];
  ?>
    <div class="form-group">
        <label for="select" class="col-md-2 control-label">Type of leave </label>        
            <div class="col-md-2">
                    <select class="form-control" id="select" name="leave_option">
                       <option value="1">Annual Leave</option>
                       <option value="2">Sick Leave</option>
                       <option value="3">No Pay Leave</option>
                       <option value="4" <?php echo $sex=="male"?"disabled":"" ?> >Maternity Leave</option>
                       <option value="5">Adoption Leave</option>
                       <option value="6">Childcare Leave</option>
                       <option value="7">Paternity Leave</option>
                       <option value="8">Shared Parental Leave</option>
                       <option value="9">Infant Care Leave</option>
                       <option value="10" <?php echo $sex=="male"?"":"disabled"?>>Reservist Leave</option>
                   </select>
                </div>
            </div>

答案 2 :(得分:0)

如果您只想显示所需的选项,请按照以下步骤操作:

ERROR c.g.htmlunit.html.HtmlPage - Error loading JavaScript from [http://example.com/mobilenew/js/jquery.mobile.swipe.min.js].
java.io.IOException: Unable to download JavaScript from 'http://example.com/mobilenew/js/jquery.mobile.swipe.min.js' (status 404).
        at com.gargoylesoftware.htmlunit.html.HtmlPage.loadJavaScriptFromUrl(HtmlPage.java:1082) ~[htmlunit-2.18.jar:2.18]
        at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1009) ~[htmlunit-2.18.jar:2.18]
        at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:395) [htmlunit-2.18.jar:2.18]
        at com.gargoylesoftware.htmlunit.html.HtmlScript$3.execute(HtmlScript.java:276) [htmlunit-2.18.jar:2.18]
        at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:290) [htmlunit-2.18.jar:2.18]
        at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:800) [htmlunit-2.18.jar:2.18]
        at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) [xercesImpl-2.11.0.jar:na]
        at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:757) [htmlunit-2.18.jar:2.18]
        at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1170) [nekohtml-1.9.22.jar:1.9.22]
        at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1072) [nekohtml-1.9.22.jar:1.9.22]

答案 3 :(得分:0)

我发现为什么它无法工作,因为我忘了检索 来自staff_id的{​​{1}},因此比较无效。很高兴修复它并能够以更有效的方式完成它。感谢您的所有输入。

$_SESSION