2016-04-06 59 views
0

我想創建一個借用按鈕的驗證,當按下但沒有項目沒有檢查它應該顯示提示,但發生了什麼是它不會顯示提示,也不會顯示在數據庫中。我將如何嘗試做到這一點。如何確定複選框是否已被檢查

以下是JavaScript代碼。 (我不知道這是否是正確的,即使)

$(".uniform_on").change(function(){ 
    var max = 1; 
    if ($(".uniform_on:checked").length < max) { 
     $(".uniform_on").attr('disabled', 'disabled'); 
     alert('Please Check the item of equipment to be borrowed!'); 
     $(".uniform_on:checked").removeAttr('disabled'); 
    } else { 
     $(".uniform_on").removeAttr('disabled'); 
    } 
}) 

請幫助我不知道大部分的代碼,因爲我只定製我自己的代碼,我對這種東西有限的知識。如果在未勾選複選框的情況下按下借用按鈕,該怎麼辦;我如何驗證這一點,即用戶在再次按借前先檢查某些東西。謝謝。

這是我借的整個代碼

<?php include('header.php'); ?> 
<?php include('session.php'); ?> 
<?php include('navbar_borrow.php'); ?> 
    <div class="container"> 
     <div class="margin-top"> 
      <div class="row"> 
           <div class="alert alert-info"> 
            <button type="button" class="close" data-dismiss="alert">&times;</button> 
            <strong><i class="icon-user icon-large"></i>&nbsp;Borrow Table</strong> 
           </div> 

     <div class="span12">   

     <form method="post" action="borrow_save.php"> 
<div class="span3"> 

              <div class="control-group"> 
       <label class="control-label" for="inputEmail">Borrower Name</label> 
       <div class="controls"> 
       <select name="member_id" class="chzn-select"required/> 
       <option></option> 
       <?php $result = mysql_query("select * from member")or die(mysql_error()); 
       while ($row=mysql_fetch_array($result)){ ?> 
       <option value="<?php echo $row['member_id']; ?>"><?php echo $row['firstname']." ".$row['lastname']; ?></option> 
       <?php } ?> 
       </select> 
       </div> 
      </div> 
       <div class="control-group"> 
        <label class="control-label" for="inputEmail">Due Date</label> 
        <div class="controls"> 
        <input type="text" class="w8em format-y-m-d highlight-days-67 range-low-today" name="due_date" id="sd" maxlength="10" style="border: 3px double #CCCCCC;" required/> 
        </div> 
       </div> 
       <div class="control-group"> 
        <div class="controls"> 

           <button name="delete_student" class="btn btn-success"><i class="icon-plus-sign icon-large"></i> Borrow</button> 

        </div> 
       </div> 
       </div> 
       <div class="span8"> 
         <div class="alert alert-success"><strong>Select Equipment</strong></div> 
          <table cellpadding="0" cellspacing="0" border="0" class="table" id="example"> 

           <thead> 
            <tr> 

             <th>Acc No.</th> 
             <th>Equipment Description</th>         
             <th>Category</th> 
             <th>Quantity</th> 
             <th>status</th> 
             <th>Add</th> 

            </tr> 
           </thead> 
           <tbody> 

            <?php $user_query=mysql_query("select * from equipment where status != 'Archive' ")or die(mysql_error()); 
            while($row=mysql_fetch_array($user_query)){ 
            $id=$row['equipment_id']; 
            $cat_id=$row['category_id']; 

              $cat_query = mysql_query("select * from category where category_id = '$cat_id'")or die(mysql_error()); 
              $cat_row = mysql_fetch_array($cat_query); 
            ?> 
            <tr class="del<?php echo $id ?>"> 


            <td><?php echo $row['equipment_id']; ?></td> 
            <td><?php echo $row['equipment_description']; ?></td> 
            <td><?php echo $cat_row ['classname']; ?> </td> 
            <td><?php echo $row['quantity']; ?> </td> 
            <?php /* <td><?php echo $row['equipment_description']; ?></td> */ ?> 
             <td width=""><?php echo $row['status']; ?></td> 
            <?php include('tooltip_edit_delete.php'); ?> 
            <td width="20"> 
               <input id="" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>" > 

            </td> 

            </tr> 
            <?php } ?> 
           </tbody> 
          </table> 

       </form> 
      </div>  
      </div>  
