Reputation: 253
I have a signup form that I am developing. There is a section on this form called "Service Area". What happens is the user enters a zip code and a mile radius and then an Ajax call pulls in all of the zip codes in that area and dynamically creates checkboxes on the page. The user then selects which cities they service and submits the form.
Is there an easy way with PHP to know which checkboxes were checked? I have read a few guides and SO articles on passing along arrays with something like:
<input type="checkbox" name="serviceCity[]">
I cannot get this to work. Everytime I try, the $_REQUEST["serviceCity"]
comes back as undefined,
so I can's use a foreach to loop through. Any help is greatly appreciated.
Upvotes: 0
Views: 53
Reputation: 27242
Try this it will work :
<input type="checkbox" name="serviceCity[]" value=""/>
You can not access serviceCity[]
value directly by using $_REQUEST["serviceCity"]
.Here, $_POST['serviceCity']
is an array so you can Fetch checkbox values by doing this :
$cities = $_POST['serviceCity'];
foreach($cities as $service_cities)
{
echo $server_cities;
}
Upvotes: 0
Reputation: 41968
You can create array postbacks in 2 ways, either explicitly:
<input name="checkbox[2]">
Or implicitly:
<input name"checkbox[]">
In the second case, they will be numbered upwards as encountered in the page.
Your real problem is probably using $_REQUEST
which was deprecated somewhere in 2004, or perhaps earlier, as it is a grossly unsafe way to access 'GPC' variables, as they might overwrite eachother. Try using $_POST
instead to read postback variables.
Upvotes: 1