-3

<html>
<head>
<script type="text/javascript" src="jquery-3.0.0.min.js"></script>
</head>
<body>
<form method="POST">
<input type="text" class="form-control"  placeholder="Search" name ="searchterm">
<a href="#" id="button"><button type="submit" id="submit">click me</button></a>                           
</form>
<script>
$(document).ready(function(){
$("#button").click(function(){
$.get("search.php");
return false;
});
});
</script>
</body>
</html>

above is the html file of my code search.html

<?php
mysql_connect("localhost","root","");
mysql_select_db("testdb");
$search = mysql_real_escape_string(trim($_POST['searchterm']));
$find_categories = mysql_query("SELECT `person3`,`person4` FROM `dbtable` WHERE `person1` LIKE'%$search%'");
$data = array();
while($row = mysql_fetch_assoc($find_categories))
{
$data[]=$row;
}   
echo json_encode($data);
?>

and this is my php file search.php

I am having problem in fetching data from my database and displaying it. How can i do it and is there any way so that i can display it on a popup?

Community
  • 1
  • 1
sharad gupta
  • 31
  • 1
  • 6
  • 1
    Please bring your code formatted for easier reading. What is output of `search.php`? – yergo Jun 21 '16 at 10:39
  • Even though its not the cause of your problem, the [`mysql`](http://php.net/manual/en/intro.mysql.php) extension was officially deprecated in PHP v5.5.0 and removed in PHP v7. You should use the [`mysqli`](http://php.net/manual/en/intro.mysqli.php) extenstion or [`PDO`](http://php.net/manual/en/intro.pdo.php) instead. You should also look into using prepared statements to [protect yourself from SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Ben M. Jun 21 '16 at 10:46

1 Answers1

0

this is the output after removing the jquery and writing php in the same file


<!doctype html>
<html>
<head>
<script type="text/javascript" src="jquery-3.0.0.min.js"></script>
</head>
<body>
<form method="POST">
<input type="text" class="form-control"  placeholder="Search"  name="searchterm">
<a href="#" id="popup"><button type="submit" id="submit">click me</button> </a>                           
<?php
mysql_connect("localhost","root" ,"");
mysql_select_db("testdb");
$search = mysql_real_escape_string(trim($_POST['searchterm']));
$find_categories = mysql_query("SELECT `person3`,`person4` FROM `dbtable` WHERE `person1` LIKE'%$search%'");
$data = array();
while($row = mysql_fetch_assoc($find_categories))
{
$data[]=$row;
}   
echo json_encode($data);
?>
</form>
</body>
</html>

search.php


sharad gupta
  • 31
  • 1
  • 6