|
lesson55
|
|||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||
|
usign this we will enter the required data at input, it will
search in the database, and print in the same page
|
|||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||
|
index.php
|
|||||||||||||||||||||||||||||||||||
|
<html>
|
|||||||||||||||||||||||||||||||||||
|
<head>
|
|||||||||||||||||||||||||||||||||||
|
<script>
|
|||||||||||||||||||||||||||||||||||
|
function findmatch(){
|
|||||||||||||||||||||||||||||||||||
|
if(window.XMLHttpRequest){xmlhttp = new XMLHttpRequest();}
|
|||||||||||||||||||||||||||||||||||
|
else{xmlhttp =new ActiveXobject('Microsoft.XMLHTTP');}
|
|||||||||||||||||||||||||||||||||||
|
xmlhttp.onreadystatechange = function(){
|
|||||||||||||||||||||||||||||||||||
|
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
|
|||||||||||||||||||||||||||||||||||
|
{document.getElementById('rdiv').innerHTML =
xmlhttp.responseText;}
|
|||||||||||||||||||||||||||||||||||
|
}
|
|||||||||||||||||||||||||||||||||||
|
xmlhttp.open('GET', 'search.php
?s_text='+document.search.s_text.value, true);
|
it will add to that page end , get the data, and print int this
page
|
||||||||||||||||||||||||||||||||||
|
xmlhttp.send();
|
<!-- if no method is mentioned, default as get method, we not
mentoned method, xml get method, have to url add if we have submit , after
clicked in submit, but we want to add using ajax using concatenatin-->
|
||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
}
|
|||||||||||||||||||||||||||||||||||
|
</script>
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
</head>
|
|||||||||||||||||||||||||||||||||||
|
<body>
|
|||||||||||||||||||||||||||||||||||
|
<form name="search" >
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
type keyword :<input type ="text"
name="s_text" onkeyup="findmatch()">
|
if key is entered, automatically get the data from data base and
print in the mentioned rdiv
|
||||||||||||||||||||||||||||||||||
|
</form>
|
|||||||||||||||||||||||||||||||||||
|
<div id ="rdiv" ></div>
|
|||||||||||||||||||||||||||||||||||
|
</body>
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
</html>
|
|||||||||||||||||||||||||||||||||||
|
search.php ?s_text='+document.search.s_text.value
|
using this we will add the information at end in a dynamic way,
get the data from that inpuyt element and concatenate that pag
|
||||||||||||||||||||||||||||||||||
|
xhttp.open("GET", "demo_get.asp?t=" +
Math.random(), true);
|
|||||||||||||||||||||||||||||||||||
|
If you want to
send information with the GET method, add the information
to the URL:
|
xhttp.open("GET", "demo_get2.asp?fname=Henry&lname=Ford", true);
|
||||||||||||||||||||||||||||||||||
|
search.php
|
|||||||||||||||||||||||||||||||||||
|
<?php
|
|||||||||||||||||||||||||||||||||||
|
|
if user entered s, related s information shown in place
|
||||||||||||||||||||||||||||||||||
|
require 'dbconnect.php';
|
connect to data base
|
||||||||||||||||||||||||||||||||||
|
if (isset ($_GET['s_text'])){
|
it is a page like template or api, we not using this page
directly
|
||||||||||||||||||||||||||||||||||
|
$s_text = $_GET['s_text'];
|
we are using this page from another page, custmoer enter any
text in input button, load that to text variable
|
||||||||||||||||||||||||||||||||||
|
$s_text = mysqli_real_escape_string($mycon, $s_text);
|
to protec serever we using this if any ' this , use escape
character, which is entereed in stext ,
|
||||||||||||||||||||||||||||||||||
|
if(!empty($s_text)){
|
if it is note empty
|
||||||||||||||||||||||||||||||||||
|
$query = "SELECT `name` FROM `foods` WHERE `name` LIKE
'$s_text%'";
|
select name column from
foods table and with condition
|
||||||||||||||||||||||||||||||||||
|
if($query_run = mysqli_query($mycon, $query)){
|
run this query against this database
|
||||||||||||||||||||||||||||||||||
|
$num_rows = mysqli_num_rows($query_run);
|
if customer is entered s, it will get who has starting with s.
got some rows
|
||||||||||||||||||||||||||||||||||
|
if ($num_rows >= 1){
|
if it is equl
|
||||||||||||||||||||||||||||||||||
|
while($row = mysqli_fetch_assoc($query_run)){
|
each row print
|
||||||||||||||||||||||||||||||||||
|
echo $row['name'].'<br>';
|
|||||||||||||||||||||||||||||||||||
|
}
|
|||||||||||||||||||||||||||||||||||
|
}
|
|||||||||||||||||||||||||||||||||||
|
}
|
|||||||||||||||||||||||||||||||||||
|
}
|
|||||||||||||||||||||||||||||||||||
|
}
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
?>
|
|||||||||||||||||||||||||||||||||||
|
dbconnect.php
|
|||||||||||||||||||||||||||||||||||
|
<?php
|
connect to database
|
||||||||||||||||||||||||||||||||||
|
$host = 'localhost';
|
|||||||||||||||||||||||||||||||||||
|
$user = 'root';
|
|||||||||||||||||||||||||||||||||||
|
$pass = '';
|
|||||||||||||||||||||||||||||||||||
|
$dbname = 'lesson44';
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
if($mycon = @mysqli_connect($host, $user, $pass)){
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
mysqli_select_db($mycon, $dbname);
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
}
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
?>
|
|||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||
Sunday, 14 June 2020
nter the required data at input, it will search in the database, and print in the same page
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment