dsunj08246 2016-02-20 08:22
浏览 49

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

I do not know how will I display the position and names of the officers from year selected by the user.

Edit:

I used ajax now, but I'm still having a problem. When I click the combobox and select a year, the officers still don't show up. Only the table with the header Position and Name that shows up, but no data from my database under those columns.

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>
  • 写回答

1条回答 默认 最新

  • dongpou1935 2016-02-20 08:46
    关注

    some pseudo-code o give you an idea of how you could achieve the desired goal.

    <?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 */
            }
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行