Check Boxes PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pete105
Forum Newbie
Posts: 1
Joined: Thu May 01, 2003 12:00 pm

Check Boxes PHP

Post by pete105 »

Well i ahve 12 check boxes

I want it so when user clicks one it goes to the database
but when user clicks no check boxes i want an error to appear


i started this code can anyone please provide me with an example
i ahve been trying to fiqure this out forever

Code:
$checkboxes = array("C49", "C49");


foreach ($checkboxes as $box) {
if (isset($_POST[$box])) {
$found = false;
break;
}

echo 'You have not entered all the required details.<br />'
.'Please go back and try again.';
exit;
}
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

try javascript

Post by phpScott »

It would probalby be easier to check if they have selected a box using javascript.

If you want to go to the database everytime they check a box you will have to put a onClick eventhandler on each check box to submit the form.

phpScott
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

for validation, u should just use a simple javascript for onsubmit that shows a promt box when no box was checked. However, if u want to use php, than just use this:

Code: Select all

if( ($_POST&#1111;'checkbox1_name']=="") && ($_POST&#1111;'checkbox2_name']="") ) //so on
&#123;echo 'You have not entered all the required details.<br />' 
.'Please go back and try again.'; &#125;
This method can become messy once reaching above 3 boxes, but you can always try looping or somthing else. Hope this helps.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Of course a JavaScript only solution is useless if a user has JS turned off (or it's not available to them), you should still use a server-side solution as backup.

Mac
Post Reply