Viewed   53 times

Let's say I have a javascript array with a bunch of elements (anywhere from 50-200).

I want to send that to PHP (prepared statement) using ajax. Currently, I .load a php file many times inside of a loop, but I want to convert that into an array and send the array once, loading the PHP file once instead of 50-200 times.

array[i] = variable;

 Answers

1

You could use JSON.stringify(array) to encode your array in JavaScript, and then use $array=json_decode($_POST['jsondata']); in your PHP script to retrieve it.

Friday, September 2, 2022
2

Step 1

$.ajax({
    type: "POST",
    url: "parse_array.php",
    data:{ array : JSON.stringify(array) },
    dataType: "json",
    success: function(data) {
        alert(data.reply);
    }
});

Step 2

You php file looks like this:

<?php 



  $array = json_decode($_POST['array']);
    print_r($array); //for debugging purposes only

     $response = array();
     if(isset($array[$blah])) 
        $response['reply']="Success";
    else 
        $response['reply']="Failure";

   echo json_encode($response);

Step 3

The success function

success: function(data) {
        console.log(data.reply);
        alert(data.reply);
    }
Thursday, August 4, 2022
5

Try below code

   <?php
        $var=5; 
        $myArray = array();
        while($var<10){
        $myArray[]=$var;
        $var++; 
        }
       $dataarray=array("myarray"=>$myArray);
        echo json_encode($dataarray);
        ?>

Jquery

  jQuery(document).ready(function(){
    jQuery("#previous").click(function(){
    var res = new Array(); 
        jQuery.getJSON("phparray.php", function(data) {
    var i= 0;
    while(i<data.myarray.length){
                res[i]=data.myarray[i];
             i++;
    }
  jQuery("#result").html(res[0]);
            });
        });

    });
Sunday, December 11, 2022
4

To send an array of params via URL from Javascript to PHP/Codeigniter, you need to follow 2 steps:

  1. Convert your params array to JSON String and send that as a single query param.
    para = JSON.stringify(para);
    window.open('somePDFView?params='+para,'_blank');

  2. Decode the query string param (i.e JSON string) at your controller
    $params = json_decode($_GET['params']);

Now you can use all the params at your controller by accessing through $params.

Your code block becomes:

function viewReport(mode,someid){
    if(mode==0){
        var para= new Array();
        para['para1'] = 'para1'||0;
        para['para2']= 'para2' || 0;
        console.log(para);
        para = JSON.stringify(para);
        window.open('somePDFView?params='+para,'_blank'); 
    }else{
        var para=[];
        var paraelements={
          para1:'anotherpara1'||0,
          para2:'anotherpara2'||0
        };
        para[0]=paraelements;
        para = JSON.stringify(para);
        window.open('somePDFView?params='+para,'_blank'); 
    }
}

Try this out and let me know if you still faces this issue.

Friday, December 2, 2022
 
2

If you want to pass a JavaScript object/hash (ie. an associative array in PHP) then you would do:

$.post('/url/to/page', {'key1': 'value', 'key2': 'value'});

If you wanna pass an actual array (ie. an indexed array in PHP) then you can do:

$.post('/url/to/page', {'someKeyName': ['value','value']});

If you want to pass a JavaScript array then you can do:

$.post('/url/to/page', {'someKeyName': variableName});
Monday, December 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :