使用下拉列表组织我的数据库结果

时间:2016-02-03 14:57:25

标签: php ajax oracle oracle11g

大家,我是新手,我正在尝试使用2个下拉列表组织我的数据库结果 我想要的是根据用户的选择过滤结果 我正在使用oracle HR数据库:

<html>
<head>
<script>
function showEmp(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","emp.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>
    <!--END Header-->

        <table>
        <tr>
            <!--Semester-->
            <td align="right" bgcolor="#CCCCCC">MANAGER ID</td>
            <td><select name="MANAGER_ID" id="M_ID" style="font-weight:bold;" onchange="showEmp(this.value)">
            <option value=""  selected="selected" >select a manager</option>
            <option value="100" >Bob</option>
            <option value="101" >alex</option>
            <option value="103" >Steven</option>
            </select></td>
            <!--Week-->

            <td align="right" bgcolor="#CCCCCC">DEPARTMENT ID</td>
            <td><select name="dep" id="dep-id"   style="font-weight:bold;" onchange="showEmp(this.value)">
            <form action="">
            <option value="all"  selected="selected" >select a department</option>
            <option value="90" >IT</option>
            <option value="30" >HR</option>
            <option value="100" >finance</option>
            </select>
            </form>
            </td>





        <tr><td align="center" >Emp info</td></tr>

    <div class="container" style="center" id="txtHint" >
    <?php

    $q = intval($_GET['q']);
    $stid = oci_parse($conn, "SELECT * FROM employees WHERE DEPARTMENT_ID = '".$q."'");
    oci_execute($stid);?>
    <table id="report" align="center" width="98%" cellpadding="5" cellspacing="0" dir="rtl" border="0" style="margin-bottom: 5px;">
    <tr>
    <td bgcolor="#CCCCCC" align="center" width="30">EMPLOYEE ID</td>
    <td bgcolor="#CCCCCC" align="center">FIRST NAME</td>
    <td bgcolor="#CCCCCC" align="center">LAST NAME</td>
    <td bgcolor="#CCCCCC" align="center">EMAIL</td>
    <td bgcolor="#CCCCCC" align="center">PHONE NUMBER</td>
    <td bgcolor="#CCCCCC" align="center">HIRE DATE</td>
    <td bgcolor="#CCCCCC" align="center">JOB ID</td>
    <td bgcolor="#CCCCCC" align="center">SALARY</td>
    </tr>


    <?php



    while ($row=oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){ 

    ?>
        <tr>        
            <td><?php echo $row['EMPLOYEE_ID']; ?></td>
            <td><?php echo $row['FIRST_NAME']; ?></td>
            <td><?php echo $row['LAST_NAME']; ?></td>
            <td><?php echo $row['EMAIL']; ?></td>
            <td><?php echo $row['PHONE_NUMBER']; ?></td>

        </tr>
        <?php
    }


    ?>

    </table>

    </div><!--end container-->

    </table>
    <br />

2 个答案:

答案 0 :(得分:0)

据我所知,html应该更像这样,但是没有定义空表/缺少单元格或colspans。这并没有解决问题

<?php
    /* Process ajax ~ is this the same page? ie: emp.php or another page? */
    if( !empty( $_GET['MANAGER_ID'] ) ){
        ob_clean();

        $manager_id=$_GET['MANAGER_ID'];

        /* construct your SQL to select from whichever table */
        $sql='select from <TABLE> where <ID>='.$manager_id;

        /* execute query and iterate through recordset to build the menu innerHTML */

        /* echo html to be processed by ajax callback */
        echo $html;


        exit(); 
    }
?>

<!doctype html>
<html>
    <head>
        <script>
            function showEmp(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","emp.php?q="+str,true);
                    xmlhttp.send();
                }
            }
        </script>
    </head>
    <body>
        <!--END Header-->
        <form action="">
            <table>
                <tr>
                    <!--Semester-->
                    <td align="right" bgcolor="#CCCCCC">MANAGER ID</td>
                    <td>
                        <select name="MANAGER_ID" id="M_ID" style="font-weight:bold;" onchange="showEmp(this.value)">
                            <option value=""  selected="selected" >select a manager</option>
                            <option value="100" >Bob</option>
                            <option value="101" >alex</option>
                            <option value="103" >Steven</option>
                        </select>
                    </td>
                    <!--Week-->
                    <td align="right" bgcolor="#CCCCCC">DEPARTMENT ID</td>
                    <td>
                        <select name="dep" id="dep-id"   style="font-weight:bold;" onchange="showEmp(this.value)">
                            <option value="all"  selected="selected" >select a department</option>
                            <option value="90" >IT</option>
                            <option value="30" >HR</option>
                            <option value="100" >finance</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="center" >Emp info</td>
                </tr>
            </table>
        </form>

        <div class="container" style="center" id="txtHint">
            <?php
                $q = intval($_GET['q']);
                $stid = oci_parse($conn, "SELECT * FROM employees WHERE DEPARTMENT_ID = '".$q."'");
                oci_execute($stid);
            ?>
            <table id="report" align="center" width="98%" cellpadding="5" cellspacing="0" dir="rtl" border="0" style="margin-bottom: 5px;">
                <tr>
                    <td bgcolor="#CCCCCC" align="center" width="30">EMPLOYEE ID</td>
                    <td bgcolor="#CCCCCC" align="center">FIRST NAME</td>
                    <td bgcolor="#CCCCCC" align="center">LAST NAME</td>
                    <td bgcolor="#CCCCCC" align="center">EMAIL</td>
                    <td bgcolor="#CCCCCC" align="center">PHONE NUMBER</td>
                    <td bgcolor="#CCCCCC" align="center">HIRE DATE</td>
                    <td bgcolor="#CCCCCC" align="center">JOB ID</td>
                    <td bgcolor="#CCCCCC" align="center">SALARY</td>
                </tr>
                <?php
                    while ( $row=oci_fetch_array( $stid, OCI_ASSOC+OCI_RETURN_NULLS ) ){
                ?>
                <tr>        
                    <td><?php echo $row['EMPLOYEE_ID']; ?></td>
                    <td><?php echo $row['FIRST_NAME']; ?></td>
                    <td><?php echo $row['LAST_NAME']; ?></td>
                    <td><?php echo $row['EMAIL']; ?></td>
                    <td><?php echo $row['PHONE_NUMBER']; ?></td>
                    <!-- what about the other cells for which there are column headers? -->
                </tr>
                <?php
                }/* close while loop */
                ?>
            </table>
        </div><!--end container-->
    </body>
