根据URL选择表单值

时间:2015-04-02 21:36:45

标签: php html string forms drop-down-menu

我试图根据网址选择一个下拉框。我想要网址

http://example.com/login.php?port=2095

或类似的东西在加载时自动选择Webmail,并为?port = 2082选择cPanel?

这是我的表格

<form action="cplogin.php" method="post">
Username:<input type="text" name="user"></td></tr>
Password:<input type="password" name="pass"></td></tr>
Login To:

 <select name="port">
 <option value="2082">cPanel</option>
 <option value="2083">Secure cPanel</option>
 <option value="2095">Webmail</option>
 <option value="2096">Secure Webmail</option>
 </select>

 <input type="submit" name="login" value="login" style="cursor:pointer">
 </td>
</tr>
</table>
</form>

我在网上看,但似乎无法找到答案。

1 个答案:

答案 0 :(得分:0)

首先为每个选项添加id属性,然后使用javascript将其选中。

<form action="cplogin.php" method="post">
Username:<input type="text" name="user"></td></tr>
Password:<input type="password" name="pass"></td></tr>
Login To:

 <select name="port">
 <option value="2082" id="2082">cPanel</option>
 <option value="2083" id="2083">Secure cPanel</option>
 <option value="2095" id="2095">Webmail</option>
 <option value="2096" id="2096">Secure Webmail</option>
 </select>

 <input type="submit" name="login" value="login" style="cursor:pointer">
 </td>
</tr>
</table>
</form>
<script>
    var url=document.location.href;
    url=url.substring(url.length-4,url.length);
    document.getElementById(url).selected="selected";
</script>