[英]Using HTML/PHP forms to access a database and output to the same page
I have a database of schools, I would like to make it so that when you search the database for existing data, it outputs right next the the input box. 我有一个学校数据库,我想这样做,以便当您在数据库中搜索现有数据时,它会在输入框的旁边输出。 So without going to a new page.
因此,无需进入新页面。 Here's what a have:
这里有一个:
<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);
?>
This is code that I have used previously to link to another page, outputting there. 这是我以前用来链接到另一个页面并在其中输出的代码。 but now I just want to output it on the same page.
但现在我只想在同一页面上输出。 I did move some code over from the other page which no longer seems to be working.
我确实从其他页面移走了一些代码,而该页面似乎不再起作用。 The PHP bit just outputs: "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); ?>" onto my page below the input.
PHP位只是输出:“ 0){而($ newArray = mysql_fetch_array($ result)){$ school = $ newArray ['school']; $ date = $ newArray ['date']; $ place = $ newArray [ 'place']; $ time = $ newArray ['time']; echo $ school。“,”。$ place。“,”。$ date。“,”。$ time。“”;}}}其他{echo“找不到记录“;} mysql_close($ conn);?>”在输入下方的我的页面上。 I'm really new to this, so anyones help would be greatly appreciated.
我真的很陌生,所以任何人的帮助将不胜感激。 :D
:D
您必须使用Ajax向服务器发出请求,而无需重新加载页面。
You could modify this little snippet for the ajax part...It's using jQuery, so you'll need to include the library into your page. 您可以为ajax部分修改此小片段...它使用的是jQuery,因此您需要将库包含在页面中。
$('#submitButtonID').click(function(){
var data = {
schoolName: $('#schoolName').val()
};
$.ajax({
url: "PhpPageWithQuery.php",
type: "post",
data: data,
success: function(msg) {
$('#resultsDiv).html(msg);
}
});
Make sure that your file extension is .php
. 确保文件扩展名为
.php
。 Also check if $_POST['schooname']
isset
and if it is then continue with php code. 同时检查
$_POST['schooname']
isset
,如果它,然后用PHP代码继续。 A few other pointers, the use of the mysql
extension is not recommended since it's deprecated. 还有其他一些建议,不建议使用
mysql
扩展,因为它已被弃用。 Use either mysqli
or PDO
, also santize your input if you must use it. 使用
mysqli
或PDO
,如果您必须使用它,也请使输入变大。 I left the sanitation bit to you. 我把卫生设施留给了你。
<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);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.