2009-10-30 83 views
0

在窗體中,如果驗證通過,則調用PHP文件。我的表頭是這樣的:[HTML/PHP]:中止刷新頁面

<form method="POST" action="inst.php" style="margin:0px;"name="form1" onSubmit="return CheckUsername(document.getElementById('username').value);"> 

</form> 

的問題是,即使驗證失敗,它顯示了一個空白頁,試圖打開PHP文件,當它必須保持在同一頁上。 PHP文件包含訪問數據庫的代碼,以檢查用戶是否存在。

有沒有辦法檢查數據庫的值,而不刷新頁面?

回答

1

儘管您不應該單獨依賴客戶端驗證,並且應該將所有數據都視爲「髒」,但使用JavaScipt可以防止瀏覽器直接發佈表單。只需定義其onsubmit函數來構造XmlHttpResponse對象,將方法設置爲POST並將數據設置爲form.serialize(),然後發送相應的POST請求,而不是設置表單的方法和動作。或者,如果PHP腳本將接受GET或REQUEST參數,則可以(在您驗證之後)構造URL查詢並簡單地將window.location設置爲使用適當的數據重定向到PHP頁面。

編輯 - 這是我的插圖 - 這使用原型的Form.serialize function

<form id="my_form" onSubmit="return checkUsername();"> 
    Username: <input type="text" name="username" id="username" /> 
</form> 

<script type="text/javascript"> 
    var xhr; // global XMLHttpRequest object 
    var formElem = $('my_form'); // our form element 

    function checkUsername() { 
    var formData = formElem.serialize(); 
    sendPOSTRequest('http://mydomain.com/mypath/myscript.php', formData); 
    } 

    function sendPOSTRequest(toURL, sendData) { 
    xhr = false; 
    if (window.XMLHttpRequest) { 
     xhr = new XMLHttpRequest(); 
     if (http_request.overrideMimeType) { 
      http_request.overrideMimeType('text/html'); 
     } 
    } else if (window.ActiveXObject) { 
     try { 
      xhr = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
      xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e) {} 
     } 
    } 
    if (!xhr) { 
     alert('Cannot create XHR'); 
     return false; 
    } 

    xhr.onreadystatechange = handleResponse; 
    xhr.open('POST', toURL, true); 
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    xhr.setRequestHeader("Content-length", sendData.length); 
    xhr.setRequestHeader("Connection", "close"); 
    xhr.send(sendData); 
    } 

    function handleResponse() { 
    if (xhr.readyState == 4) { 
     if (xhr.status == 200) { 
     var result = xhr.responseText; 
     // result is now whatever content was returned by the PHP script 
     // do whatever you want with the result here 
     // for example, you might have the PHP return 'true' or some such thing, and then 
     // change window.location, or perhaps if it returns 'false' you put up an alert('No!') 
     // use your imagination, go nuts 
     } else { 
     alert('The script returned an error.'); 
     } 
    } 
    } 
</script> 

有一些更復雜的方法來創建和處理XMLHttpRequest對象。我稍後可能會發布一些更新。

+0

爲什麼放棄對非JS用戶的支持,以便在JS活動時可以實現不同的功能?我不明白。 – 2009-10-30 13:55:56

+0

我根本不會。我只是指出了一種完成問題的方法。我會建議堅持服務器端驗證,永遠不要依賴JS。 – defines 2009-10-30 14:16:59

+0

@Dustin:你能說明一下嗎? – RKh 2009-10-30 15:25:27

2

JavaScript函數很可能有錯誤。然後驗證功能將不會被執行,併發送表單(!)。檢查Firefox的JavaScript控制檯是否有錯誤,即使頁面已經重新載入,它們也會出現在那裏。

但是,您應該永遠不要依賴客戶端驗證。我強烈建議檢查PHP腳本。

+0

Pekka建議非常好,你應該考慮一些瀏覽器可能已禁用javascript – 2009-10-30 13:13:36

0

一旦發送POST請求,它就由瀏覽器如何處理響應,但在我遇到的每個瀏覽器中都會顯示請求的結果,因爲它是一條消息,說明它收到了迴應(200,404等),空白頁面或其他內容,所以恐怕您必須重新構建頁面並將其發送回客戶端(在表單元素中包含無效條目)作爲響應。

這是一個痛苦,但這就是HTTP的工作原理。