3

i need to send array parameter or value using XMLHttpRequest

Something like this

$(document).on('click','.save_edit_class',function(){

var crit_name_text = $('#crit_name_text').val();
var crit_desc = $('#crit_desc_text').val();
var sub_crit_arr = [];
$('.sub_crit_text').each(function(k , v){
    sub_crit_arr[k] = $(v).val();

});

var http = new XMLHttpRequest();
var url = "xml_remove_criteria.php";
var params =  "crit_name_text="+crit_name_text+"&crit_desc="+crit_desc+"&sub_crit_arr[]="+sub_crit_arr+"";

http.open("GET", url+"?"+params, true);
http.onreadystatechange = function(){//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200){
        alert(http.responseText);
    }
}
http.send(null);

});

How can i send the sub_crit_arr(the array) variable to my xml page as array? I'm trying to send using that, but i got an error invalid argument supplied foreach.

This is my xml page..

<?php
session_start();
include("../global.php");
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

echo '<response>';
    if(isset($_GET['crit_name_text'])){
       $criteria_name = $_GET['crit_name_text'];
       echo $criteria_name;//testing

        if(isset($_GET['crit_desc'])){
            $crit_desc = $_GET['crit_desc'];
            echo $crit_desc;//testing
        }


    if(isset($_GET['sub_crit_arr'])){
        $sub_crit_arr = $_GET['sub_crit_arr'];
        $sub_crit_arrs = array();
        $count = 0;
        foreach ($sub_crit_arr as $value) {
            $sub_crit_arr[$count] = $value;
            echo $sub_crit_arr[$count]." - value<br/>";//testing
            $count++;
        }
    }

   }
   echo '</response>';

?>

ßiansor Å. Ålmerol
  • 2,849
  • 3
  • 22
  • 24
  • You JSON.stringify the array and post it.. then you can json_decode() on the server side – Brunis Aug 07 '16 at 15:26
  • Possible duplicate of [jQuery posting JSON](http://stackoverflow.com/questions/5570747/jquery-posting-json) – Ozan Aug 07 '16 at 15:46

0 Answers0