<script>   
$(".uniform_on").change(function(){ 
    var max = 1; 
    if($(".uniform_on:checked").length < max){ 

     $(".uniform_on:checked").attr('disabled', 'disabled'); 
       alert('Please Check the item of equipment to be borrowed!'); 
     $(".uniform_on:checked").removeAttr('disabled'); 

    }else{ 

     $(".uniform_on").removeAttr('disabled'); 
    } 
}) 
</script>  
      </div> 
     </div> 
    </div> 
<?php include('footer.php') ?> 

做出改變和它驗證部分現在對於交易的一部分,我想借用它不會保存到數據庫工作但它只是提示你已經借 CODE新借按鈕:

<input name="delete_student" class="btn btn-success" type="button" value="Borrow" onClick="return validationfunction();" /> 

CODE爲的javascript:

<script> 
function validationfunction() { 
    if($(".uniform_on:checked").length > 0) { 
     alert('Equipment has been borrowed.'); 
     return true 
    } 
    else { 
     alert('Please check the item of equipment to be borrowed!'); 
     return false; 
    } 
} 
</script>  
+2

這個我JavaScript和jQuery,而不是PHP ...另外,我看到0 ajax。 – SpYk3HH

+0

向我們顯示你的'html' –

回答

2

首先它是一個javascript和jQuery代碼。

點擊bowrrow按鈕調用該函數來檢查複選框是否被選中或不在下面。

function validationfunction() { 
    if($('.uniform_on').is(':checked')) { 
     // Your code goes here. 
    } 
    else { 
     alert('Please Check the item of equipment to be borrowed!'); 
     return false; 
    } 
} 

請點擊以下按鈕來調用此函數。

<input type="submit" value="Borrow Button" onClick="return validationfunction();" /> 
+0

你也可以直接返回'$('。uniform_on')。is(':checked')'而不使用if/else語句。 事實上,這裏最大的問題是他不會在按鈕點擊時調用驗證函數... – vdubus

+0

確實,不需要第一個功能。編輯!他正在「試圖創建一個按鈕的驗證按鈕」,所以他可以使用上述功能進行驗證。 –

+0

我不明白?諾言;我將在那裏插入什麼代碼,然後如何將它稱爲借用按鈕? – Chouny

1

As @ SpYk3HH已經提到您在這裏發佈的代碼部分不是PHP。但根據閱讀您的完整帖子。我可以猜測,您在後端(PHP)端收到發佈數據時基本上遇到了一些問題。

實際上,HTML Element Checkbox通常扮演BOOL值(真/假)的角色。所以如果複選框被選中,您將獲得發佈數據中的複選框值,否則您將不會收到它。

如何處理這種情況:

好了,這是因爲與你的代碼玩,你應該在數據庫中插入數據之前作出的數組:

$myData = array(); 
$myData['key1'] = $_POST['key1']; 
$myData['key2'] = $_POST['key2']; 
/* 
    As you can see we are handeling if the Checkbox was not seleted and giving it the value '0' in this case. 
*/ 
$myData['checkbox_key'] = !empty($_POST['checkbox_key']) ? $_POST['checkbox_key'] : 0; 


// then insert your $myData array in to database or do anything you wants to do. 

此外,您也可以在提交表單之前驗證您的複選框:

function validate_it(){ 
    if($(".yourClassName:checked").length > 0){ 
     // Submit it or anything else 
    }else{ 
     // alert or warn the user! 
    } 
} 
+0

請記住,如果他使用jQuery來驗證頁面,未選中的複選框將不會被序列化,因此'$ _POST ['key']'可能不存在 – SpYk3HH

+0

如何提交表單而不檢查任何內容我應該如何編輯這個代碼? – Chouny

+0

'$ myData ['checkbox_key'] = isset($ _ POST ['checkbox_key'])? $ _POST ['checkbox_key']:0;' – SpYk3HH

相關問題