0

我有一个学校数据库,我想这样当您在数据库中搜索现有数据时,它会在输入框旁边输出。因此,无需进入新页面。这是一个有什么:

<form action=" " method="post">
School's name: <input type="text" name="schoolname"> <br/>
<input type="submit" name="button" value="Search">
</form>

<?php
$school      = $_POST['schoolname'];

$conn = mysql_connect("localhost", "root");
mysql_select_db("finalproject");

$sql = "select * from presentations where school like '%$school%'";

$result = mysql_query($sql, $conn) or die(mysql_error());

if ( mysql_num_rows($result) >0)
    {
    while ($newArray = mysql_fetch_array($result))
        {
        $school  = $newArray['school'];
        $date = $newArray['date'];
        $place  = $newArray['place'];
        $time = $newArray['time'];


        echo $school . ", " . $place . ", " . $date . ", " . $time . "<br />" ;
        }
    }
    else 
        {
        echo "Record not found";
        }



mysql_close($conn);
?>

这是我以前用来链接到另一个页面的代码,在那里输出。但现在我只想在同一页面上输出它。我确实将一些代码从似乎不再有效的其他页面移了过来。PHP 位只输出: "0) { while ($newArray = mysql_fetch_array($result)) { $school = $newArray['school']; $date = $newArray['date']; $place = $newArray[ 'place']; $time = $newArray['time']; echo $school . ", " . $place .", " . $date .", " . $time ." " ; } } else { echo " Record not found"; } mysql_close($conn); ?>" 到我的页面输入下方。我对此真的很陌生,所以任何人的帮助将不胜感激。:D

4

3 回答 3

0

您必须使用Ajax向服务器发出请求,而无需重新加载页面。

于 2013-01-07T19:06:19.840 回答
0

您可以为 ajax 部分修改这个小片段...它使用 jQuery,因此您需要将库包含到您的页面中。

$('#submitButtonID').click(function(){

var data = {
    schoolName: $('#schoolName').val()
};
$.ajax({
    url: "PhpPageWithQuery.php",
    type: "post",
    data: data,
    success: function(msg) {
        $('#resultsDiv).html(msg);

    }
});
于 2013-01-07T19:16:48.590 回答
0

确保您的文件扩展名是.php. 还要检查是否$_POST['schooname'] isset以及是否继续使用 php 代码。其他一些指针,mysql不推荐使用扩展,因为它已被弃用。使用mysqlior PDO,如果你必须使用它,也可以清理你的输入。我把卫生用品留给你了。

<form action=" " method="post">
School's name: <input type="text" name="schoolname"> <br/>
<input type="submit" name="button" value="Search">
</form>

<?php

if( isset( $_POST['schoolname'] ) && strlen( trim( $_POST['schoolname'] ) ) > 0  )
{
        $school      = $_POST['schoolname'];

        $conn = mysql_connect("localhost", "root");
        mysql_select_db("finalproject");

        $sql = "select * from presentations where school like '%$school%'";

        $result = mysql_query($sql, $conn) or die(mysql_error());

        if ( mysql_num_rows($result) >0)
        {
            while ($newArray = mysql_fetch_array($result))
            {
                $school  = $newArray['school'];
                $date = $newArray['date'];
                $place  = $newArray['place'];
                $time = $newArray['time'];


                echo $school . ", " . $place . ", " . $date . ", " . $time . "<br />" ;
            }
        }
        else 
        {
            echo "Record not found";
        }



        mysql_close($conn);
}
?>
于 2013-01-07T19:33:20.157 回答