Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Sending and receiving arrays

    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

    Code:
    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 });
    
    });
    
    });
    PHP Code:

    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);


    Last edited by alex57; 01-11-2010 at 05:53 PM.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try
    PHP Code:
    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.

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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:

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

  • Users who have thanked angst for this post:

    alex57 (01-11-2010)

  • #5
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    you could do a 2D array in the same method, just use another delimiter to split the arrays.

  • #7
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    $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?

  • #8
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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!!

  • #9
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    lol, was just about to reply back that 0 had no value. glad u caught it!

  • #10
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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

    PHP Code:

    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

    PHP Code:
    //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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •