简体   繁体   中英

Upon selecting from an html drop down, redirect page and pass the selection value through url?

Here is what the form looks like as of now...

<select onchange="viewForm()">
<?php
    $db = mysqli_connect('xxxxxx.com', 'xxxx', 'xxxx', 'xxxxx');
    $sql = "SELECT title, fid FROM total";
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()){
        $sid = $row['fid'];
        $stitle = $row['title'];
        echo '<option value="'.$sid.'" >'.$stitle.'</option>';
    }
?>
</select>

And then here is the javascript function....

function viewForm(){
    window.location.replace("http://website.com/main.php?sid=IDwouldGoHere");
}

What i am trying to do is when one of the drop down options is selected, redirect to a new page while passing the 'sid' value through GET. Right now the redirect is working, but i can't find a way to pass the sid value to the javascript function.

DEMO: jsFiddle

HTML (generated by php for you)

<select id="yourSelectID">
    <option value="01201">test</option>
    <option value="012012">test2</option>
    <option value="012013">test3</option>
    <option value="012014">test4</option>
</select>

JavaScript

window.onload = function () {
    var eSelect = document.getElementById('yourSelectID');

    eSelect.onchange = function () {
        var strUser = eSelect.options[eSelect.selectedIndex].value;
        window.location.replace("http://website.com/main.php?sid=" + strUser);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM