SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast Chip D's Avatar
    Join Date
    Nov 2000
    Location
    Ontario, Canada
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating Dynamic Form Names

    At the moment I'm trying to create a "group bookings" form for one of my sites. Bet you can't guess which one!

    Anyway, I've managed to set it up so that the user types in the number of rooms they wish to book, and the page that is generated shows them the options that they need to select for each room they wish to book. For example, if they say they want to book 5 rooms, then 5 sets of options will appear.

    The problem that I'm having now, is that I can't work out how to get the names of the form fields to be labelled accordingly, i.e. room_1_type, room_1_smoking, room_2_type, room_2_smoking. I need the numbers to be generated dynamically, in a sequential order, such as that shown above.

    Anybody have any ideas?

    Many Thanks
    Chip D
    ----------------------------------------------------
    Chances are what I think I'm typing is nothing like what I type.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Setting up the form fields will be the easy part, grabbing the values from the dynamic number of form fields will be the tricky part. To set up the forms I assume a number will get passed to the script that will say how many rooms to book, let's call it $numrooms.

    PHP Code:
    <?
    for($i=1;$i<=$numrooms;$i++) {
    ?>
        Room <?=$i?> type: <input type="text" name="room_type_<?=$i?>"><br>
        Room <?=$i?> smoking: <input type="text" name="room_smoking_<?=$i?>"><br>
    <?
        
    }

    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast Chip D's Avatar
    Join Date
    Nov 2000
    Location
    Ontario, Canada
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help on this, it's greatly appreciated!

    I've got the form generating as I wanted now, however, I'm now realising what you meant when you said that it would be tricky to get the variable passed to the next stage. What I need to do now is figure out a way to get the data into an email, preferably with a confirmation of data entered page, although it's not essential. Every idea I've had so far, I've managed to dismiss before even typing a piece of code! If you, or anyone else has any ideas on how to achieve this, then it would be great if you could share them with me.

    Thanks again,
    Chip D
    ----------------------------------------------------
    Chances are what I think I'm typing is nothing like what I type.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you'll need to use is variable variables, which you will see I used inside the for() loop at the top. Check the php site for more information on variable variables.


    PHP Code:
    <?

    //Check for form submission
    if ($submit) {
        
    //Loop through rooms based on $numrooms
        
    for($i=1;$i<=$numrooms;$i++) {
            
    //Make tmp vars for each
            
    $tmptype "room_type_$i";
            
    $tmpsmoke "room_smoking_$i";

            
    //Using variable variables append each room type and smoking 
            //pref to the variable $data which can then be emailed
            
    $data .= "Room $i Type = ".$$tmptype."\n<br>";
            
    $data .= "Room $i Smoking = ".$$tmpsmoke."\n<br><br>";
            }

        
    //Print $data for testing
        
    print $data;
        }

    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body>
    <form action="<?=$PHP_SELF?>" method="post">
    <?
    for($i=1;$i<=$numrooms;$i++) {
    ?>
        Room <?=$i?> type: <input type="text" name="room_type_<?=$i?>"><br>
        Room <?=$i?> smoking: <input type="text" name="room_smoking_<?=$i?>"><br>
    <?
        
    }

    ?>
    <input type="hidden" name="numrooms" value="<?=$numrooms?>">
    <input type="submit" name="submit" value="Submit">
    </form>

    </body>
    </html>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •