php - Page refreshing on Submit -
i'm following tutorial:
http://www.plus2net.com/php_tutorial/ajax-search.php
they provided me i'm looking for. problem i'm facing is, when press enter button, page refreshing :( don't want that.
demo:
http://www.plus2net.com/php_tutorial/ajax-search-demo.htm
html code
<html> <body> <style> #displaydiv { background-color: #ffeeaa; width: 200; } </style> <script type="text/javascript"> function ajaxfunction(str) { var httpxml; try { // firefox, opera 8.0+, safari httpxml = new xmlhttprequest(); } catch (e) { // internet explorer try { httpxml = new activexobject("msxml2.xmlhttp"); } catch (e) { try { httpxml = new activexobject("microsoft.xmlhttp"); } catch (e) { alert("your browser not support ajax!"); return false; } } } function statechanged() { if (httpxml.readystate == 4) { document.getelementbyid("displaydiv").innerhtml = httpxml.responsetext; } } var url = "ajax-search-demock.php"; url = url + "?txt=" + str; url = url + "&sid=" + math.random(); httpxml.onreadystatechange = statechanged; httpxml.open("get", url, true); httpxml.send(null); } </script> </head> <body> <form name="myform"> <input type="text" onkeyup="ajaxfunction(this.value);" name="username" /> <div id="displaydiv"> </div> </form> </body> </html>
php code (ajax-search-demock.php)
<? //*************************************** // downloaded www.plus2net.com // /// can distribute code link www.plus2net.com /// // please don't remove link www.plus2net.com /// // learning not commercial use. /////// //the author not responsible type of loss or problem or damage on using script.// /// can use @ own risk. ///// //***************************************** //error_reporting(e_error | e_parse | e_core_error); ////// database details here ///////// $dbservertype='mysql'; $servername='127.0.0.1'; $dbusername='test'; $dbpassword='test'; $dbname='sql_tutorial'; //////////////////////////////////////// ////// donot edit below ///////// /////////////////////////////////////// connecttodb($servername,$dbname,$dbusername,$dbpassword); function connecttodb($servername,$dbname,$dbuser,$dbpassword) { global $link; $link=mysql_connect ("$servername","$dbuser","$dbpassword"); if(!$link){die("could not connect mysql");} mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); } ///////////////////// main code sarts //////// $in=$_get['txt']; $msg=""; if(strlen($in)>0 , strlen($in) <20 ){ $t=mysql_query("select name, id student name '$in%'"); while($nt=mysql_fetch_array($t)){ $msg.=$nt[name]."->$nt[id]<br>"; } } echo $msg; ?>
update :
ok enter thing solved :d great!!.. have 1 more request please
i want data when input reaches 8 characters. possible ?
you're not submitting anything, technically don't need <form>
element @ all, remove , leave rest is.
alternatively, if you're stickler strict standards, set action="javascript:void(null)
on form prevent doing anything.
Comments
Post a Comment