#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    68
    Rep Power
    13

    PHP reading Javascript array problem


    Hi,

    I am populating an array in javascript and then POSTing this array to my PHP script. The problem is my PHP script doesnt seem to be able to find it!

    Im treating it as a regular PHP array in my PHP script, am I not taking something into account here?


    Hope you can help!

    Alba
  2. #2
  3. No Profile Picture
    <(>_~)>
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    315
    Rep Power
    13
    Might this problem be related to register_globals = off?

    Try accessing your array with $HTTP_POST_VARS["array"].
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    68
    Rep Power
    13
    Ive changed it from POST to GET.

    The url of the recieving PHP script now shows this:

    mypage.php?name=fred&array=hey&array=you


    where "array" is my array. The number of elements im entering into it are determining the number of times "array=" is shown.

    This means it is being passed, how can i get at it, and is it actually an array??

    Thanks btw CodeE
  6. #4
  7. No Profile Picture
    <(>_~)>
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    315
    Rep Power
    13
    mypage.php?name=fred&array=hey&array=you

    That isn't going to work.

    You are sending $array, which is set to "hey", and then you are sending $array which is set to "you". In the receiving script, $array will probably equal "you", I presume. It won't even be an array - it will just be a string with "you" in it.

    If I would be you, I would stick to using POST. Send the array as one variable. Do not try to send all the values in the array sepparately. That kinda destroys the point of an array, no?
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    68
    Rep Power
    13
    I think ive found the problem, How do you pass an array to a PHP script? for example using hidden or another POST method?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Germany - Aachen
    Posts
    12
    Rep Power
    0
    why don't you make your javascript code name the vars like they should:

    array[1] array[2] or anything else. shouldnt be that hard, because normaly you have the counting var so it is easy to index your vars.

    if not possible try to change the name from array to array[] this will probably work better.
  12. #7
  13. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    68
    Rep Power
    13
    ive done that I just need to know how to post an array to my PHP script. nothing seems to be working!
  14. #8
  15. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5053
    You should be able to access the POST array using the $_POST variables ($_POST['name'] == 'fred', and so on). If you're using an older version of PHP, you may need to use $HTTP_POST_VARS.
    Please check PHP.net's description of reserved variables for more detail on what you're looking for.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    13
    Rep Power
    0
    i didnt think you could do such a thing in php. i thought you could only pass variable from input tags, and only pass an array from an select tag? right now, i have a similar problem but *solved* it by putting my javascript array contents into a hidden field, then sending that. if you can pass javascript variables, could someone post an example?

    mrGarretson
  18. #10

  19. Join Date
    Aug 2001
    Location
    NYC
    Posts
    57
    Rep Power
    13
    it doesn't matter if the variable is created using javascript or declared directly in HTML code, every variable that is declared inside a FORM tag and then POSTed to the following page will be available in the $_POST array.
  20. #11
  21. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Jun 2002
    Location
    Washington, DC
    Posts
    2,692
    Rep Power
    22
    Or he could push the array...

    PHP Code:

    $myarray 
    = array();

    if (isset(
    $var_name_1)) {

       
    array_push$myarray$var_name_1 );

    }

    if (isset(
    $var_name_2)) {

       
    array_push$myarray$var_name_2 );

    }

    // and so on for your posting variables // 

IMN logo majestic logo threadwatch logo seochat tools logo