在html中执行php函数

时间:2014-07-22 18:44:59

标签: javascript php html mysqli

我是php的新手。我在php中有一个函数,它在执行时返回数据库的结果。当我使用onclick方法在我的html页面中使用此功能时,它无法正常工作。你能帮帮我吗? 下面是author.php页面的代码,其中包含函数 -

<?php

function author_list()
{
global $con;
$con=mysqli_connect("localhost","root","","project");
if(mysqli_connect_errno())
{
echo "failed to connect".mysqli_connect_errno();
}

$author=mysqli_query($con,"select * from english where author='".$SBA."' ");


echo"<table>";
 while($data=mysqli_fetch_array($author))
 {
   echo"<tr>";
   echo"<td>".$data['genre'];
   echo"<td>".$data['author'];
   echo"<td>".$data['title'];
   echo"<td>".$data['year'];
   echo"<td>".$data['avail'];
 }

echo"</table>";
mysqli_close($con);
}
?>

下面是html页面代码的一部分,我必须在onclick上设置上述功能:

<form action="author.php" method=GET>
Search By Author:<input type="text" name="SBA" value=""><br>
<input type="submit" value="submit" onclick= "author_list()">

只是打印&#34; while循环&#34;页面上的部分功能。我也被允许使用javascript(如果这有帮助)。 你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

使用isset()函数执行应用程序的特定部分/正文/功能Javascript支持前端和php在后端运行..如果你想通过javascript调用,你可以调用ajax

<form action="author.php" method=GET>
Search By Author:<input type="text" name="SBA" value=""><br>
<input type="submit" value="submit" name="author_list">



<?php

if(isset($_GET['author_list'))
{
global $con;
$con=mysqli_connect("localhost","root","","project");
if(mysqli_connect_errno())
{
echo "failed to connect".mysqli_connect_errno();
}

$author=mysqli_query($con,"select * from english where author='".$SBA."' ");


echo"<table>";
 while($data=mysqli_fetch_array($author))
 {
   echo"<tr>";
   echo"<td>".$data['genre'];
   echo"<td>".$data['author'];
   echo"<td>".$data['title'];
   echo"<td>".$data['year'];
   echo"<td>".$data['avail'];
 }

echo"</table>";
mysqli_close($con);
}
?>