Jump to content

HTML Form Elements - The Checkbox


iwato
 Share

Recommended Posts

QUESTION: How do you parse the $_GET string of a Form element containing <input> tags with the same name attribute, but different value attributes?BACKGROUND: The following code creates a $_GET string with two name attributes called "country" and "other". Although I am able to write code that captures the last value of the "country" name attribute and the only value of the "other" name attribute, I am unable to capture multiple values of the "country" name attribute when more than one checkbox is checked.

<form action="checkBox.php" method="get">	 <input type="checkbox" name="country" value="USA" />United States of America<br />	 <input type="checkbox" name="country" value="Korea" />Republic of Korea<br />	 <input type="checkbox" name="country" value="Japan"/>Japan<br />	 <input type="other" name="other" />Other<br />	 <input type="submit" value="Submit" /></form>

For example, if all of the boxes were checked and Morocco entered as the value of the other attribute, does the $_GET string take on the following format?

country[0] = "USA"country[1] = "Korea"country[2] = "Japan"other = "Morocco"

I tried using the foreach loop in a variety of ways, but could not get it to parse properly.Roddy

Link to comment
Share on other sites

You have to put brackets after the input names, e.g.

<input type="checkbox" name="country[]" value="USA" />

Link to comment
Share on other sites

You have to put brackets after the input names, e.g.
<input type="checkbox" name="country[]" value="USA" />

Yes, this is all it took! Great help, Synook!I was beginning to wonder, if I would have to write some sort of regular expression to do the job. Then too, I kept thinking that the developers of PHP would have made things simpler.Many thanks!Roddy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...