PHP forms and checkboxes

Discussion in 'OT Technology' started by babygodzilla, Jul 21, 2003.

  1. babygodzilla

    babygodzilla I love rice

    Joined:
    Nov 5, 2001
    Messages:
    3,108
    Likes Received:
    0
    im using a loop to recreate a form 5 times, consisting of a drop down menu and a text field. to name them, i use

    "<input name=\"code".$x."\" (and so on)

    so the names of the 5 fields are code1, code2, code3, code4, code5. so my question is is this a good way to name them? cause im kinda having trouble with the handling form. im not sure how to loop thru them. i thought i could just increment the numbers at the end of code# by appending something like code.$x or something like that. rite now i just have 5 separate if statements for each code#.

    another thing, after all this form is processed and results displayed, i want to put checkboxes right next to each search result to perform another operation. how should i name those checkboxes?

    THANKS!
     
  2. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    I'll say code1, code2, ... codeX is not very human friendly. As you see, PHP doesn't care though.

    I'll say it depends on what your PHP comfort level is and how much time you want to spend setting things up (which when done right, can save you a ton of time later on in other apps).

    Here's what I do (and this is from a flow standpoint):

    Determine page action (no action) -> show form -> user submits -> validate form data (lets assume its not ok and I need to reshow the form) -> show form (literally use same code as seen in 2nd step)

    The trick I use is templates. To show the form, I load a template (I make my templates as HTML files that sit in a sub directory). Then where ever I want a variable, I put in:

    Code:
    <!--$myvarname$-->
    
    My template library will match "myvarname" with "myvarname" in an array I pass to my template library. If I don't have a match, I have an option of clearing that tag out or leaving it there (handy for troubleshooting). The beauty is you write 1 form and use it for add, edit, and validate. This works by sending $_POST or $_GET to the template parser and it does the rest. In some cases, you have to do some pre-processing for the form to work and I've got mine setup so its not a big deal.

    This may work for you. This may be over kill for you. I'll let you decide.

    The plain and simple way (you may like this one better):

    Code:
    <input type="text" name="myvar" value="<?=$_POST['myvar']?>">
    
    In this case, the first time the user loads the form, they'll see a blank text box. If they submit and the data fails, you just pull what they entered out of $_POST (or $_GET).

    It will require a little bit of finesse on your part to make something like this work, but I'd recommend it.

    As for processing a collection of variables, remember $_GET and $_POST are arrays. You can use count($_POST) to get the number of variables submitted (remember you may have $_POST['x'] and $_POST['y'] for the mouse location upon submit).

    Plus foreach(myarray as $key => $value) will be your friend too. The foreach() will allow you to process each array element and grab its key name (in this case, the form element name) and the value (the elements value or what the user entered).
     
  3. tac

    tac (:-|)=|=

    Joined:
    Jan 3, 2001
    Messages:
    21
    Likes Received:
    0
    Location:
    vancouver
    you could use: <input name="code[]" >

    that way when you submit the form you can access the fields like:
    Code:
    if (isset($_POST['code'])) {
        $count = sizeof($_POST['code']);
        for ($i=0; $i<$count; $i++) {
            $value = $_POST['code'][$i];
            
               ......
        }
    }
    
    or similarly:
    Code:
    if (isset($_POST['code'])) {
        foreach ($_POST['code'] as $value) {
        
            ........    
        }
    }
    
     

Share This Page