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

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    12

    Pass parameter array from JavaScript to PHP


    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. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    57
    Rep Power
    13
    why do you need an array?
    usually you use the brackets ( [ ] ) when you need to pass many values to a php page ,
    but it seems you only want to pass 1 value, or am I wrong?
    I guess you will get a javascript error on that line:

    theform.color_array[].value=newcolor;

    becuse of the naming of that hidden field ,
    try and remove the brackets ...
    theform.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
    12

    Missing information


    Sorry, I forget to mention one thing, which is the color[] array I need to use because there're more than one values I've to store in the parameter, that's why I need to use array. Is anyone can help? Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    57
    Rep Power
    13
    ok i see,
    but that brackets syntax is used when you have multiple fields of that form:
    <INPUT TYPE=HIDDEN NAME=color_array[]>
    and then you can automatically threat those fields as an array: $color_array in the php page,

    it is not used to save many values in one particlar html field
    maybe you already know this...
    but if you want to pass many values you dont have to do it that way..
    if you post the entire code it will be easier to provide a workaround
    Last edited by danjel; November 26th, 2002 at 08:26 AM.
  8. #5
  9. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Couldn't be that urgent...

    var newcolor = theform.color[theform.color.selectedIndex].value;
    theform['color_array[]'].value = newcolor;
    }
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    12

    follow yours, but it doesn't work


    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!!
  12. #7
  13. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    ipandai...

    All HTML form values are strings - you can't pass objects (arrays, e.g.) in a query string or an http body. The Select you posted isn't a multiple, so it can only specify one selected value at a time. Why not change it to <select multiple="multiple"......> so that the values get passed directly from user selection? Some of this will have to be resolved server-side (PHP) - you're jumping between two programming environments (and languages).
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    12

    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!

IMN logo majestic logo threadwatch logo seochat tools logo