dongqing5925 2015-11-13 17:11
浏览 42
已采纳

表单已禁用选择选项作为默认选项而不发送任何要验证的内容

I have a form like so:

<select name="employment">
    <option disabled="" selected="">-- What is your employment status --</option>
    <option value="employed">Employed</option>
    <option value="self_employed">Self Employed</option>
</select>

In PHP I am running through each of the $_POST variables and then checking if they have a value. If not I then add that field to the array for an error message.

The issue is that if I leave the default 'disabled' message selected nothing is passed through a post value so theres nothing for me to validate.

If I print_r my $_POST variable then it contains no 'employment' field unless I select an option.

How can I solve this?

  • 写回答

2条回答 默认 最新

  • douzhang2680 2015-11-13 17:52
    关注
    <select name="employment">
        <option disabled selected>-- What is your employment status --</option>
        <option value="employed">Employed</option>
        <option value="self_employed">Self Employed</option>
    </select>
    

    disabled attribute

    1. The disabled attribute is a boolean attribute.
    2. When present, it specifies that an option should be disabled
    3. A disabled option is unusable and un-clickable.

    Syntax:

    <select disabled> 
    

    Not

    <select disabled="">
    

    In case of XHTML, syntax differs like

    <select disabled="disabled">
    

    hidden attribute

    So, If you want to validate it. Use hidden in option.

    <select name="employment">
        <option hidden>-- What is your employment status --</option>
        <option value="employed">Employed</option>
        <option value="self_employed">Self Employed</option>
    </select>
    

    When, nothing got selected, then it will output as -- What is your employment status --

    <?php
    echo $Employment=$_POST['employment'];
    ?>
    

    Output: -- What is your employment status --

    So, Now you can easily use your validation in dropdown

    For more info, click disabled attribute - W3 Schools

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据