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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度