将ajax中的变量从一页传递到另一页

时间:2019-10-31 06:00:45

标签: php mysql ajax

我有一个表格,其中

<form method="post" id="insert_form">
    <div class="table-repsonsive">
        <span id="error"></span>
        <table  class="css-serial" class="table table-bordered" id="item_table">
            <thead>
                <h5>Package Name:-<input type="text" name="packname" /><input type="hidden" id="packname" name="packname" value="<?php $packname;?>" /></h5>
                <tr>
                    <th>sl.no</th>
                    <th>Service Type</th>
                    <th>Service Name</th>
                    <th>Sessions</th>
                    <th><button type="button" name="add" class="btn btn-success btn-xs add"><span class="glyphicon glyphicon-plus"></span>Add Services</button></th>
                </tr>

            </thead>
            <tbody></tbody>
        </table>
        <div align="center">
            <input type="submit" name="submit" class="btn btn-info" value="Save" />
        </div>
    </div>
</form>

使用脚本

$(document).ready(function(){

    var count = 0;

    $(document).on('click', '.add', function(){
        count++;
        var html = '';
        html += '<tr>';
        html += '<td>&nbsp;</td>';
        html += '<input type="hidden" name="item_name[]" class="form-control item_name" />';
        html += '<td><select name="item_category[]" class="form-control item_category" data-sub_category_id="'+count+'"><option value="">Select Service Type</option><?php echo fill_select_box($connect, "0"); ?></select></td>';
        html += '<td><select name="item_sub_category[]" class="form-control item_sub_category" id="item_sub_category'+count+'"><option value="">Select Service Name</option></select></td>';
        html +='<td><input type="text" name="nosessions[]" class="form-control nosessions" /></td>';
        html += '<td><button type="button" name="remove" class="btn btn-danger btn-xs remove"><span class="glyphicon glyphicon-minus"></span></button></td>';
        $('tbody').append(html);
    });

    $(document).on('click', '.remove', function(){
        $(this).closest('tr').remove();
    });

    $(document).on('change', '.item_category', function(){
        var category_id = $(this).val();
        var sub_category_id = $(this).data('sub_category_id');
        $.ajax({
            url:"fill_sub_category.php",
            method:"POST",
            data:{category_id:category_id},
            success:function(data)
            {
                var html = '<option value="">Select Sub Category</option>';
                html += data;
                $('#item_sub_category'+sub_category_id).html(html);
            }
        })
    });

    $('#insert_form').on('submit', function(event) {
        event.preventDefault();
        var error = '';


        $('.item_category').each(function() {
            var count = 1;

            if($(this).val() == '')
            {
                error += '<p>Select Item Category at '+count+' row</p>';
                return false;
            }

            count = count + 1;

        });

        $('.item_sub_category').each(function(){

            var count = 1;

            if($(this).val() == '') {
                error += '<p>Select Item Sub category '+count+' Row</p> ';
                return false;
            }

            count = count + 1;

        });
        $('.nosessions').each(function(){

            var count = 1;

            if($(this).val() == '') {
                error += '<p>Select no of sessions '+count+' Row</p> ';
                return false;
            }

            count = count + 1;

        });

        var form_data = $(this).serialize();

        if(error == '') {
            $.ajax({
                url:"insert.php",
                method:"POST",
                data:form_data,
                success:function(data) {
                    if(data == 'ok') {
                        $('#item_table').find('tr:gt(0)').remove();
                        $('#error').html('<div class="alert alert-success">Item Details Saved</div>');
                    }
                }
            });
        } else {
            $('#error').html('<div class="alert alert-danger">'+error+'</div>');
        }
    });
});

我需要将包名添加到插入器表insert.php中,请有人帮忙,我不太了解ajax。如何发送packname是所有其他服务的整体名称,因此我需要为每个条目传递该数组中的packname。对于所有完成的条目,packname是一个,应该在插入的每一行中输入。

1 个答案:

答案 0 :(得分:0)

  

FormData类提供了一种轻松构造代表表单字段及其值的键/值对的方法。

使用它后,您的脚本将如下所示:

<script>
    $(document).ready(function(){

      var count = 0;

      $(document).on('click', '.add', function(){
        count++;
        var html = '';
        html += '<tr>';
        html += '<td>&nbsp;</td>';
        html += '<input type="hidden" name="item_name[]" class="form-control item_name" />';
        html += '<td><select name="item_category[]" class="form-control item_category" data-sub_category_id="'+count+'"><option value="">Select Service Type</option><?php echo fill_select_box($connect, "0"); ?></select></td>';
        html += '<td><select name="item_sub_category[]" class="form-control item_sub_category" id="item_sub_category'+count+'"><option value="">Select Service Name</option></select></td>';
        html +='<td><input type="text" name="nosessions[]" class="form-control nosessions" /></td>';
        html += '<td><button type="button" name="remove" class="btn btn-danger btn-xs remove"><span class="glyphicon glyphicon-minus"></span></button></td>';
        $('tbody').append(html);
      });

      $(document).on('click', '.remove', function(){
        $(this).closest('tr').remove();
      });

      $(document).on('change', '.item_category', function(){
        var category_id = $(this).val();
        var sub_category_id = $(this).data('sub_category_id');
        $.ajax({
          url:"fill_sub_category.php",
          method:"POST",
          data:{category_id:category_id},
          success:function(data)
          {
            var html = '<option value="">Select Sub Category</option>';
            html += data;
            $('#item_sub_category'+sub_category_id).html(html);
          }
        })
      });

      $('#insert_form').on('submit', function(event){
        event.preventDefault();
        var error = '';


        $('.item_category').each(function(){
          var count = 1;

          if($(this).val() == '')
          {
            error += '<p>Select Item Category at '+count+' row</p>';
            return false;
          }

          count = count + 1;

        });

        $('.item_sub_category').each(function(){

          var count = 1;

          if($(this).val() == '')
          {
            error += '<p>Select Item Sub category '+count+' Row</p> ';
            return false;
          }

          count = count + 1;

        });
        $('.nosessions').each(function(){

          var count = 1;

          if($(this).val() == '')
          {
            error += '<p>Select no of sessions '+count+' Row</p> ';
            return false;
          }

          count = count + 1;

        });

        var form_data = new FormData($(this)[0]);

        if(error == '')
        {
          $.ajax({
            url:"insert.php",
            method:"POST",
            data:form_data,
            success:function(data)
            {
              if(data == 'ok')
              {
                $('#item_table').find('tr:gt(0)').remove();
                $('#error').html('<div class="alert alert-success">Item Details Saved</div>');
              }
            }
          });
        }
        else
        {
          $('#error').html('<div class="alert alert-danger">'+error+'</div>');
        }

      });

    });
</script>

此外,您可能需要更改:

<h5>Package Name:-<input type="text" name="packname" /><input type="hidden" id="packname" name="packname" value="<?php $packname;?>" /></h5>

进入:

<h5>Package Name:-<input type="text" name="packname" value="<?php $packname;?>" disabled="disabled" /></h5>
<input type="hidden" id="packname" name="packname" value="<?php $packname;?>" />