0

Based on Adding a parameter to the URL with JavaScript i tried to make a script that adds parameters to the existing URL but kinda failed and i don't know why...

    <script type="javascript">
    function insertParam(key, value)
    {
        key = encodeURI(key); value = encodeURI(value);
        var kvp = document.location.search.substr(1).split('&');
        var i=kvp.length; var x; while(i--) 
        {
            x = kvp[i].split('=');
            if (x[0]==key)
            {
                x[1] = value;
                kvp[i] = x.join('=');
                break;
            }
        }
        if(i<0) {kvp[kvp.length] = [key,value].join('=');}
        //this will reload the page, it's likely better to store this until finished
        document.location.search = kvp.join('&'); 
    }
</script>
<label>
    <select name="id" onchange="window.location='somepage.php?page=inserari-note&selected_value='+this.value">
    <option>--Alege user--</option>
        <?php 
            while($runrows = mysql_fetch_assoc($run))
            {
                $user = $runrows ['user_login'];
                echo"<option value=\"$user\">$user</option>";
            }
        ?>
    </select>
</label>
<label>
    <select name="idelev" onchange="insertParam('selected_valueelev',+this.value)">
    <option>--Alege Elev--</option>
        <?php 
            while($runrows4 = mysql_fetch_assoc($run4))
            {
                $elev = $runrows4 ['Nume'];
                echo"<option value=\"$elev\">$elev</option>";
            }
        ?>
    </select>
</label>

So the first select is populating the second select with students and also change the URL into somepage.php?page=inserari-note&selected_value='+this.value which works great but when i click on the options from the second select, nothing happens. In my opinion it should add at the existing URL which is the one i have mentioned before, the values &selected_valueelev="chosen-option" so that the URL should look like let's say somepage.php?page=inserari-note&selected_value=user&selected_valueelev=student. What am i doing wrong?

Community
  • 1
  • 1
ion voinescu
  • 45
  • 1
  • 6
  • Never mind the question... i have solved it by using another script... I have changed the second select with – ion voinescu Oct 01 '13 at 14:32

1 Answers1

0

You should define script tag by the following:

<script type="text/javascript">
...
</script>

And also you have a syntax bug in the following:

// remove + before this.value    
<select name="idelev" onchange="insertParam('selected_valueelev',+this.value)">
4ir4o
  • 37
  • 1
  • 10