使用输入的数据属性为php post生成多维数组

时间:2018-08-24 04:48:51

标签: javascript php arrays

我已经设置了一个代码来生成动态表来捕获数据,该代码将用于向学生发送有关时间表的消息。以下操作将生成所需的用户输入表单,以便用户可以根据需要插入数据。

$(document).ready(function() {
  var index = 1;
  var tableindex = 0;
  var daydd = '<select><option value="Saturday">Saturday</option><option value="Sunday">Sunday</option></select>';
  $("#addRow").click(function() {
    if (tableindex == 0) {
      tableindex++;
      $("#myTable").append("<tr data-day='" + tableindex + "'><td colspan=3>"+daydd+" " + tableindex + "</td><td><button class='dayclose' data-day='" + tableindex + "'>Close</button></td></tr>");
    }
    $("#myTable").append("<tr data-day='" + tableindex + "'><td>Day " + tableindex + "</td><td>row " + index + "</td><td><input type='text' data-clday='" + tableindex + "' data-subj='" + index + "' ></td><td><button class='subjclose' id='" + index + "'>Close</button></td></tr>");
    index++;
  });
  $("#addtable").click(function() {
    tableindex++;
    $("#myTable").append("<tr data-day='" + tableindex + "'><td colspan=3>"+daydd+" " + tableindex + "</td><td><button class='dayclose' data-day='" + tableindex + "'>Close</button></td></tr><tr data-day='" + tableindex + "'><td>Day " + tableindex + "</td><td>row " + index + "</td><td><input type='text' data-clday='" + tableindex + "' data-subj='" + index + "' ></td><td><button class='subjclose' id='" + tableindex + "'>Close</button></td></tr>");

    index++;
  });

  $('table').on('click', '.subjclose', function(e) {
    e.preventDefault();
    $(this).parents('tr').remove();
  });
  $('table').on('click', '.dayclose', function(e) {
    e.preventDefault();
    var closeday = $(this).attr("data-day");
    $('tr[data-day="' + closeday + '"]').remove();
    $(this).parents('tr').remove();
  });
  //$('.row-container[data="product_id"]').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<table id="myTable">
</table>
<input type="button" id="addRow" value="add subject" />
<input type="button" id="addtable" value="add day" />
<input type="button" id="submit" value="Send Data" />

现在,我正在尝试捕获要发送到php脚本的数据,以便它可以将消息发送给选定的一组学生。我的信息如下: (考虑在这种情况下,第3、4、6、9行已删除)

Saturday Subjects
Row 1 data
Row 2 data

Sunday Subjects
Row 5 data
Row 7 data

Monday Subjects
Row 8 data
Row 10 data

我试图在我的php脚本中为此运行一个for每个循环,但是我不知道如何使用数据属性来实现这一点。

如何使用输入的数据属性生成多维数组以发送给PHP脚本,以便可以用于生成foreach循环?

1 个答案:

答案 0 :(得分:1)

您可以像这样使用JQuery函数

$.fn.onAddDefaultValue = function (options) {

    // This is the easiest way to have default options.
    var settings = $.extend({
            // These are the defaults.
            data: '',
            profile_main_div_id: '',
            server_div_id: '',
            addi_id : '',
            confirm_btn_id: ''
        }, options);

        $(settings.data).each(function(idx,obj){
            var item_id = obj.item_id;
            var item_title = obj.item_title;

            // server pratik - 10112017 location widget interaction
            var isLocationExist = false;

            $("#"+settings.server_div_id).children().each(function () {
                var child_div = $(this);
                var localtion_value = child_div.find("span").text();

                if (localtion_value == item_title) {
                    //child_div.find("span").css("border", "1px solid #f16262");
                    isLocationExist = true;
                    //return false;
                }else {
                    //child_div.find("span").css("border", "0px solid #f16262");
                }
            });

            if (!isLocationExist) {

                var input_value = item_title;
                var newTextBoxDiv = $(document.createElement('div'))
            .attr({"class": 'customcheckbox',"style":'margin: 0px 0px 10px;'});

                var chk_id = item_id;

                newTextBoxDiv.after().html('<input type="checkbox" checked name="remember" id="'+chk_id+'" value="user_profile_remember_me-no">'+
                '<input type="hidden" class="server_item_id" value="'+item_id+'" />'+
                '<label style="float: right;position: relative;margin-right: 10px;border: 1px solid #000000;border-radius: 0px" for="'+chk_id+'">'+
                '<div class="input-label"></div>'+ 
                '</label>'+
                '<span style="padding-left: 5px;" class="my-label1">'+input_value+'</span>');
                newTextBoxDiv.appendTo("#"+settings.server_div_id);

                //$("#"+settings.addi_id).slideUp();
                //$(this).closest('#'+settings.profile_main_div_id).removeClass("additional-field-open");

                // add new item                 
            } else {
                    $('#'+settings.server_div_id).children().each(function () {
                        var child_div_appearance = $(this);
                        var title_v = $(this).find("span").text();
                        var status_v = $(this).find("input[type='checkbox']").prop("checked");

                        if((item_title == title_v) && (!status_v)){

                            $(this).find("input[type='checkbox']").click();//attr("checked",false)
                            // break loop
                            // return false;
                        }                                                   
                    });
            }
            $("#"+settings.confirm_btn_id).click();
            //$("#"+settings.server_div_id).addClientNote({});
        }); 
};

并删除这样添加的元素

$.fn.removeDiagnosisValue = function (options) {

    // This is the easiest way to have default options.
    var settings = $.extend({
            // These are the defaults.
            title: ''
        }, options);

    $(this).children().each(function () {
        var child_div = $(this);
        var div_buttons = child_div.find("div");

        var i = 0;
        $(div_buttons).children().each(function () {
            // delete icon
            var child_span = $(this);
            if (child_span.attr('name') == "delete") {
                var img_delete = child_span.find("img");
                img_delete.click(function () {
                    child_div.remove();
                });

                // break loop
                return false;
            }

        });

    });

};