-1

I need to pass array data through ajax, and also should post back it in another page. but my code is not working, this is my code:

var data = [page_num: page, lstGend: <?php echo $gender;?>, lstFrom: <?php echo $fromyear;?>, lstTo: <?php echo $toyear;?>];

$.ajax({
    type: "POST",
    url: "data.php",
    data:{ activitiesArray : data },
    success: function(res) {
        $("#result").append(res);
        console.log(res);
        }
});

POST the array:

$myArray = $_REQUEST['activitiesArray'];
foreach($myArray as $a){
    echo $a['page_num'];
    echo $a['lstGend'];
    echo $a['lstFrom'];
    echo $a['lstTo'];
}

Please help me guys, thnx

Send JSON data from Javascript to PHP?

Above issue is not same as mine, i need to pass multiple data(array data), please consider about this.

Community
  • 1
  • 1
batMask
  • 734
  • 3
  • 17
  • 35

1 Answers1

1

Replace

data:{ activitiesArray : data },

with

data:{ page_num: page, 
       lstGend: <?php echo $gender;?>,
       lstFrom: <?php echo $fromyear;?>,
       lstTo: <?php echo $toyear;?> },

and in PHP code make below changes.

Replace

$myArray = $_REQUEST['activitiesArray'];
foreach($myArray as $a){
    echo $a['page_num'];
    echo $a['lstGend'];
    echo $a['lstFrom'];
    echo $a['lstTo'];
}

with

echo $_POST['page_num'];
echo $_POST['lstGend'];
echo $_POST['lstFrom'];
echo $_POST['lstTo'];
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90