...

View Full Version : Sending and receiving arrays



alex57
01-11-2010, 05:49 PM
Hello,

this is the code I have. The javascript works fine because I have sent values across and received them ok but I want to send an array over to the php file for processing as in the example below. I think the problem is in receiving the array in the PHP file. The PHP writes "slot2" to the file. Any ideas? thanks



var myArray = new Array(2);

myArray[1] = 'slot1';
myArray[2] = 'slot2';

$(document).ready(function(){
$("#googlelink").click(function() {

var email = document.emailForm.email.value;

$.post("sendMail.php", { linkid: myArray });

});

});





if (isset($_POST['linkid'])) {

$stringData[1] = $_POST['linkid'];

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $stringData[1]);
fclose($fh);

}

angst
01-11-2010, 06:17 PM
try


print_r($_POST['linkid']);


to see exactly what it's getting. I'm not completely sure that javascript and php arrays are structured the same way.

alex57
01-11-2010, 06:25 PM
unfortunately the only way i can print what is coming out is too the file because the javascript does not actually load the php page; it goes nowehere but runs the php script.

I think it is that the arrays are structured differently but there must be a way to pass the array across if it can be done with normal variables.

angst
01-11-2010, 06:28 PM
sure there is, this is a ajax function, simply make a debug function. what I do is just alert or innerHTML the response so you can see what is going on behind the scenes.

but what you could try is converting your array to a string, then sending it over, then convert it back:


function ExplodeArray($Array){
if(is_array($Array)){
$ArrayToString = "";
foreach($Array AS $Value){
$ArrayToString .= $Value . "|";
}
return substr($ArrayToString,0,-1);
}
}

alex57
01-11-2010, 07:52 PM
I really like the idea of turning the array to a string then passing it. I can do this with a one dimensional array but I actually need to pass a 2D array. I will try this now.

angst
01-11-2010, 08:07 PM
you could do a 2D array in the same method, just use another delimiter to split the arrays.

alex57
01-11-2010, 08:56 PM
Hey,

I got errors when using your code - i am sure its just me not using it correctly. Anyway, I made a string out of my 2D array, passed the string across and received it into a PHP variable. The test string is simply "@a@b@c@d" and I was trying to explode this using:



$A = "@a@b@c@d";
$output = Explode("@", $A);

echo $output[0];
echo $output[1];


I only get the letter "a" returned to the screen. Surely I should get a "b" too?

alex57
01-11-2010, 08:57 PM
ah, its cos the string starts with a delimiter. Thanks for your help. Hopefully I should be able to complete the full task now.

Thanks!!

angst
01-11-2010, 09:02 PM
lol, was just about to reply back that 0 had no value. glad u caught it! ;)

alex57
01-11-2010, 11:35 PM
Still following this thread??? Im baffled again. I think it must be to do with PHP not treating the POST variable as a string in someway. If I run the first script below using the POST variable the explode does not work correctly. If I run the script solo and hardcode the string to explode it works correclty. I cannot work out why. I have given the result of each script directly below. any ideas? thanks




if (isset($_POST['linkid'])) {

$destinations = $_POST['linkid'];

$destinations = explode(":", (string)$destinations);

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $destinations[0]);
fclose($fh);

}


Auckland, New Zealand,22.00 GBP,0.00 GBP:Sri Racha, Thailand,8.40 GBP,0.00 GBP



//if (isset($_POST['linkid'])) {

$destinations = "Auckland, New Zealand,22.00 GBP,0.00 GBP:Sri Racha, Thailand,8.40 GBP,0.00 GBP";

$destinations = explode(":", (string)$destinations);

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

fwrite($fh, $destinations[0]);

fclose($fh);

//}


Auckland, New Zealand,22.00 GBP,0.00 GBP



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum