onclick window.location.href with variable

56,960

Solution 1

Try this:

<select name="period" onchange="window.location.href = 'test.php?Period=' + this.options[this.selectedIndex].value;">

Solution 2

you can easily read the value when the select triggers the change event:

<select onchange="window.location.href = 'test.php?Period=' + this.value;">
</select>
Share:
56,960
user2397282
Author by

user2397282

Updated on July 27, 2022

Comments

  • user2397282
    user2397282 almost 2 years

    I have a select with some options inside and I want the user to be sent to another page with a variable in the URI so that I can extract it.

    This is how my select is set up:

    <select name="period" onclick="window.location.href = 'test.php?Period=' + this.selectedIndex;">
    

    I should then be able to use $_GET['Period'] to get the value.

    However, the user is never sent to test.php.

    Why is this?

  • user2397282
    user2397282 over 10 years
    I have another similar question: <input name="date" id="datepicker" onchange="window.location.href = 'test.php?Date=' + document.getElementById("datepicker").value;"> It should send the value inside the input box but it doesn't send anything?
  • user2397282
    user2397282 over 10 years
    Can you help with this please?
  • Goran.it
    Goran.it over 10 years
    Your code should work, did you test in console (ff/chrome) if that selector outputs anything ? document.getElementById("datepicker").value Remember you should have <input id="datepicker" value="something" /> for this to work