簡體   English   中英

如何顯示所選年份的兩個不同列

[英]how to display the two different columns from the selected year

我不知道如何根據用戶選擇的年份顯示官員的職位和名稱。

編輯:

我現在使用了ajax,但是仍然有問題。 當我單擊組合框並選擇年份時,軍官仍然不會出現。 僅顯示標題為“位置”和“名稱”的表,而這些列下沒有來自我的數據庫的數據。

getyear.php

<?php
$q = strval($_GET['q']);

$con = mysqli_connect('localhost','root','','test');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM officers WHERE year = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Position</th>
<th>Name</th>

</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['position'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";

    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

main.php

<form>
<?php
    mysql_connect("localhost","root","");
    mysql_select_db("test");
    $sql = "SELECT DISTINCT year FROM officers ORDER BY year DESC";
    $result = mysql_query($sql);

    /* assign an onchange event handler */
    echo "<select name='year' onchange='showofficers(this.value)'>";
    while ($row = mysql_fetch_array($result)) {
         echo "<option value='" . $row['year'] ."'>" . $row['year'];
    }   
    echo "</select> <br>";
    ?>
    <div id="txtHint">
    </div>
</form>
<script>
    /* event handler ~ no ajax function shown */
    function showofficers(str){
        if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","getyear.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

一些偽代碼可以使您了解如何實現期望的目標。

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("test");
    $sql = "SELECT DISTINCT year FROM officers ORDER BY year DESC";
    $result = mysql_query($sql);

    /* assign an onchange event handler */
    echo "<select name='year' onchange='showofficers(this.value)'>";
    while ($row = mysql_fetch_array($result)) {
         echo "<option value='" . $row['year'] ."'>" . $row['year'];
    }   
    echo "</select> <br>";

?>


<script>
    /* event handler ~ no ajax function shown */
    function showofficers( value ){
        /* 
            use ajax to send a request that fetches the officers details
            based upon the year selected. Preferred method=POST for ajax query
        */
        alert( 'send '+value+' via ajax, build the sql query and use the ajax callback to generate the new html content' );
    }
</script>


<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        /* Intercept and process ajax request */

        /* the year is POSTed by ajax */
        $year = $_POST['year'];

        $sql='select * from table where year='.$year;

        $res=$db->query( $sql );

        if( $res ){
            /* process recordset and send back response */
        }
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM