php - 如何显示所选年份的两个不同列

标签 php mysql sql database combobox

我不知道如何显示用户选择的年份中官员的职位和姓名。

编辑:

我现在使用ajax,但仍然遇到问题。当我单击组合框并选择年份时,官员仍然没有出现。仅显示带有标题“位置”和“名称”的表,但这些列下没有来 self 的数据库的数据。

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);
?>

ma​​in.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 */
        }
    }
?>

关于php - 如何显示所选年份的两个不同列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520799/

相关文章:

php - ORDER BY RAND() 返回重复项

sql - 为什么 row_number() 比使用 offset 更快?

php - 在PHP中使用配置

php - 解析错误 : syntax error, 意外 'unset' (T_UNSET)

php - wp_enqueue_script() 和 wp_register_script() 有什么区别

php - 插入同一张表避免重复

mysql - 将 NoSQL 数据库用于关系目的

php - HTML 表格到 php 数组

mysql - 如何一次启动多个 Node 脚本,或将每个脚本与 main 连接以一起启动

java - PostgreSQL:如何在第一个事务未完成时从其他事务中读取更新