Question

how do i make the page reloaded if i choose selected dropdown ?

assume there is a dropdown look like this

<select name="selectyear">
    <option value="2000">2000</option>
    <option value="2000">2001</option>
    <option value="2000">2002</option>
    <option value="2000">2003</option>
</select>

and a query like this

SELECT * FROM TABLEYEAR WHERE YEAR = 2014  

and if i choose 2000

the page will reload and the url will look like this

http://www.dashboard.php?year=2000

and the query also will change

SELECT * FROM TABLEYEAR WHERE YEAR = 2000

i really appreciate the help..

this is my dropdown look likes ..

<select class="chzn-select deselect" name="selectyear" style="width:150px;" data-placeholder="Select year">
<option value=""></option>
<?php
$date = date("Y");
for($i = $date-10;$i<$date+5;$i++)
{
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php   
}
?>
</select>

and this is the query .. i dont know how to put the if statement for the query if none is selected, then display this year . if it selected, display the selected year.

$id = $_GET['year'];

$date = date("Y");


$query = mysql_query("select * from visimisi where year = '$id'");
Était-ce utile?

La solution

Put jQuery code:

<script src="JQUERY-PATH"></script>
<script>
$(function(){
    $("[name=selectyear]").change(function(){
        var val = $(this).val();
        var year;
        if (typeof val !== 'undefined') {
            year = val;
        }
        else {
            year = '<?php echo date('Y');?>';
        }
        window.location.href = 'http://www.dashboard.php?year='+year;
        return true;
    });
});
</script>

Autres conseils

<select name="selectyear" onchange="getval(this);">
    <option value="2000">2000</option>
    <option value="2000">2001</option>
    <option value="2000">2002</option>
    <option value="2000">2003</option>
</select>


<script type="text/javascript">
    function getval(sel) {
       alert(sel.value);
       window.location.href = 'http://www.dashboard.php?year='+sel.value;
       // give your same page url if you want same page redirect
       // or try window.location.href = '?year='+sel.value;
    }
</script>

then you can get $_GET['year'] value on redirect page and run your query

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top