2013-05-02 52 views
-2

我下面這個教程:頁面清爽的提交

http://www.plus2net.com/php_tutorial/ajax-search.php

他們爲我提供了我要找的。但我現在面臨的唯一問題是,當我按下回車鍵,頁面清爽:(但我並不想這樣做,

DEMO:

http://www.plus2net.com/php_tutorial/ajax-search-demo.htm

HTML代碼

<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 does 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的搜索demock.php)

<? 
//*************************************** 
// This is downloaded from www.plus2net.com // 
/// You can distribute this code with the link to www.plus2net.com /// 
// Please don't remove the link to www.plus2net.com /// 
// This is for your learning only not for commercial use. /////// 
//The author is not responsible for any type of loss or problem or damage on using this script.// 
/// You can use it at your own risk. ///// 
//***************************************** 
//error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR); 
////// Your 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 to 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 and strlen($in) <20){ 
    $t=mysql_query("select name, id from student where name like '$in%'"); 
    while($nt=mysql_fetch_array($t)){ 
    $msg.=$nt[name]."->$nt[id]<br>"; 
    } 
} 
echo $msg; 

?> 

UPDATE:

OK回車事情就解決了:D這實在是太棒了!!多了一個請求,請

我想只有當輸入達到8個字符來獲取數據。可能嗎 ?

+0

'onkeyup =「ajaxFunction(this.value);」'只需添加**返回false **像這樣'onkeyup =「ajaxFunction(this.value); return假' – dreamweiver 2013-05-02 14:17:02

+0

在現實世界的情況下,讓它與你一起工作t JavaScript! – Ryan 2013-05-02 14:36:25

回答

0

實際上,您並未提交任何內容,因此從技術上講,根本不需要<form>元素,只需將其刪除即可。

或者,如果您是嚴格標準的堅持者,請在表單上設置action="javascript:void(null)以防止其執行任何操作。

+0

這裏沒有提交按鈕,標籤的存在問題也是如此嗎? – dreamweiver 2013-05-02 14:17:59

+0

即使沒有提交按鈕,當按下Enter鍵時,這是包含單個輸入的表單的默認行爲。如果存在多個表單元素,則標準行爲是模擬表單中第一個提交按鈕的點擊。 – 2013-05-02 14:19:11

+0

哦,好吧,這對我來說是新東西:)無論如何謝謝:) – dreamweiver 2013-05-02 14:22:33

0

您可以向表單添加一個提交按鈕(並隱藏在您的CSS中)並將以下內容放入其中;

<input type="submit" name="submit" value="Submit" onclick="return false;"/> 
+0

工作:D非常感謝toby :) – 2013-05-02 14:23:59

+0

但是當我試圖選擇這個答案。 TICK標記未到:(正在嘗試... – 2013-05-02 14:24:40

+0

我認爲它只會讓你在經過這麼多分鐘後,很高興它爲你工作! – Toby 2013-05-02 14:38:29

0

製作如下更改,也將努力:

返回FALSE;

此行之後:

httpxml.send(空);

AND

的onkeyup = 「返回ajaxFunction(THIS.VALUE);」

的inslead:

的onkeyup =「ajaxFunction(此。值); 「

,或者您還可以添加按照形成標籤:

的onsubmit = 」返回false;「

與@Kolink同意」 如果你不這樣做想要提交日期,則不需要表單標籤