2013-06-26 76 views
1

可能是一個非常簡單的答案,但我正在努力。我有一個基本的表單,它有一個標準的提交按鈕。當它被點擊時,它被jquery序列化,然後Ajax處理php頁面等。我很困惑,爲什麼當用戶點擊它時,我的提交按鈕'submit'的名稱沒有被髮布。我能想到的唯一原因是序列化僅處理文本,文本區域和選擇元素。如果是這樣,我怎麼才能發送名稱通過PHP進行處理。我已經包含了一些示例代碼,如果有人能指出我正確的方向,我將不勝感激。由於表單提交按鈕不傳遞名稱到PHP

HTML表單

<!--- Form to add box --> 

<div id="boxaddform" style="display:none;"> 
    <div class="dialogTop_padd"></div> 
     <form id="BA_boxform" name="BA_boxform" method="post"> 

     <fieldset> 
     <legend><span></span>Select Company</legend> 
      <div class="spacer"></div> 
     <div class="formMessage">Click again to open</div> 
     <div class="fld_fld"> 

     <div> 
     <label for="BA_customer">Company:</label><br /> 
     <select name="BA_customer" id="BA_customer"> 
     <option SELECTED VALUE="">Select a Company</option> 
     <?php 
     do { 
     ?> 
     <option value="<?php echo $row_Recordsetcust['customer']?>"><?php echo $row_Recordsetcust['customer']?></option> 
     <?php 

     } 
     while ($row_Recordsetcust = mysql_fetch_assoc($Recordsetcust)); 
     $rows = mysql_num_rows($Recordsetcust); 

     if($rows > 0) 

     { 
     mysql_data_seek($Recordsetcust, 0); 
     $row_Recordsetcust = mysql_fetch_assoc($Recordsetcust); 
     } 

     ?> 
     </select> 

     <div class="spacer"></div> 
     <!--- displays the address and dept from the change function --> 
     <div id="BA_dept"></div> 
     <br /> 
     <div id="BA_address"></div> 

      </div> 
     </fieldset> 
     <div class="dialogTop_padd"></div> 
     <!--- fieldset for service level --> 
     <fieldset> 
     <legend>Service Level</legend> 
     <div class="spacer"></div> 
     <div> 
     <label for="BA_service">Service level:</label> 
     <select name="BA_service" id="BA_service"> 
     <option SELECTED VALUE="">Select an option</option> 
     <option value="Standard">Standard</option> 
     <option value="Rapid">Rapid</option> 
     </select><br /> 
     </div> 
     </fieldset> 
     <div class="dialogTop_padd"></div> 

     <!--- fieldset for box # --> 
     <fieldset> 
     <legend>Box Details</legend> 
     <div class="spacer"></div> 
     <div> 
     <label for="BA_box">Box#:</label><br /> 
     <input id="BA_box" name="BA_box" type="text" size="32" maxlength="128" /><br /> 
     </div> 

     <div> 
     <label for="BA_destdate">Destroy date:</label> 
     <input id="BA_destdate" name="BA_destdate" type="text" size="32" maxlength="128" value = "" /><br /> 
     </div> 
     </fieldset> 
     <div class="dialogTop_padd"></div> 
     <!--- fieldset for authorisation --> 
     <fieldset> 
     <legend>Authorisation</legend> 
     <div class="spacer"></div> 
     <div> 
     <label for="BA_authorised">Requested By:</label> 
     <input id="BA_authorised" name="BA_authorised" type="text" value="<?php echo $_SESSION['kt_name_usr']; ?>"><br /> 
     </div> 
     </fieldset> 

     <!--- div to show callback result from ajax via dialog --> 
      <br />  
      <div id="BA_addbox"></div> 
      <br /> 
      <input class="AddBoxSubmitButton" type="submit" id="submit" name="submit" value="Add Box" /> 
      <input class="AddBoxResetButton" type="reset" name="cancel" value="Clear Form" /> 
     <!--- buttons to submit form and reset form to default status --> 
     <!-- <button id="BA_submit" class="submitbutton icon-right ui-state-default2 ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Add Box</button> 
     <button type="reset" id="BA_reset" class="resetbutton icon-right ui-state-default2 ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Reset</button> 
     --><br /> 

     </form> 
      <br /> 
    </div> 

jQuery代碼用於說明

$(function(){   
     $("#BA_boxform").submit(function(){ 

     var formdata = $('#BA_boxform').serialize(); 
     alert(formdata); 
     $.ajax({ 
      type: "POST", 
      url: "/domain/admin/requests/boxes/boxesadd.php", 
      data: formdata, 
      dataType: 'json', 
      success: function(msg){ 
       //$("#confirm_department").hide(); 

       /* 
       var $dialog = $('<div id="dialog"></div>') 
       .html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.'); 
       $dialog.dialog({ 
       autoOpen: true, 
       modal: true, 
       title: 'Box intake submission successfull', 
       width: 400, 
       height: 200, 
       draggable: false, 
       resizable: false, 
       buttons: { 
       Close: function() { 
       $(this).dialog("close"); 
       } 
       } 
       }); 
       */ 
       //alert('You have succesfully submitted your ' + msg.company + ' report. Thank you.'); 
       //console.log(msg); 
       $("#BA_addbox").show(); 

       //$("#formImage .col_1 li").show(); 
       $("#BA_boxform").get(0).reset(); 
       //$("#boxaddform").hide(); 
      } 
     }); 
     return false; 
    }); 
}); 

// End function to submit box intake form 

切開PHP代碼

if (isset($_POST['submit'])) { } 
+0

這是'alert(formdata);''的輸出? – Ander2

+0

@ Ander2發送的所有數據除了提交之外都處於警報狀態。 – user1532468

回答

1

就像說的那樣,提交按鈕不會被序列化。一個可能的解決方法:{未經測試,但據我瞭解,應該工作}

$('#BA_boxform input:submit').on('click', function() { 
    var formdata = $('#BA_boxform').serialize() + '&submit=' + $(this).val(); 
    //.... 
    return false; 
}); 
+0

這會起作用,但乾燥原理呢? – Leri

+0

@PLB DRY原理是什麼? – user1532468

+0

@ user1532468 http://en.wikipedia.org/wiki/Don't_repeat_yourself – Leri

3

jQuery將不序列提交按鈕:

Docs:

注意:只有「成功的控件」被序列化爲字符串。沒有 提交按鈕值被序列化,因爲表單未提交 使用按鈕。

解決方法是檢查是否$_POST不爲空:

if (!empty($_POST)) { 
    // Your code here 
} 

或檢查特定的輸入設置。例如。

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

} 
+0

但是在這裏:http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2你看到這個:「如果一個表單包含多個提交按鈕,只有激活的提交按鈕是成功「。 – Cherniv

+1

@Cherniv - 提交按鈕未被激活。它被點擊。 JavaScript運行。它的激活被JS取消。 – Quentin

+0

@Cherniv,屬實,但這種情況下的表單不是通過常規提交按鈕提交的,而是通過javascript調用提交的。 (該死的電話和錯過的頁面刷新!:D) –