-2

Trying to send a Javascript variable value to another page on SUBMITTING a form.

HTML

<form action="po1.html">
<input type="button" value="Submit" onclick="getVals();" />
</form>

JS

function getVals(){
        var e = document.getElementById("Features");    
        var strUser = e.options[e.selectedIndex].value;
        alert(strUser);
    }

i have selected that strUser value by a SELECT OPTION menu, now i don't know how to send it to another HTML page buy submitting... help please

Junaid Farooq
  • 353
  • 1
  • 5
  • 17
  • Just clarifying - you're submitting the form to po1.html but wish to send the value within the select box to **an entirely different** page? Or you wish to change the form's destination? – PeteAUK Aug 14 '14 at 09:49
  • i just want to send strUser's value to page, TO ANY PAGE just through a onCLICK function – Junaid Farooq Aug 14 '14 at 11:10

3 Answers3

1
<form action="po1.html" method="get">
  <select name="selectnames">
    <option>me</option>
    <option>you</option>
    <option>she</option>
  </select>
  <input type="submit" value="Submit" />
</form>

the value of select with name selectnames will be automatically available on another site, you would catch it by its name on another page with GET or POST depending on which method of form you use.

doniyor
  • 36,596
  • 57
  • 175
  • 260
0

If you insist on doing this with JS, then add it to the form using a hidden input.

<form id="myForm" action="po1.html">
    <input type="submit" value="Submit">
</form>
<script>
    document.getElementById('myForm').addEventListener('submit', getVals);
    function getVals(event) {
        var e = document.getElementById("Features");    
        var strUser = e.options[e.selectedIndex].value;
        var hidden = document.createElement('input');
        hidden.type = "hidden";
        hidden.name = "foo";
        hidden.value = strUser;
        event.target.appendChild(hidden);
    }
</script>

… but it would make more sense to just put the select element in the form.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • you didn't get.. i have value in strUser, now i want to send it to that po1.html page on submitting – Junaid Farooq Aug 14 '14 at 09:50
  • Just tell me how to send a Javascript variable value to another page. – Junaid Farooq Aug 14 '14 at 09:52
  • @JunaidFarooq — I did. – Quentin Aug 14 '14 at 09:53
  • like you have a variable strUser having a value of 1 now print it to another page – Junaid Farooq Aug 14 '14 at 09:53
  • @JunaidFarooq — You didn't ask how to display it on the other page, only how to send it. It is being sent. To display it, you need to pull it out of the query string. That is best done with server side code. Otherwise see http://stackoverflow.com/questions/9870512/how-to-obtaining-the-querystring-from-the-current-url-with-javascript – Quentin Aug 14 '14 at 09:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59348/discussion-between-junaid-farooq-and-quentin). – Junaid Farooq Aug 14 '14 at 11:17
0

Try this:

You have to set value to page 1:

localStorage.setItem('val',strUser)

You have to get value to page 2:

localStorage.getItem('val')

Be aware, that not every user has a localStorage (older browsers).

Zim84
  • 3,404
  • 2
  • 35
  • 40
CSK
  • 777
  • 7
  • 17