-1

I'm trying to get a dropdown box to select based on the URL. I would like the url

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

or something similar to automatically select Webmail on load and for ?port=2082 to select cPanel?

Here is my form

<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>

I have look online but can't seem to find an answer.

aled2305
  • 43
  • 1
  • 10
  • possible duplicate of [How to create a hyperlink that directs to a page with pre-selected option in a drop-down menu?](http://stackoverflow.com/questions/25207583/how-to-create-a-hyperlink-that-directs-to-a-page-with-pre-selected-option-in-a-d) – Dave Apr 02 '15 at 21:39
  • You need a loop, and if-statement, and a knowledge of HTML. – developerwjk Apr 02 '15 at 21:39
  • @developerwjk Why does he need a loop and if-statement? –  Apr 02 '15 at 21:52
  • 1
    @Mathematician171 To do it in PHP would require looping through the options and building the HTML for the dropdown and checking each one to see if it was chosen. To do it in Javascript he doesn't. – developerwjk Apr 02 '15 at 21:53
  • @developerwjk He never says that he need to do it in PHP. Also, it can be done in a PHP without using any loops or if-statements. –  Apr 02 '15 at 21:56
  • @Mathematician171 I'd like to see how it can be done in PHP without a loop or if-statements. – developerwjk Apr 02 '15 at 21:56
  • @developerwjk Simple. `var url=document.location.href; url=url.substring(url.length-4,url.length); document.getElementById(url).selected="selected";'; ?>` –  Apr 02 '15 at 21:57
  • @Mathematician171 Printing Javascript from PHP doesn't count as a pure PHP solution, obviously. – developerwjk Apr 02 '15 at 22:01

1 Answers1

0

First add id attribute to each option, then using javascript make it selected.

<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>