2012-08-29 19 views
0

我需要檢查多個textareas是否爲空,然後才能在mysql數據庫中寫入任何數據。 表格是動態生成的,我不知道textareas的id /名稱。在發佈之前驗證多個textareas到mysql

形成多個,動態生成的文字區域,身份證和名字動態生成的,以及:

<form action=\"$self\" method=\"POST\" id=\"daily_frm\" enctype=\"multipart/form-data\"> 
<table border=\"1\" cellspacing=\"2\" cellpadding=\"2\" id=\"tb_daily\" class=\"tb_daily\" width=\"100%\"> 
    <tr> 
    <th scope=\"col\" id=\"col_stn\">Station</th> 
    <th scope=\"col\" id=\"col_comm\">Comment</th> 
    <th scope=\"col\" id=\"qcif\">QCIF</th> 
    <th scope=\"col\" id=\"attach\">Attachment</th> 
</tr>"; 

while ($stn_b = mysql_fetch_array($station_b)){ 
    print "<tr> 
      <td>$stn_b[station]</td> 
      <td><textarea id=\"$stn_b[station]_comment\" cols=\"60\" rows=\"5\" value=\"\"></textarea></td> 
      <td><input type=\"checkbox\" id=\"$stn_b[station]_qcif\" value=\"1\"/></td> 
      <td><input id=\"$stn_b[station]_attach\" type=\"file\" /> 
     </tr>  
     "; 
} 

print" 
<tr><td colspan=\"4\"><input type=\"submit\" value=\"Submit\" name=\"submit_daily\" id=\"submit_daily\"><input type=\"reset\" name=\"clear_daily\" value=\"Clear\"></td></tr> 
</table> 
</form> 

我的方法來檢查,如果點擊提交按鈕時,任何文本區域爲空:

<script type='application/javascript'> 

$(document).ready(function(){ 

    $('#daily_frm').submit(function() { 
    $('#daily_frm textarea').each(function(){ 
    if ($(this).val()==='') { 

    alert($(this).attr('id') + ' field is empty'); 
    $(this).focus(); 
    return false; 

    } 
}); 
    }); 
}); 

我使用相應的textarea名稱獲取警報框。

我的PHP代碼寫什麼東西都往DB:

if (isset($_POST['submit_daily'])){ 

while ($stn_b = mysql_fetch_array($station_b)){ 
    $s = $stn_b['station']; 
    $sid = $stn_b['idtb_station']; 

    $daily_comment = addslashes($_POST[$s."_comment"]); 


    if(!empty($_POST[$s."_qcif"])) { 
     $qcif = $_POST[$s."_qcif"]; 
     echo "$qcif<br />"; 
    } else { 
     $qcif = "0"; 
    } 


    $post_daily_sql = "INSERT INTO tb_daily tb_station_idtb_station,date,author,comment,upload) VALUES ('$sid','$today','$user','$daily_comment','$qcif')"; 
} 
} 

我現在的問題是,在一個空的文本區域的情況下,我會得到警告框,但我的腳本仍然試圖寫入繼續db(我知道php/mysql部分不完整)。 任何提示?謝謝!

回答

0

嘗試類似:

$(document).ready(function(){ 
    $('#daily_frm').submit(function (evt) { 
     evt.preventDefault(); 
     var errCount = 0; 
     $('#daily_frm textarea').each(function(){ 
      if ($(this).val()==='') { 
       alert($(this).attr('id') + ' field is empty'); 
       $(this).focus(); 
       errCount++; 
      } 
     }); 
     if(errCount > 0) { 
      reutrn false; 
     } 
     else { 
      return true; 
     } 
    }); 
}); 
+0

謝謝你的提示,做工精細! – user1631975

相關問題