SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Form Array

  1. #1
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Array

    Hi all,

    I am pulling my hair out on this one because it's so trivial, yet I figure it out. So basically I have a form that has a series of checkboxes

    Code:
    table id="restaurant_types_table">
    <tr>
    <td><input type="checkbox" name="restaurant_types[]" value="1"> Afternoon Tea</td>
    <td><input type="checkbox" name="restaurant_types[]" value="2"> Bakery</td>
    
    </tr>
    <td><input type="checkbox" name="restaurant_types[]" value="3"> Breakfast/ Brunch</td>
    <td><input type="checkbox" name="restaurant_types[]" value="4"> Café</td>
    </tr>
    <td><input type="checkbox" name="restaurant_types[]" value="5"> Dessert</td>
    (yes yes I am using tables...just be quiet). All my checkboxes are part of the array restaurant_types[]. Using javascript, I would like to ensure that at least ONE checkbox is checked.

    So i figure the code would be like

    Code:
    if (form.restaurant_types.length < 0)
    alert("Please check more than one!!");
    But I think that is incorrect, because the length would actually be all the checkboxes, and not just the checked ones. But the problem is I can't even seem to access the form array and I don't know what I am doing wrong.

    Can anyone help me?

    Thanks

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,115
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Hi, I imagine the HTML validator would complain about '[]' in your name attribute.
    Try removing them restaurant_types

    The names aren't an array, they are names of the form elements.

  3. #3
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are right about removing the []...but then PHP won't be able to read it as an array anymore...is there a compromise?

  4. #4
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,115
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    You're mistaken.

    If you submit the form using the POST method you can access the value that was selected with:
    $_POST['restaurant'] or $_GET['restaurant'] if you use the GET method.

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That presumes only one value is expected. If more than one is allowed then you need the [] to force an array so PHP sees it. Here is a test script that shows a working principle ...
    HTML Code:
    <?php
    $rt = $_GET['restaurant_types'];
    echo "<pre>";
    print_r ($rt);
    echo "</pre>";
    ?>
    <script type="text/javascript">
    function validate (theForm) {
    	var count = 0;
    	var num_of_elements = theForm.length;
    	for (var i=0; i<num_of_elements; i++) {
    		var theElement = theForm.elements[i];
    		var element_type = theElement.type;
    		var element_name = theElement.name;
    		var element_value = theElement.value;
    		if (element_type == "checkbox" && element_name == "restaurant_types[]") {
    			if (theElement.checked) {
    				count++;
    			}
    		}
    	}
    	if (count > 0) {
    		alert ("OK");
    		return true;
    	} else {
    		alert ("NO");
    		return false;
    	}
    }
    </script>
    <form method="GET" onsubmit="return validate(this)">
    <table id="restaurant_types_table">
    <tr>
    <td><input type="checkbox" name="restaurant_types[]" value="1"> Afternoon Tea</td>
    <td><input type="checkbox" name="restaurant_types[]" value="2"> Bakery</td>
    
    </tr>
    <td><input type="checkbox" name="restaurant_types[]" value="3"> Breakfast/ Brunch</td>
    <td><input type="checkbox" name="restaurant_types[]" value="4"> Caf&#233;</td>
    </tr>
    <td><input type="checkbox" name="restaurant_types[]" value="5"> Dessert</td>
    </tr>
    <tr><td colspan="2"><input type="submit"></td></tr>
    </table>
    </form>
    Ian Anderson
    www.siteguru.co.uk

  6. #6
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,115
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Interesting, I wasn't aware that PHP did that, thanks.
    I still say it's just bad though..

    For checkboxes just give them different names!

    Edit: You should never change the HTML to suit the server side language of choice. You gotta keep em separated.

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Re. your edit ... I agree that this is best. However if you are developing something for a bespoke site then it is OK to simplify matters to suit the environment.

    Of course, if the method="GET" then it is relatively easy to read the querystring in PHP and extract the form elements/values, in which case the back-end environment matters little.

    Regarding giving the checkboxes different names - that is also good advice. In this case I suspect the form feeds a database search function to returns restaurants that match one or more of the noted categories. Having separate form element names may or may not complicate the PHP/SQL coding.
    Ian Anderson
    www.siteguru.co.uk


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
  •