Click here to Skip to main content
14,828,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to pass Javascript array variable to PHP ,

so am not able to pass the array variable from javascript to php

So below is the code i tried

What I have tried:

  <title>CSV File to HTML Table Using AJAX jQuery


    <div class="container">
        <div class="table-wrapper">
            <div class="table-title">
                <div class="row">
                    <div class="col-sm-6">
      <h2>Manage Instances</h2>
     </div>
     <div class="col-sm-6">
        
    </div>
</div>
</div>
    <div align="center">
     Load Data
    </div>
    <br>
    <div id="employee_table">
    </div>
   </div>
  </div>

 

    $( document ).ready(function() {
        $( ".button1" ).click(function() {
            var val = [];
            $("input:checked").each(function (index,value) {
                    val[index] = this.value;
            });

            var myJSONText = JSON.stringify(val);
            $.ajax({
                data: {'kvcArray': myJSONText},
                url: 'Index.php',
                type: 'POST',
                success: function(data) {
                    alert(data);
                }
            });
        });
    });


console.log('Debug Objects: " . json_decode($_POST['kvcArray'], true) . "' );";
        var_dump(json_decode($_POST['kvcArray'], true));
        die();
    }
?>




$(document).ready(function(){
 $('#load_data').click(function(){
  $.ajax({
   url:"FILES/employee.csv",
   dataType:"text",
   success:function(data)
   {
    var employee_data = data.split(/\r?\n|\r/);
    var table_data = ' <table class="table table-striped table-hover"><thead> <tr><th><span class="custom-checkbox"><input type="checkbox" id="selectAll"><label for="selectAll"></label></span></th>';
    table_data += '<tr><th></th>';
    for(var count = 0; count<employee_data.length; count++)
    {
     var cell_data = employee_data[count].split(",");
    //  table_data += '<tr>';
     for(var cell_count=0; cell_count<cell_data.length; cell_count++)
     {
      if(count === 0)
      {
       table_data += '<th>'+cell_data[cell_count]+'</th>';
       if(cell_count == 4){
           table_data += '<th></th><th></th>';
       }
      }
      else
      {
          if(cell_count === 0 ){
            table_data +='<tr><td><span class="custom-checkbox"><input type="checkbox" class="checkbox" id="checkbox"'+count+'" name="options[]" value="'+count+'"><label for="checkbox1"></label></span></td>';
            table_data += '<td>'+cell_data[cell_count]+'</td>';
         }else{
             table_data += '<td>'+cell_data[cell_count]+'</td>';
          }
      }
     }
         table_data += '</tr>';
    }
    table_data += '</table>';
    $('#employee_table').html(table_data);
   }
  });
 });
 
});
Posted
Updated 30-Oct-20 2:36am

1 solution

A way that this can be done is with JSON.

It turns your array into a single string which is easily passed in AJAX and at the other end you can convert it back to an array.

See here: JSON[^], for a start.

JSON is very well established and works with javaScript (your client side), php (server side) and even SQL. Among others.

   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900