anam
anam

Reputation: 3913

ajax to pass array value , how to fetch array on called page

I have array containg image path ,

GenerateReport[0] ="../images/Desert1.jpg"
GenerateReport[1] ="../images/Desert2.jpg"
GenerateReport[2] ="../images/Desert3.jpg"
GenerateReport[3] ="../images/Desert4.jpg"
GenerateReport[4] ="../images/Desert5.jpg"

I am trying to pass this array with following code,

$.ajax({
type: "POST",
url: "generatePdf.php",
data: { 
genRep: "sample value"
},
success: function(data) {
                 alert(data);
             console.log('getting '+data);
             }

    });

sample value is passed successfully , but how can i pass array to ajax and use it on another page?? i tried passing array and using with below code but it is not working

$data1 = $_REQUEST['genRep'];

echo "tested".$data1[0];

Upvotes: 0

Views: 8090

Answers (7)

jayadevan
jayadevan

Reputation: 11

$arr = array();
$arr[0] = "Mark Reed";
$arr[1] = "34";
$arr[2] = "Australia";
echo json_encode ($arr);

use this on the php page and use the following to get the output

success:function(msg){
id_numbers = JSON.parse(msg);
alert(id_numbers)
}

Upvotes: 1

anam
anam

Reputation: 3913

if(isset($_REQUEST['genRep']))
{
$data = $_REQUEST['genRep'];
print_r($data1);
echo "tested".$data1[0];

createPdf($data);
    }

My code was correct , there was no value on index 0 , thats why it was not printing anything .My Mistake !!!

Upvotes: 0

Manwal
Manwal

Reputation: 23816

try this

GenerateReport[0] ="../images/Desert1.jpg"
GenerateReport[1] ="../images/Desert2.jpg"
GenerateReport[2] ="../images/Desert3.jpg"
GenerateReport[3] ="../images/Desert4.jpg"
GenerateReport[4] ="../images/Desert5.jpg"

corrected code:

var imagedata = '';
$.each(GenerateReport,function(index,value))
{
  if(imagedata=='')
  {
    imagedata = imagedata +'image'+index+'='+value;
  }
  else
  {
    imagedata = imagedata +'&image'+index+'='+value;
  }
});
$.ajax({
type: "POST",
url: "generatePdf.php",
data: imagedata               //pass string imagedata
success: function(data) {
                 alert(data);
             console.log('getting '+data);
             }

    });

imagedata should have sting like this:

image1=../images/Desert1.jpg&image2=../images/Desert2.jpg and so on

Upvotes: 0

Rahul Gupta
Rahul Gupta

Reputation: 10131

I have tested it, this will definitely work !

JS:

var GenerateReport = [];
GenerateReport[0] ="../images/Desert1.jpg";
GenerateReport[1] ="../images/Desert2.jpg";
GenerateReport[2] ="../images/Desert3.jpg";
GenerateReport[3] ="../images/Desert4.jpg";
GenerateReport[4] ="../images/Desert5.jpg";

$.ajax({
    type: 'POST',
    url: 'generatePdf.php',
    data: 'image_array='+GenerateReport,
    success: function(msg) {
        alert(msg);
    }
});

generatePdf.php :

<?php
$image_string = $_POST['image_array'];
$image_array = explode(",", $image_string);
print_r($image_array);
?>

Then you can loop over the $image_array to get the values.

Upvotes: 0

404 Not Found
404 Not Found

Reputation: 1223

try to use json object. in object you can store your images path

           var data=[]; // array
           var data[0] = 'something';
           var data[1] = 'something1';
           var data = { 'name': name, 'email' : email, 'contact' : contact, 'type' : type, 'msg' : msg }; // object 
           $.ajax({
               url : 'contact.php',
               type : 'POST',
               data : {contact:JSON.stringify(data)}, // for json object
               data : {contact: data}, // for array
               success : function (msg)
               {
                   alert(msg);
               }
           })

contact.php

 $info = $_POST['contact'];
 $info = json_decode($info,true); // for json object
 echo $info.name; // for json object

 echo $info[0]; // will print something...

Upvotes: 1

Sridhar R
Sridhar R

Reputation: 20408

Try:

$.ajax({
    url: "generatePdf.php",
    type: 'POST',
    data: form_data,
    dataType:"json",
    success: function(data) {
        alert(data[0]);
   }

On the PHP side, you'll be wanting to print:

print json_encode($photos);

Made Some Change

print json_encode(array("photolist"=>$photos));

Then on the server, you'd access these with:

data.photolist[0]; //First photo

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try like

genRep = []; 
$.ajax({
    type: "POST",
    url: "generatePdf.php",
    data: { 
        genRep: GenerateReport
    },
    success: function(data) {
        alert(data);
        console.log('getting '+data);
    }

});

It will send your array as genRep

Upvotes: 1

Related Questions