Monday, 15 June 2020

how to get the data using check box and ajax

dbconnect.php


<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'lesson44';

if($mycon = @mysqli_connect($host, $user, $pass)){

mysqli_select_db($mycon, $dbname);

}


?>

search.php

<?php
require 'dbconnect.php';
?>

<?php
if(isset($_GET['username']) || isset($_GET['password'] )  ){

if (!empty($_GET['username'])){
$u = $_GET['username'];
$username = trim ($u);
}
if (!empty($_GET['password'])){
$p = $_GET['password'];
$password = trim ($p);
}


@$s_text = $username ." ". $password ;

echo 'u selected' .$s_text.'</br>';
$namesar = explode(' ' , $s_text);
$size= sizeof($namesar);

$i=0;
while ($i < $size)
{
$search= $namesar[$i]  ;
if (!empty($search)){
echo 'search engine started' . $search;
$query = "SELECT `name` FROM `foods` WHERE `name` LIKE '%$search%' ";
$query_run = mysqli_query($mycon, $query);
$num_rows = mysqli_num_rows($query_run);

if ($num_rows >= 1) {
echo '<h1>' .$num_rows.'results found</h1>';
while ($row = mysqli_fetch_assoc($query_run)){
echo $row['name']. '<br><br>';}
}else{echo 'no results found';}
}
else {

}


$i++;
}

}
?>


index.php


<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>

function findmatch(){
var url = "search.php";

if ($("input[name=username]").is( ":checked"))
{var x= "username="+document.search.username.value;} else {var x ="username= ";}
if ($("input[name=password]").is( ":checked"))
{var y= "password="+document.search.password.value;} else {var y ="password= ";}


var params = x +"&"+y;
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', url+"?"+params, true);
xmlhttp.send();

}
</script>

 </head>
<body>
<form name="search" >

<input type="checkbox" name="username" value="ves" onclick="findmatch()" />JavaScript
<input type="checkbox" name="password" value="ges" onclick="findmatch()" />password
<input type="submit" value="submit">
<div id ="rdiv" ></div>
</body>

</html>

No comments:

Post a Comment