Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onchange reload page with value of select

I have a table displaying results, I want to give people the ability to update the results of a cell/field individually without needing a whole update form

Here is what I have:

echo "<select name='tv_taken' id='tv_taken' onchange='window.location=\"samepage.php?update=tv_taken&update_id=$tv_id\" '>";

As you can probably tell its a select box with a list of users. Now the only problem is i need to pass the new selected value in the url string, so i want the new value of the select.

So lets say it was mary and someone chooses a new user John I want something like this

echo "<select name='tv_taken' id='tv_taken' onchange='window.location=\"samepage.php?update=tv_taken&update_id=$tv_id&user_name=$find_username\" '>";

What I'm not sure on is there a way to pass the changed value via php or will I need a javascript solution?

Ill then use an

IF if(isset($_GET['tv_id'])) 
{
Do the update
}

This is not a public site so I'm guessing there is a more secure way to do this but this should probably suffice since it will only be team members using it who would be required to login before they can see this page.

like image 307
user1547410 Avatar asked Sep 08 '12 12:09

user1547410


2 Answers

You'll want to use JavaScript to achieve this.

Here's a jQuery solution:

$('#tv_taken').change(function() {
    window.location = "samepage.php?update=tv_taken&update_id=" + $(this).val();
});

And here's a vanilla JavaScript solution:

document.getElementById('tv_taken').onchange = function() {
    window.location = "samepage.php?update=tv_taken&update_id=" + this.value;
};
like image 53
Nathan Taylor Avatar answered Nov 05 '22 03:11

Nathan Taylor


window.addEventListener('load', function(){
    var select = document.getElementById('tv_taken');

    select.addEventListener('change', function(){
        window.location = 'pagename.php?tv_id=' + this.value;
    }, false);
}, false);
like image 37
Alex Avatar answered Nov 05 '22 02:11

Alex