在jquery函数

时间:2018-04-06 09:52:12

标签: jquery

我的每个功能都出错了 我试图在每个声明中组合所有输入,但它在检查工具中给我一个错误。

HTML CODE

<input type="text" name="publishername[]" class="form-control publishername">
<input type="text" name="publisherlocation[]" class="form-control publisherlocation" >
<input type="text" name="publisheremail[]" class="form-control publisheremail">

脚本功能

$("input[name='publishername[]' ,input[name='publisherlocation[]' , input[name='publisheremail[]'  ").each(function () { /// error here

这是我将数据发送到查询的方式。我试图实现这种输出。如果文本框中输入的数据存在,它将执行数据更新,否则它将数据插入到我的表中。

    $(document).on("click", "#btn_add_publisher", function(event) {
    var getpublishers = [];
    event.preventDefault();
    $("input[name='publishername[]'], input[name='publisherlocation[]'], input[name='publisheremail[]']").each(function() {

        var getpublishername = $("publishername").val();
        var getpublisherlocation = $("publisherlocation").val();
        var getpublisheremail = $("publisheremail").val();
        var whatprocess = "add_publisher_process";
        $.ajax({
            url: "adminfunctions.php",
            async: false,
            method: "POST",
            data: {
                getpublishername: getpublishername,
                getpublisherlocation: getpublisherlocation,
                getpublisheremail: getpublisheremail,
                whatprocess: whatprocess
            },
            success: function(data) {
                var getdata = data.trim();
                getpublishers.push(getdata);
                if (getdata == "SUCCESSINSERT") {
                    swal({
                        title: 'Success!',
                        text: 'Publisher Added',
                        type: 'success',
                        confirmButtonClass: "btn btn-success",
                        buttonsStyling: false
                    }, function() {

                    });
                } else if (getdata == "SUCCESSUPDATED") {
                    swal({
                        title: 'Success!',
                        text: 'Publisher Updated',
                        type: 'success',
                        confirmButtonClass: "btn btn-success",
                        buttonsStyling: false
                    }, function() {

                    });
                } else if (getdata == "ERROREMPTY") {
                    swal({
                        title: 'Oops..',
                        text: 'You must enter a value to proceed',
                        type: 'warning',
                        confirmButtonClass: "btn btn-warning",
                        buttonsStyling: false
                    }).catch(swal.noop)
                } 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-info",
                        buttonsStyling: false
                    }, function() {

                    });
                }
            },
            error: function(jqXHR, exception) {
                console.log(jqXHR);
            }
        });


    });

});

选择查询

$query = "SELECT publisher_name , publisher_location , publisher_email FROM tbl_publisher_add WHERE publisher_name = ? AND publisher_location = ? AND publisher_email = ?";

动态文本框图片 here is the picture

它只插入第一个文本框数据 table image

控制台日志console log picture

2 个答案:

答案 0 :(得分:0)

您需要单独选择元素,而不是一组:

$("input[name='publishername[]'], input[name='publisherlocation[]'], input[name='publisheremail[]']").each(function () {
  // your logic...
});

答案 1 :(得分:0)

为每个元素提供类,按类可以访问每个元素值

<form id="publisherForm">
<input type="text" name="publisher[name][]" class="data form-control publishername">
<input type="text"  name="publisher[location][]" class="data form-control 
 publisherlocation" >
 <input type="text"  name="publisher[email][]" class="data form-control publisheremail">
 </form>

脚本文件

var form = $('#publisherForm');
$.ajax( {
  type: "POST",
  url: your url,
  data: form.serialize(),
  success: function( response ) {
    console.log( response );
  }
} );

现在访问服务器文件中的变量$_POST['publisher'] 使用您可以通过

访问每个
$publisher = $_POST['publisher'];
$index=0;
$namearr=$publisher['name'];
foreach($namearr as $value){
 $name=$value;
 $location=$publisher['location'][$index];
 $email=$publisher['email'][$index];
 $index++;
 $query = "SELECT publisher_name , publisher_location , publisher_email FROM tbl_publisher_add WHERE publisher_name = ".$name." AND publisher_location = ".$location." AND publisher_email = ".$email." ";
}