如何使用ajax PHP将输入框中的数组值插入到我的表中

时间:2018-03-23 10:07:15

标签: php jquery ajax

我有几个带有数组的文本框,它们处理作者到我的表的插入,我有一个问题就是使用ajax将它们插入到我的表中,我对这种方法有点新意,所以很想转换它。

<input type="text" class="form-control text-center" id="author_lname_add[]" name="author_lname_add[]" placeholder="Last Name" required>
<input type="text" class="form-control text-center" placeholder="First Name" id="author_fname_add[]" name="author_fname_add[]" required>
 <input type="text" class="form-control text-center" id="author_mname_add[]" name="author_mname_add[]" placeholder="Middle Name / Initial" required>

AJAX SCRIPT

 $(document).on("click", "#btn_addbook", function(e){
           e.preventDefault();
    var getbook_authorfname = $("#author_fname_add").val();
                 var getbook_authormname = $("#author_mname_add").val();
                 var getbook_authorlname = $("#author_lname_add").val();
    var whatprocess = "ADDBOOK";
                        $.ajax({
                                url: "adminfunctions.php",
                                method: "POST",

                                data: {getbook_authorfname:getbook_authorfname ,
                                getbook_authormname:getbook_authormname, 
                                getbook_authorlname:getbook_authorlname , 
                                whatprocess : whatprocess},


                                success: function(data) {
                                    var getdata = data.trim();
                                    if (getdata == "SUCCESS") {
                                        swal({
                                                title: 'Success!',
                                                text: '',
                                                type: 'success',
                                                confirmButtonClass: "btn btn-success",
                                                buttonsStyling: false
                                            }).then(function() {

                                        });
                                    }
                                    else {
                                        swal({
                                            title: 'Sorry for the inconvenience!',
                                            text: "There's a problem. Please contact the technical support for any concerns and questions.!",
                                            type: 'error',
                                            confirmButtonClass: "btn btn-danger",
                                            buttonsStyling: false
                                        }).catch(swal.noop)
                   }
             },
                error: function(jqXHR, exception) {
                console.log(jqXHR);
            }
         });
    });

1 个答案:

答案 0 :(得分:0)

将输入标记包裹在<form></form>

周围
<form id="bookform">
<input type="text" class="form-control text-center" id="author_lname_add[]" name="author_lname_add[]" placeholder="Last Name" required>
<input type="text" class="form-control text-center" placeholder="First Name" id="author_fname_add[]" name="author_fname_add[]" required>
 <input type="text" class="form-control text-center" id="author_mname_add[]" name="author_mname_add[]" placeholder="Middle Name / Initial" required>
</form>

在您的AJAX中,只需序列化表单,您将获得要发送到PHP代码的所有值。

$(document).on("click", "#btn_addbook", function(e){
    e.preventDefault();
    var data = $("#bookform").serialize()+"&whatprocess=ADDBOOK";
        $.ajax({
        url: "adminfunctions.php",
                            method: "POST",

                            data: data,


                            success: function(data) {
                                var getdata = data.trim();
                                if (getdata == "SUCCESS") {
                                    swal({
                                            title: 'Success!',
                                            text: '',
                                            type: 'success',
                                            confirmButtonClass: "btn btn-success",
                                            buttonsStyling: false
                                        }).then(function() {

                                    });
                                }
                                else {
                                    swal({
                                        title: 'Sorry for the inconvenience!',
                                        text: "There's a problem. Please contact the technical support for any concerns and questions.!",
                                        type: 'error',
                                        confirmButtonClass: "btn btn-danger",
                                        buttonsStyling: false
                                    }).catch(swal.noop)
               }
         },
            error: function(jqXHR, exception) {
            console.log(jqXHR);
        }
     });
});

Serialize会为您生成一个字符串key=value&key1=value1。 在您的PHP代码中,您可以获得此值$_POST['key'];