根据下拉选择显示内容?

时间:2014-08-20 21:12:13

标签: php forms

当用户从下拉菜单中选择特定月份时,是否有人知道如何获取要呈现的内容?我觉得表格需要一些东西。而且我已经每月宣布,所以如果月份与数据库中的日期匹配,则要呈现内容。思考?谢谢!

<div id="content">
    <h1>Search by Album, Artist, or Genre</h1>
    <form  method="GET" action=""  > 
            <select>
            <option value="selectmonth">Select a month</option>
            <option value="July 2014" name="July">July 2014</option>
            <option value="August 2014" name="August">August 2014</option>
            <option value="September 2014" name="September">September 2014</option>

</select>
        </form> 
        <br />
    <?php 


$conn = new COM("ADODB.Connection") or die("Cannot start ADO");

$connString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\\FinalProject.mdb";

//creates the connection object and define the connection string

$month1 = $_GET ['July'];
$month2 = $_GET ['August'];
$month2 = $_GET ['September'];

//update
$conn->Open($connString);
$selectCommand="SELECT * FROM products WHERE  relasedate LIKE '$released'";

if(isset($_GET['July','August','September'])){

$rs = $conn->Execute($selectCommand);

//execute search when user selects month

if (!$rs->EOF){
    print "<h2>New releases for '<b>$released</b>':</h2>";
} //search results header

if ($rs->EOF){
    print "No results found.<br /><br />";
}else{
    while (!$rs->EOF){ 
        $selectCommand=$rs->Fields("ItemID");
        $artist=$rs->Fields("artist");
        $album=$rs->Fields("album");
        $genre=$rs->Fields("Genre");
        $desc=$rs->Fields("itemdesc");
        $recordl=$rs->Fields("recordlabel");
        $released=$rs->Fields("releasedate");
        $images=$rs->Fields("photos");

        echo "
        <div><img src=$images></div>
        <div><b>Artist</b>: $artist <br />
        <b>Album</b>: $album <br />
        <b>Record Label</b>: $recordl <br />
        <b>Release Date</b>: $released <br />
        <b>Genre</b>: $genre <br />
        <b>Description</b>: $desc <br />
        </div>";

        $rs->movenext();
    } //while loop to render results
} //end if statement for null results


$rs->Close;
} //close connection
?>

    </div>

1 个答案:

答案 0 :(得分:1)

在页面中创建一个div,我们将其称为contentDiv。其次,使用AJAX更新div:

<script>
function loadContent(selectedVal) {
     var xmlhttp;
     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("contentDiv").innerHTML=xmlhttp.responseText;
          }
     }
     xmlhttp.open("GET","getInfoFromDatabase.php?selected=" + selectedVal,true);
     xmlhttp.send();
}
</script>

您必须在选择框中添加onchange="loadContent(this.value)"才能获得其值。

然后,您将获得getInfoFromDatabase.php,根据$_GET['selected']值返回您需要的任何内容。