-2

I'm new to php. I have a function in php which when executed returns results from the database. When i'm using this function in my html page with the onclick method it is not working. Can you please help me out? Below is the code for the page author.php which contains the function-

<?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);
}
?>

below is the part of the code of the html page where i have to set the above function on 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()">

It is just printing the "while loop" part of the function as it is, on the page. I am also allowed to use javascript(if that helps). Can you please help me out?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
learner
  • 95
  • 2
  • 2
  • 14
  • 7
    You need to learn the difference between server-side and client-side. – John Conde Jul 22 '14 at 18:45
  • Use `isset()` for that and name your submit button. – Funk Forty Niner Jul 22 '14 at 18:46
  • The `onclick` attribute is for executing Javascript. Please, do some research on the differences between client- and server-side programming. – esqew Jul 22 '14 at 18:51
  • Plus, you're best using POST instead of GET (with what you're using), what with possible SQL injection at stake on top of that. I tend to shy away from GET, plus using prepared statements is a given. – Funk Forty Niner Jul 22 '14 at 18:54
  • In the author.php, shouldn't you, at some point, recover the value from the form? Since you are using GET, you should have somewhere in your code $SBA = $_GET['SBA'] which will recover the value from the submitted form. You would have to escape this value before using it in your query or better yet use prepared queries to avoid sql injections. – Bogdan Jul 22 '14 at 21:02

1 Answers1

0

use isset() function to execute the specific part/body/function of application Javascript support for frontend and php runs at backend ..if you like to call via javascript , u can call 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);
}
?>
Muhammad Ali
  • 1,992
  • 1
  • 13
  • 20