我有一个学校的数据库,我想使它,当你搜索数据库的现有数据,它输出旁边的输入框。不用翻到新的一页。以下是我的建议:
<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。","。美元的地方。","。美元的日期。","。时间到了。";}} else{返回"未找到记录";} mysql_close(康涅狄格州);?>在我的页面输入下面。我对这个真的很陌生,所以任何人的帮助都会非常感激。: D
确保您的文件扩展名是.php
。还要检查$_POST['schooname']
是否为isset
,如果是,则继续php代码。另外,不建议使用mysql
扩展,因为它已被弃用。使用mysqli
或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);
}
?>
必须使用Ajax向服务器发出请求,而不需要重新加载页面。
您可以为ajax部分修改这一小段代码…它使用jQuery,所以您需要将库包含到页面中。
$('#submitButtonID').click(function(){
var data = {
schoolName: $('#schoolName').val()
};
$.ajax({
url: "PhpPageWithQuery.php",
type: "post",
data: data,
success: function(msg) {
$('#resultsDiv).html(msg);
}
});