表单提交按钮没有将名称传递给php

时间:2013-06-26 09:33:11

标签: php jquery ajax

可能是一个非常简单的答案,但我正在努力解决。我有一个基本表单,其中有一个标准的提交按钮。当点击它时,它被jquery序列化,然后Ajax处理php页面等等。我很困惑为什么当用户点击它时我的提交按钮'submit'的名称没有被发布。我能想到的唯一原因是序列化只处理文本,文本区域和选择元素。如果是这样,我如何将名称发送到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']))    { }

2 个答案:

答案 0 :(得分:3)

jQuery不会序列化提交按钮:

  

Docs:

     

注意:只有“成功控件”被序列化为字符串。没有   由于表单未提交,因此提交按钮值已序列化   使用按钮。

解决方法是检查$_POST是否为空:

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

或检查是否设置了特定输入。 E.g。

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

 }

答案 1 :(得分:1)

如上所述,提交按钮不是序列化的。一个可能的解决方法:{未经测试,但据我了解,应该工作}

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