2015-03-03 39 views
0

我現在有一個html5表單,裏面有一個基本的文本區域,當你點擊提交按鈕時,調用一個php腳本將數據發送到數據庫。在調用php函數之前驗證html5表單?

但是,在調用PHP腳本之前,我希望在調用PHP之前在窗體上運行一些驗證(檢查它是否爲空等),最好是在javascript中。

有沒有一種方法可以做到這一點?

謝謝。

Html5的形式:

<form action="submit.php" method="POST"> 
      <tr> 
       <td id="textBox-back"> 
        <textarea id="rage-box" type="text" name="rage" cols="40" rows="5" maxlength="160"> </textarea> 

        <input id="submit" value="" name="submit" type="submit"/> 


       </td>   

     </form>  
+0

是嗎?用Javascript?... Google是你最好的朋友。 – Jordy 2015-03-03 09:44:45

+0

您可以使用這個所需的屬性。但仍然:PHP檢查是強制性的,因爲不是每個瀏覽器都會執行檢查(無論是在javascript中)。 – Blaatpraat 2015-03-03 09:45:11

+0

好的,所以一些PHP是manditory。好的謝謝你的迴應。 – 2015-03-03 09:46:44

回答

0
<script language="javascript"> 
    function validate(){ 

     //Code of validation 

     return false; 
    } 
</script> 


<form action="submit.php" method="POST" onsubmit="return validate();"> 
     <tr> 
      <td id="textBox-back"> 
       <textarea id="rage-box" type="text" name="rage" cols="40" rows="5" maxlength="160"> </textarea> 

       <input id="submit" value="" name="submit" type="submit"/> 


      </td>   

    </form> 
+0

啊,這是行得通的。然而,我可以花更長時間使用Google搜索。感謝您的迴應! +1 – 2015-03-03 09:49:33

0

您需要在提交或窗體上添加一個onclick。

var sub = document.getElementById('submit'); 
var tex = document.getElementById('rage-box'); 
sub.onclick = function(e){ 
    if(tex.value == ''){ 
    //stopping it from being sent 
    e.preventDefault(); 
    alert('make sure everything is filled out'); 
    } 
} 

如果您在窗體上添加事件,你需要:

  1. 給一個ID到表單
  2. 讓你的窗體並添加form.onsubmit =函數(){};
  3. 然後只需複製匿名函數中的代碼即可。