</html>

例如

<?php
    /*
        -------
        emp.php
        -------
    */
    /* include database connection */
    include 'dbconn.php';

    if( isset( $_GET['q'] ) ){

        /* Filter by ONE field */
        $var=$_GET['q'];

        /* construct your sql */
        $sql='select * from <TABLE> where <COL>="'.$var.'"';

        /* empty array to store generated html */
        $html=array();

        /* query the db */
        $result=$db->query( $sql );
        /* Iterate through recordset */

        while( $row=$result->fetch() ){
            /* add html to array */
            $html[]="<option value='".$row->FIELD_NAME_ID."'>".$row->FIELD_NAME_STRING;
        }
        /* close db connection */
        $db->close();

        /* send data back to ajax callback */
        echo implode( PHP_EOL, $html );

        exit();
    }

?>

search.php
----------
<!doctype html>
<html>
    <head>
        <title>Example pseudo code and method explanation</title>
    </head>
    <body>
        <!--
            here is YOUR html with the select menus
            The select menus have an `onchange` event
            handler which sends a request to `emp.php`

            `emp.php` is sent, by the ajax function 1
            parameter - called `q`

            emp.php must process the querystring / GET 
            request variables to generate the sql that is
            used to build the html content that will be used
            by the AJAX callback function to populate the div
            with ID "txtHint"

            Ideally the AJAx request would use POST rather than GET
            but that might just be a personal preference in situations
            like this.

            Assume this page is called "search.php" and has the db
            connections and works OK.
        -->
    </body>
</html>

答案 1 :(得分:0)

HTML code:

<html>
<head>
<script>
function showUser(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","getuser.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
  <option value="">Select a salary:</option>
  <option value="17000">17000</option>
  <option value="9000">9000</option>
  <option value="4800">4800</option>
  <option value="7700">7700</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

php代码:                         表{         宽度:100%;         边界崩溃:崩溃;     }

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

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

    $conn = oci_connect('hr', 'hr123', 'Glory-PC:1521/XE');
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    $stid = oci_parse($conn, "SELECT * FROM employees WHERE SALARY= '".$q."'");
                oci_execute($stid);


echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row=oci_fetch_array( $stid, OCI_ASSOC+OCI_RETURN_NULLS )) {
    echo "<tr>";
    echo "<td>" . $row['FIRST_NAME'] . "</td>";
    echo "<td>" . $row['LAST_NAME'] . "</td>";
    echo "<td>" . $row['EMAIL'] . "</td>";
    echo "<td>" . $row['PHONE_NUMBER'] . "</td>";
    echo "<td>" . $row['HIRE_DATE'] . "</td>";
    echo "</tr>";
}
echo "</table>";

?>
</body>
</html>