Search data from array in php -
i want data php array , show on same page. how import data php array using search box. code not working properly. th error of code?
foodstore.js
var xmlhttp = createxmlhttprequestobject(); function createxmlhttprequestobject(){ var xmlhttp; if(window.activexobject){ try{ xmlhttp = new activexobject("microsoft.xmlhttp"); }catch(e){ xmlhttp = false; } } else{ try{ xmlhttp = new xmlhttprequest(); }catch(e){ xmlhttp = false; } } if(!xmlhttp) alert("cant create object hoss!"); else return xmlhttp; } function process(){ if(xmlhttp.readystate==0|| xmlhttp.readystate==4){ food = encodeuricomponent(document.getelementbyid("userinput").value); xmlhttp.open("get","foodstore.php?food="+food,true); xmlhttp.onreadystatechange = handleserverresponse; xmlhttp.send(null); }else{ settimeout('process()',1000); } } function handleserverresponse(){ if(xmlhttp.readystate==4){ if(xmlhttp.status==200){ xmlresponse = xmlhttp.responsexml; xmldocumentelement = xmlresponse.documentelement; message = xmldocumentelement.firstchild.data; document.getelementbyid("underinput").innerhtml ='<span style="color:blue">'+message+'</span>'; settimeout('process',1000); }else{ alert('something went wrong!'); } } }
foodstore.php
<?php header('content-type: text/xml'); echo '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'; echo '<response>'; $food = $_get['food']; $foodarray = array('tuna','bacon','beef','loaf','ham'); if(in_array($food,$foodarray)) echo 'we have '.$food'!'; elseif($food =='') echo 'enter food want buy'; else echo 'sorry don't sell '.$food'!'; echo '</response>'; ?>
index.html
<html><head> <script type="text/javascript" src="foodstore.js"></script> </head> <body onload="process()"> <h3>the foods </h3> order foods: <input type="text" id="userinput"></input> <div id="underinput"></div> </body> </html>
how show array data searching search box
i have changed code using jquery simple . can try it.
index.html
<html> <head> <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script> $(function() { $("#userinput").keyup(function() { process(); }); $("#userinput").keydown(function() { process(); }); $("#userinput").focus(function() { process(); }); $("#userinput").change(function() { process(); }); }); function process() { var input_food = $("#userinput").val(); $.ajax({ type: "get", url: "foodstore.php", data: {food: input_food}, success: function(message) { $("#underinput").html('<span style="color:blue">' + message + '</span>'); }, error: function() { $("#underinput").html('<span style="color:red">some error occured</span>'); } }); } </script> </head> <body > <h3>the foods </h3> order foods: <input type="text" id="userinput" ></input> <div id="underinput"></div> </body> </html>
foodstore.php
<?php if (!empty($_get['food'])) { $food = $_get['food']; $foodarray = array('tuna', 'bacon', 'beef', 'loaf', 'ham'); if (in_array($food, $foodarray)) echo "we have " . $food . "!"; elseif ($food == '') echo "enter food want buy"; else echo "sorry don't sell " . $food . "!"; } else { echo "enter food want buy"; } ?>
i think simple if know jquery .and there simple error in php did't escape single quotes in (don't) used double quotes echo statements. copy paste , tell if want or not.got doubt ask.
Comments
Post a Comment