plaedien Posted April 10, 2012 Share Posted April 10, 2012 Hi all, Hi have a bit of a problem that I am completely stuck on. I have a form on a website that needs checkboxes to be processed by PHP, but i have no idea how to implement them. I have found examples, but my knowledge of php is exactly zero, so its confusing the hell out of me. The website is: http://www.geelongnannies.com.au/test/employment.html http://www.geelongnannies.com.au/test/freecontactformprocess3.php http://www.geelongnannies.com.au/test/freecontactformsettings.php if anyone can have a look and give me an example of how to change the php to process the checkboxes when submitted, I will be very grateful cheers! Quote Link to comment https://forums.phpfreaks.com/topic/260678-new-to-php-validating-checkboxes-in-php-please-help/ Share on other sites More sharing options...
trq Posted April 10, 2012 Share Posted April 10, 2012 You say you've already read some examples and didn't understand them, how could we make them easier to understand? Instead, why don't you post the code your having trouble with and a description of your actual problem? Quote Link to comment https://forums.phpfreaks.com/topic/260678-new-to-php-validating-checkboxes-in-php-please-help/#findComment-1336043 Share on other sites More sharing options...
plaedien Posted April 10, 2012 Author Share Posted April 10, 2012 fair enough. What I dont understand is how to get the php form to submit the checkboxes with the rest of the form. I understand that the checkboxes need the same 'name' to be part of an array(?) but thats were my understanding ends... Here is the html: <table cellspacing="5" cellpadding="5" border="0"> <tr> <td valign="top"> Please check any of the options below that apply to you: </td> </tr> <tr> <td valign="top"> <input type="checkbox" value="Working with children check" name="check[]" id="Working_children"/> Working with children check<br/> <input type="checkbox" value="Police check" name="check[]" id="Police_check"/> Police check<br/> <input type="checkbox" value="First aid certificate" name="check[]" id="First_aid"/> First aid certificate<br/> <input type="checkbox" value="ABN" name="check[]" id="ABN"/> ABN<br/> <input type="checkbox" value="Current drivers licence" name="check[]" id="Drivers_licence"/> Current drivers licence<br/> <input type="checkbox" value="Car" name="check[]" id="Car"/> Car<br/> </td> </tr> <tr> <td colspan="2" align="right"> <input type="submit" name="submit" value="Submit!" class="submit-button3" /> </td> </tr> </table> and the PHP Im using currently: if(isset($_POST['Email_address'])) { include 'freecontactformsettings.php'; function died($error) { echo "Sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } if(!isset($_POST['First_name']) || !isset($_POST['Surname']) || !isset($_POST['Suburb']) || !isset($_POST['Email_address']) || !isset($_POST['Experience_ages']) || !isset($_POST['Availability']) || !isset($_POST['Phone_number']) || !isset($_POST['Working_children']) || !isset($_POST['Police_check']) || !isset($_POST['First_aid']) || !isset($_POST['ABN']) || !isset($_POST['Drivers_licence']) || !isset($_POST['Car']) ) { died('Sorry, there appears to be a problem with your form submission.'); } $first_name = $_POST['First_name']; // required $surname = $_POST['Surname']; // required $suburb = $_POST['Suburb']; // required $email_from = $_POST['Email_address']; // required $experience_ages = $_POST['Experience_ages']; // required $availability = $_POST['Availability']; // required $phone_number = $_POST['Phone_number']; // not required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(preg_match($email_exp,$email_from)==0) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } if(strlen($First_name) < 2) { $error_message .= 'Your name does not appear to be valid.<br />'; } if(strlen($Date_needed) < 2) { $error_message .= 'The Date needed you entered do not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\r\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\r\n"; $email_message .= "Surname: ".clean_string($surname)."\r\n"; $email_message .= "Suburb: ".clean_string($suburb)."\r\n"; $email_message .= "Email address: ".clean_string($email_from)."\r\n"; $email_message .= "Experience with ages: ".clean_string($experience_ages)."\r\n"; $email_message .= "Availability: ".clean_string($availability)."\r\n"; $email_message .= "Phone number: ".clean_string($phone_number)."\r\n"; $email_message .= "Working with children check: ".clean_string($working_children)."\r\n"; $email_message .= "Police check: ".clean_string($police_check)."\r\n"; $email_message .= "First aid: ".clean_string($first_aid)."\r\n"; $email_message .= "ABN: ".clean_string($abn)."\r\n"; $email_message .= "Drivers licence: ".clean_string($drivers_licence)."\r\n"; $email_message .= "Car: ".clean_string($car)."\r\n"; $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); header("Location: $thankyou"); ?> <script>location.replace('<?php echo $thankyou;?>')</script> <?php } die(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/260678-new-to-php-validating-checkboxes-in-php-please-help/#findComment-1336053 Share on other sites More sharing options...
Sajesh Mohan Posted April 11, 2012 Share Posted April 11, 2012 if(document.sform.chec.checked == false) { alert("Please Accept I Agree Check Box"); document.sform.chec.focus(); return false; } Quote Link to comment https://forums.phpfreaks.com/topic/260678-new-to-php-validating-checkboxes-in-php-please-help/#findComment-1336281 Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 If the checkboxes are named "check[]", then they will create an array you can address by one of these: $_GET['check'], $_POST['check'] or $_REQUEST['check']. I would use the second one, but you can't just do that out of nowhere, it needs to be specified in the HTML, and I will get back to that soon. Here is how to print the array: print_r($_POST['check']); To for example address the first element in the array: echo $_POST['check'][0]; To for example address the second element in the array: echo $_POST['check'][1]; To for example check if enough check how many checkboxes have been checked: echo count($_POST['check']); To loop through all the checkboxes: foreach($_POST['check'] AS $check){ echo $check; } But it can also be done like this: $count = count($_POST['check']); for($i=0; $i<$count; $i++){ echo $_POST['check'][$i]; } Also many other ways with different types of loops like while for example. Okay, back to one of your main problems, one that I told you I would get back to later. I can't see you create a HTML form... All your inputs should be inside the form. The form parameters will tell the user who visit your site where to send the data and how it is expected to be sent. The form tag in HTML has one required parameter, action, but it is also recommended to use method. Let me show you... <form action="" method="post"> <input type="checkbox" name="check[]" value="Police Check" /> Police Check<br /> <input type="checkbox" name="check[]" value="Car" /> Car<br /> <input type="submit" name="submit" value="send checkdata" /> </form> Action is where the data is supposed to be sent. I set the action to be empty, because then it will send the data to the same page, but you can make it send the data to another page as well... This form can be used with the above examples, because I set the method to be post. It will send the form data in the background, without the user noticing it. You could have set it to get as well, but then you would have needed to use $_GET instead of $_POST, and the user would see the form data in their URl. On the other hand, this has the advantage of making URls with form data. I'm not sure why you would want to put it all in one array though? Usually this would have been smart if there was some kind of list, and the order of the items didn't matter. Like if you were supposed to update a whole list of elements in a database that you had checked and sent along with the id in the database. I don't see anything like that, so we would have to check every single input if it for examples equals "Car". That does sound a bit dumb to me, but you could do it like this: foreach($_POST['check'] AS $check){ if($check=='Car'){ echo 'car was checked<br />'; }elseif($check=='Police Check'){ echo 'Police Check was checked<br />'; } } Now you would check every single element in the array every time if it equals one of the things in the list. :\ You could instead have given them different names, and checked if they were set. <?php if(isset($_POST['submit'])){ echo 'form was sent...<br />'; if(isset($_POST['car'])){ echo 'car was checked<br />'; } if(isset($_POST['policecheck'])){ echo 'police check was checked<br />'; } } ?> <form action="" method="post"> <input type="checkbox" name="policecheck" value="Police Check" /> Police Check<br /> <input type="checkbox" name="car" value="Car" /> Car<br /> <input type="submit" name="submit" value="send checkdata" /> </form> Try to use the last example I gave you alone in a new file... play around with it, only way to really learn programming is doing it yourself. I hope this helps you! Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/260678-new-to-php-validating-checkboxes-in-php-please-help/#findComment-1336289 Share on other sites More sharing options...
silkfire Posted April 11, 2012 Share Posted April 11, 2012 Because your checkboxes are not related, you can give each checkbox a unique name. Remember, if a checkbox is UNCHECKED (not clicked) it will not post, so by using this knowledge you can check if a checkbox is checked or not. In your processing code, use statements like these: if (isset($_POST['police_check'])) // do something if checked Also I could see you named your form elements a bit unconventional. Use short lower case names, no spaces. Plus your checkboxes were never posted because they're outside the <form> tag. All form elements have to reside WITHIN the preferably same <form>!. The submit button does not need a name, by the way: First_name => first_name Surname => last_name Suburb => suburb Email_address => email Experience with child ages... => experience Availability => availability Phone_number => phone submit => REMOVE this name tag Quote Link to comment https://forums.phpfreaks.com/topic/260678-new-to-php-validating-checkboxes-in-php-please-help/#findComment-1336333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.