douliu7929 2015-10-09 08:34
浏览 57
已采纳

HTML - 使URL变量覆盖select标记中的默认选择

I have a form with select tags for date of birth. The day and month default to the first options (the 1st, and January), which is fine. For the year, I use selected="selected" for 1975, since this is the average age for my users.

What I want to be able to do is to overwrite this default year value by passing the variable in a URL.

For example:

www.domain.com?dob_day=14&dob_month=july&dob_year=1980

The reason being is that I want to send an email and pre-populate the fields they filled out.

So what's happening right now when I look at the source code is both the year 1975 is selected, and the year passed in the variable.

What would be the best way to achieve the above?

Thank you very much.

  • 写回答

2条回答 默认 最新

  • dpxw7293 2015-10-09 09:04
    关注

    To overwrite the default values, you would use something like this to retrieve the variables from the URL:

    $dob_year = $_GET["dob_year"];
    $dob_day = $_GET["dob_day"];
    $dob_month = $_GET["doby_month"];
    

    And then for each select drop down, you could do this:

    echo "<select id = \"dob_month\">";
    
    for($i=1;$i<=12;$i++)
    {
        $sel = $i==$dob_month ? "selected" : "";
    
        echo "<option value=\"$i\" $sel>$i</option>";
    }
    
    echo "</select>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在