SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question regarding variables

    Hi there,

    I am facing a little problem regarding a form built dynamically. We have a flexible amount a boxes where the user can select options regarding video productions. The amount of the boxes depend on the amount of actors seletced on a previous page.

    Until now I used a simple for statement to set the variables (e.g. name="variable_$i"). There is a lot of javascript included to calculate prices etc.

    Now I face the problem to collect all the data, since I do not know in advance how many variables I have on the form (variable_x). I was thinking about using arrays instead such as name="variable[]", but that doesn't seem to work with my Javascript code.

    Javascript code is e.g.
    Code:
    gvalue1[i] = p.elements['actor1_'+i].options[p.elements['actor1_'+i].selectedIndex].value;
    I know this post kind of belongs into both the PHP and/or Javascript section, but maybe there are some multi talents around

    Thanks for any advise,

    regards ASchweti

  2. #2
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd suggest you look into the foreach() function, www.php.net/foreach

    foreach ($my_element as $value) {
    do something;
    }

    ast

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi astericks,

    I suppose this would work fine with an array, but as I tried to explain... my variable look e.g. like this actor1_1, actor1_2, actor1_3 ... actor1_x and I do not know what x is in advance. Of course I know when the page is loaded...

    Do you get my problem now??

    ASchweti

  4. #4
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think (and hope ) I do.

    since you need to have your variables in this format: actor1_x

    Let's say you have checkboxes, or multiple select box or something. The amount of checkboxes checked = x.

    your checkboxes would be <input type="checkbox" name="actor_[]" />.

    notice the underscore. i hope it works....

    thus, the above, with the foreach() fn would work irrespective of the number of actors selected.

    try and see if it works...coz i could very well be wrong.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Astericks,

    I am not sticking to the format using an underscore. It could also be actor1[]. But my problem seems to be the Javascript part, when I use an array as the field name.

    This might be an easy task, if you look at my Javascript code I posted above. I might have to post this in the Javascript section as well

    What do think?

    ASchweti

  6. #6
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ack...javascript is not really sometihng i can help with...sorry.

    asT

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi asT,

    since this hass been an issue inbetween JS and PHP I posted my question in both areas. Fortunately somebody could help and I have solved it now.

    Thanks for your support,...

    ASchweti

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi everybody,

    please let me add one question to this post.

    I have a radiobox selection on my page. As I described before I have an unknown amount of boxes where the users have to make their selection which is why I used arrays in the first place.

    For options like a dropdown or a checkbox that's no problem. The array holds the first value "actor[0]" the second as "actor[1]" and so on.

    No my question. How can I establish that with radio boxes? I can't name all of them "radio[]" of course.

    Hope you get it, otherwise please ask me for details..

    Regards ASchweti


Bookmarks

Posting Permissions

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