How to pass variable to href in javascript?
Asked Answered
W

6

14

How to pass this variable value here? Below code is not working. And all other discussions on Stackoverflow are unclear.

<script type="text/javascript">
        function check()
        {
            var dist = document.getElementById('value');
            if (dist!=""){
                window.location.href="district.php?dist="+dist;
            }
            else
               alert('Oops.!!');
        }
</script>

And my HTML code is:

<select id="value" name="dist" onchange="return check()">
Wainscoting answered 25/12, 2013 at 5:20 Comment(1)
You may want to encode it as well.Bartram
F
18

You have to fetch field value using .value as you are passing whole object to the URL as document.getElementbyId('value') returns whole field object.

var dist = document.getElementById('value').value;

So your function should be like this

function check() {
    var dist = document.getElementById('value').value; // change here
    if (dist != "") {
        window.location.href = "district.php?dist=" + dist;
    } else
        alert('Oops.!!');
}
Fogbow answered 25/12, 2013 at 5:22 Comment(0)
S
6

You have fetch value of the field, Currently you are using DOM object

Use

 var dist = document.getElementById('value').value;

OR

Use

 if (dist.value!=""){
     window.location.href="district.php?dist="+dist.value;

instead of

if (dist!=""){
     window.location.href="district.php?dist="+dist;
Speer answered 25/12, 2013 at 5:22 Comment(2)
@Joel, upvoted your question soon you will enough reputation to upvote. As far I am concerned Its more important to help a fellow community member than reputation point. Mary x-masSpeer
Merry x'mas dude :) ThanksWainscoting
P
4

Try this:

function check() {
    var dist = document.getElementById('value').value;

    if (dist) {
        window.location.href = "district.php?dist=" + dist;
    } else {
        alert('Oops.!!');
    }
}
Pocket answered 25/12, 2013 at 5:24 Comment(0)
A
3

Try this:

var dist = document.getElementById('value').value;
if (dist != "") {
 window.location.href="district.php?dist="+dist;
}
Accordingly answered 25/12, 2013 at 5:24 Comment(0)
C
1

You have to make some correction in your function..

function check()
    {
        var dist = document.getElementById('value').value;  //for input text value
       if (dist!==""){  //  for comparision
           window.location.href="district.php?dist="+dist;
       }
       else
           alert('Oops.!!');
    }
Coff answered 25/12, 2013 at 5:25 Comment(0)
S
1

I use a very different approach to this. I set browser cookies in the client that expire a second after I set window.location.href.

This is way more secure than embedding your parameters in the URL.

The server receives the parameters as cookies, and the browser deletes the cookies right after they are sent.

const expires = new Date(Date.now() + 1000).toUTCString()
document.cookie = `oauth-username=user123; expires=${expires}`
window.location.href = `https:foo.com/oauth/google/link`
Seventh answered 8/2, 2021 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.