pass id array to a php file of selected rows over all pages

pass id array to a php file of selected rows over all pages

redsunsetredsunset Posts: 44Questions: 15Answers: 0
edited November 2018 in Free community support

I am using datatables.net with this checkbox plugin: https://www.gyrocode.com/projects/jquery-datatables-checkboxes/ and this file structure https://www.sitepoint.com/creating-a-scrud-system-using-jquery-json-and-datatables/
So what I need now are some tiny code snippets in a data.php file and in the webapp.js via jquery that perform some specific mysql actions FOR ALL SELECTRED ROWS OVER ALL PAGES after pressing different submit buttons.
When you press the main drop down menu („Mass Select Operations“) on my live example page http://mydigital.school/mtgtreasure/ you will find all the specific submit buttons on the right, with the specific actions on the left.
So the first one should update the value of the mysql column „forsale“ in the mysql table to the value 1 after pressing it for ALL rows that have a selected checkbox threw all pages.

my datatable structure:

        var table_cards = $('#table_cards').dataTable({
             "ajax": "data.php?job=get_cards",
            "columns": [
              { "data": "checkboxes" },
              { "data": "forsale" },
              { "data": "quantity" },
              { "data": "cardname",   "sClass": "cardname" },
              { "data": "edition" },
              { "data": "editionicon"},
              { "data": "price", "sClass": "integer"},
              { "data": "pricechange", "sClass": "integer"},
              { "data": "sellingprice", "sClass": "integer"},
              { "data": "cond"},
              { "data": "cardlanguage"},
              { "data": "foil" },
              { "data": "note" },
              { "data": "functions",   "sClass": "functions" }
            ],
            
        "aoColumnDefs": [
          
          {
                'targets': 0,
                'orderable': false,
                'render': function(data, type, row, meta){
                   if(type === 'display'){
                      data = '<div class="checkbox"><input type="checkbox" class="dt-checkboxes"><label></label></div>';
                   }
    
                   return data;
                },
            'checkboxes': {
                   'selectRow': true,
                   'selectAllRender': '<div class="checkbox"><input type="checkbox" class="dt-checkboxes"><label></label></div>'
                }
     
          },
          
          { "bSortable": false, "aTargets": [-1] }
        ],
        "select":  "multi",
        'order': [[3, 'asc']], // sortiert nach der vierten Spalte - Kartennamen.
        
        "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
        "oLanguage": {
          "sSearch": "<label id='has-tt-label' for='has-tt' title='Some <b>great</b> input!'>Search: </label>", //search
          
          "oPaginate": {
            "sFirst":       " ",
            "sPrevious":    " ",
            "sNext":        " ",
            "sLast":        " ",
          },
          "sLengthMenu":    "Records per page: _MENU_",
          "sInfo":          "Total of _TOTAL_ records (showing _START_ to _END_)",
          "sInfoFiltered":  "(filtered from _MAX_ total records)"
        }
    
       
      });

I tried to do it with the following code in the webapp.js to test it with a single mysql change (the button id is mass_putonsale2):

    $(document).on('submit','#mass_putonsale2', function(e){
       e.preventDefault();
         show_loading_message();
          var form = this;
          
          var rows_selected = table_cards.column(0).checkboxes.selected();
    
          // Iterate over all selected checkboxes
          $.each(rows_selected, function(index, rowId){
             // Create a hidden element 
             $(form).append(
                 $('<input>')
                    .attr('type', 'hidden')
                    .attr('name', 'id[]')
                    .val(rowId)
                   );
                }
          );
       
        var form_data_putonsale = $(form).serialize();
        var request = $.ajax({
          url:          'data.php?job=mass_putonsale',
          cache:        false,
          data:         form_data_putonsale,
          dataType:     'json',
          contentType:  'application/json; charset=utf-8',
          type:         'get'
        });
        hide_loading_message();

and with the following code in data.php:
    elseif ($job == 'mass_putonsale'){
        
         $query = "UPDATE cards SET forsale = 1 WHERE id = '";  mysqli_real_escape_string($conn,$_GET['id'][0]) ."'";
         $query  = mysqli_query($conn, $query);
      }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin
    Answer ✓

    So what I need now are some tiny code snippets in a data.php file and in the webapp.js via jquery that perform some specific mysql actions FOR ALL SELECTRED ROWS OVER ALL PAGES after pressing different submit buttons.

    I'm afraid we can't write the code for you to do this - that's not the kind of support we offer (that's contract work and you'd need a freelancer :) ). I did glace over your code though - I'd suggest you echo out $query so you can see what you are making. It isn't valid SQL.

    What you are looking for is something that Editor does out of the box and we can support that.

    Allan

This discussion has been closed.