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

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    13

    How can I pass Array as parameter PHP to JavaScript


    Hi, all

    I wrote a PHP script like this:

    <?php
    ...
    ?>
    <Script>
    function changeColor(theform)
    {
    newcolor="filename".theform.color.Select[theform.color.Select[SelectedIndex]].value;
    theform.color_array[].value=newcolor;
    }
    </Script>
    <?
    <form name="myform">
    <SELECT name=color onChange=changeColor(this.form)>
    <Option value=red>red
    <Option value=blue>blue
    </SELECT>
    <INPUT TYPE=HIDDEN NAME=color_array[]>
    ...

    I'm trying to do is I have a variable selected from Option (in PHP), then its value will pass to JavaScript, (the line "newcolor="filename".theform.color.Select[theform.color.Select[SelectedIndex]].value;" is not exactly correct, but it's not important). I want to modify the value of parameter once I change the option(red or blue) and store in the parameter named newcolor, then I want to save it to the form array in PHP called color_array[] for transfering the parameter by submit to another PHP.
    But it's not work!!! Why? help in urgent!!!!

    I can't get the value of array from PHP to JavaScript, is it possible? Is it work if I want to pass a parameter from PHP to JavaScript, then change the value of array in PHP from JavaScript?
    Thanks!
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,959
    Rep Power
    4035
    based on the info giving, I think you just need to do this:

    theform.elements['color_array[]'].value=newcolor;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    13

    Sorry, still got error


    even I use your code, but it doesn't work,

    it shows:

    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Program Files\Apache Group\Apache\htdocs\handle_data.php on line 54

    how to write the line for getting variable from another PHP.

    like this:
    print("Color: $color_array[]");

    ????
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,959
    Rep Power
    4035
    Post your code so we can see what you are trying to do.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    13
    Hi , even it works without appearing error, the color_array[] cannot really store variables like array. It only stores the last one variable, those previous variables store in color_array[] is overwritten by the latest one. How come? how can I make it like array to store multiple variable into one array?
    Urgent and ThankS!!
  10. #6
  11. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,959
    Rep Power
    4035
    well, color_array is just a text input, so you'd have to separate them some how. Something like this would work probably.

    Code:
    <Script>
    function changeColor(theform)
    {
    newcolor="filename".theform.color.Select[theform.color.Select[SelectedIndex]].value;
    theform.color_array.value+=newcolor+'|';
    }
    </Script>
    <?
    <form name="myform">
    <SELECT name=color onChange=changeColor(this.form)>
    <Option value=red>red
    <Option value=blue>blue
    </SELECT>
    <INPUT TYPE=HIDDEN NAME=color_array>
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    13

    How is it?


    If I say, I want to let the user change the color choice many times, I've to record all history of user's selection. How can I do it? Thanks! Urgent, pls!

IMN logo majestic logo threadwatch logo seochat tools logo