Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select from drop-down menu and reload page

Tags:

php

mysql

I've got a table that populates data from a MYSQL database and populates a drop-down menu from the same database. I have the drop down menu and table just fine, I would like to be able to choose which data I show in the table however.

<select name = 'peer-id' method='post' style = 'position: relative'>
        <?php
        while ($content = mysql_fetch_array($peer)) {
            echo "<option value='" . $content['Peer'] . "'>" . $content['Peer'] . "</option>";
        }
        $results = mysql_query("SELECT Destination FROM rate  ");
        ?>
    </select>

That's what I have for the select box. How can I get the choice from that and save that as a variable and refresh the table data?

I need to clarify that this will change that current data

 #Data#Data#Data
 #Data#Data#Data
 #Data#Data#Data

Then choose drop down choice and I want it to show new data

 #Data2#Data2#Data2
 #Data2#Data2#Data2
 #Data2#Data2#Data2

So it's going to need to load a new page or refresh some how because it's changing via PHP and not javascript.

like image 810
Seth Earby Avatar asked Dec 03 '22 22:12

Seth Earby


2 Answers

I think form may be better, for example

<form id="myform" method="post">
<select name = 'peer-id' style = 'position: relative' onchange="change()">
    <option value="1">12</option>
    <option value="2">15</option>
    <option value="3">16</option>
    <option value="4">18</option>
</select>
</form>

<script>
function change(){
    document.getElementById("myform").submit();
}
</script>

In the above code, whenever you change the value of select, it will post to the backend, then according to the posted value, you can do want you want, to get the peer-id in php, you can use the following code

$peer-id = $_POST['peer-id'];

Hope helps!

like image 133
Hanfeng Avatar answered Dec 09 '22 15:12

Hanfeng


apply this code in select tag hope this works

<select  onchange="location = this.options[this.selectedIndex].value;" style="text-decoration:none;">
 <option value="customers.php"></font></option>
</select>
like image 44
Yogesh Prajapati Avatar answered Dec 09 '22 14:12

Yogesh Prajapati