deziner76hd Posted October 19, 2010 Author Share Posted October 19, 2010 I have no idea where to go from here Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1123703 Share on other sites More sharing options...
Pikachu2000 Posted October 19, 2010 Share Posted October 19, 2010 Actually, there are two different instances of the array you'll need to deal with when using checkboxes. One is when you set up the form. The next is in processing the data from the form. Here's an example you can cut and paste into a new file to help you understand how it works. I was something like this that helped me understand how arrays interact with forms, and how they can be used to keep things grouped and organized. This isn't the only way to do it, but I feel it's one of the better ways, especially if you have a large number of checkboxes, or groups of checkboxes. <?php if( $_POST['submitted'] == 'yes') { echo '<pre>'; print_r($_POST); echo '</pre><br>'; if( is_array($_POST['boxes']) ) { foreach( $_POST['boxes'] as $key => $value ) { echo "Index: $key - Value: $value<br>"; } } } ?> <html> <form action="" method="POST"> First: <input type="checkbox" name="boxes[]" value="first" <?php echo (is_array($_POST['boxes']) && in_array('first', $_POST['boxes'])) ? 'checked = "checked"' : ''; ?>><br> Second: <input type="checkbox" name="boxes[]" value="second" <?php echo (is_array($_POST['boxes']) && in_array('second', $_POST['boxes'])) ? 'checked = "checked"' : ''; ?>><br> Third: <input type="checkbox" name="boxes[]" value="third" <?php echo (is_array($_POST['boxes']) && in_array('third', $_POST['boxes'])) ? 'checked = "checked"' : ''; ?>><br> <input type="hidden" name="submitted" value="yes"> <input type="submit" name="submit" value="Submit"> </form> </html> ?> Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1123713 Share on other sites More sharing options...
deziner76hd Posted October 19, 2010 Author Share Posted October 19, 2010 Thank you can you tell me why it prints this above the answers it prints? Array ( [boxes] => Array ( [0] => first [1] => second ) [submitted] => yes [submit] => Submit ) This is what it prints in entirety. Array ( [boxes] => Array ( [0] => first [1] => second [2] => third ) [submitted] => yes [submit] => Submit ) Index: 0 - Value: first Index: 1 - Value: second Index: 2 - Value: third First: Second: Third: I would think it should just print this with all three checked Index: 0 - Value: first Index: 1 - Value: second Index: 2 - Value: third Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1123721 Share on other sites More sharing options...
deziner76hd Posted October 19, 2010 Author Share Posted October 19, 2010 If I include it in a form and print to email it sends a return of Array A user gary submitted the contact form: Full Name: gary Email: [email protected] Gender: male Languages I Speak: English Preferred Language: en All Boxex: Array Q1: an answer Message: ukg IP: 74.74.118.219 Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1123722 Share on other sites More sharing options...
deziner76hd Posted October 19, 2010 Author Share Posted October 19, 2010 Did you notice it's 1:00am again? it seems like I am always up at 1:00 am working on something. I want you to know I really do appreciate all the time you have put in helping me. Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1123723 Share on other sites More sharing options...
Pikachu2000 Posted October 19, 2010 Share Posted October 19, 2010 The first part is the output from the print_r($_POST). I threw that in there so you'd be able to see exactly how the $_POST array is structured. That code block can be commented out or removed at any time. That output shows that $_POST is an array, containing 3 elements: 'boxes', 'submitted', and 'submit'. You can see the 'boxes' element is also an array, itself containing the 3 elements from the checkboxes. The next block of code uses a foreach() loop to to through just the array in 'boxes', and output the values to the screen. Is this starting to make more sense now? Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1123839 Share on other sites More sharing options...
deziner76hd Posted October 19, 2010 Author Share Posted October 19, 2010 Yes The last explanation of how it is all put together actually clicked and gave me insight into how it all works. Now I will try to implement it into my existing form and get it to work. Thank you Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1123858 Share on other sites More sharing options...
deziner76hd Posted October 19, 2010 Author Share Posted October 19, 2010 Ok I see how this all works but I still can't get it to print to e-mail Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1124021 Share on other sites More sharing options...
deziner76hd Posted October 19, 2010 Author Share Posted October 19, 2010 Well I am trying to learn and understand all this. I start to think I have things under control and when I try what I think will work (several variations) and it doesn't it's gets a little frustrating. This is what is returned A user Gary T White submitted the contact form: (working) Full Name: Gary T White (working) Email: [email protected] (working) Gender: female (working) Languages I Speak: English (not working) Preferred Language: English (working) Favorite color: Array (not working) Education: HighSchool (working) Check Boxes: Array (not working) Q1: the ands Message: xcghnfcg IP: 74.74.118.219 Above Head <?php $your_email ='[email protected]';// <<=== update to your email address session_start(); $errors = ''; $name = ''; $visitor_email = ''; $user_message = ''; if(isset($_POST['submit'])) { $name = $_POST['name']; $visitor_email = $_POST['email']; $visitor_q1 = $_POST['q1']; $user_message = $_POST['message']; $gender = $_POST['gender']; $language = $_POST['lang']; $Preferred = $_POST['preflang']; $color = $_POST['color']; $education = $_POST['education']; $boxes = $_POST['boxes']; ///------------checkbox------------- if( $_POST['submitted'] == 'yes') { echo '<pre>'; print_r($_POST); echo '</pre><br>'; if( is_array($_POST['boxes']) ) { foreach( $_POST['boxes'] as $key => $value ) { echo "Index: $key - Value: $value<br>"; } } } ///// collecting form data color ///// @$color= $_POST['color']; if( is_array($color)){ while (list ($key, $val) = each ($color)) { ////---echo "$val <br>"; } }//else{echo "not array";} ///------------Do Validations------------- if(empty($name)||empty($visitor_email)) { $errors .= "\n Name and Email are required fields. "; } if(IsInjected($visitor_email)) { $errors .= "\n Bad email value!"; } if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "\n The captcha code does not match!"; } if(empty($errors)) { //send the email $to = $your_email; $subject="New form submission"; $from = $your_email; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $body = "A user $name submitted the contact form:\n". "Full Name: $name\n". "Email: $visitor_email \n". "Gender: $gender \n". "Languages I Speak: $lang \n". "Preferred Language: $preflang \n". "Favorite color: $color \n". "Education: $education \n". "Check Boxes: $boxes \n". "Q1: $visitor_q1 \n". "Message: \n ". "$user_message\n". "IP: $ip\n"; $headers = "From: $from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; mail($to, $subject, $body,$headers); header('Location: thank-you.html'); } } // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?> Form <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST" name="contact_form" class="style25"> <p> <label for='name'>Full Name: </label><br> <input type="text" name="name" value='<?php echo htmlentities($name) ?>'> </p> <p> <label for='email'>Email: </label><br> <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'> <p> <p> <label for='q1'>Q1: </label><br> <input type="text" name="q1" value='<?php echo htmlentities($visitor_q1) ?>'> <p>My gender is: <p> <input type="radio" name="gender" value="male" <?php echo $_POST['gender'] == 'male' ? 'selected="selected"' : ''; ?>/> <label for='gender'>Male: </label> <br /> <input type="radio" name="gender" value="female" <?php echo $_POST['gender'] == 'female' ? 'selected="selected"' : ''; ?>/> <label for='Gender'>Female:</label> <br /><br /> <label for='boxes'>Checkboxes:</label><br /><br /> First: <input type="checkbox" name="boxes[]" value="first" <?php echo (is_array($_POST['boxes']) && in_array('first', $_POST['boxes'])) ? 'checked = "checked"' : ''; ?>><br> Second: <input type="checkbox" name="boxes[]" value="second" <?php echo (is_array($_POST['boxes']) && in_array('second', $_POST['boxes'])) ? 'checked = "checked"' : ''; ?>><br> Third: <input type="checkbox" name="boxes[]" value="third" <?php echo (is_array($_POST['boxes']) && in_array('third', $_POST['boxes'])) ? 'checked = "checked"' : ''; ?>><br> <br /><br />Select a Level of Education:<br /><br /> <select name="education"> <option value="Jr.High">Jr.High</option> <option value="HighSchool">HighSchool</option> <option value="College">College</option></select>:<br /><br /> Select your favorite time of day::<br /><br /> <select name="TofD" size="3"> <option value="Morning">Morning</option> <option value="Day">Day</option> <option value="Night">Night</option></select>:<br /><br /> Select your favorite color::<br /><br /> <select name='color[]' size=4 multiple> <option value='blue'>Blue</option> <option value='green'>Green</option> <option value='red'>Red</option> <option value='yellow'>Yelllow</option> <option value='' selected>Select a Color </option> <option value='white'>White</option> </select> <p>I speak the following languages: </p> <p> <input type="checkbox" name="lang" value="English" <?php echo $_POST['lang'] == 'en' ? 'selected="selected"' : ''; ?>/> English<br /> <input type="checkbox" name="lang" value="Français" <?php echo $_POST['lang'] == 'fr' ? 'selected="selected"' : ''; ?>/> Français<br /> <input type="checkbox" name="lang" value="Español" <?php echo $_POST['lang'] == 'es' ? 'selected="selected"' : ''; ?>/> Español<br /> </p> <p>My preferred language is: </p> <input type="radio" name="preflang" value="English" <?php echo $_POST['preflang'] == 'en' ? 'selected="selected"' : ''; ?>/> <label for='Preferred'>English: </label> <br /> <input type="radio" name="preflang" value="Français" <?php echo $_POST['preflang'] == 'fr' ? 'selected="selected"' : ''; ?>/> <label for='Preferred'>Français:</label> <br /> <input type="radio" name="preflang" value="Español" <?php echo $_POST['preflang'] == 'es' ? 'selected="selected"' : ''; ?>/> <label for='Preferred'>Español:</label> <p> <label for='message'>Message:</label> <br> <textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea> </p> <p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br> <label for='message'>Enter the code above here :</label><br> <input id="6_letters_code" name="6_letters_code" type="text"><br> <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small></p> <input type="submit" value="Submit" name='submit'> </form> Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1124054 Share on other sites More sharing options...
deziner76hd Posted October 19, 2010 Author Share Posted October 19, 2010 What am I not getting here? Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1124144 Share on other sites More sharing options...
deziner76hd Posted October 20, 2010 Author Share Posted October 20, 2010 Ok I commented out: if( $_POST['submitted'] == 'yes') { echo '<pre>'; print_r($_POST); echo '</pre><br>'; And All I can get it to print to email is: Array Why? Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1124176 Share on other sites More sharing options...
deziner76hd Posted October 20, 2010 Author Share Posted October 20, 2010 And with the above commented out i still get this printed on the page Index: 0 - Value: first Index: 1 - Value: second Index: 2 - Value: third Warning: Cannot modify header information - headers already sent by (output started at /home/richardi/public_html/test2.php:33) in /home/richardi/public_html/test2.php on line 93 Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1124205 Share on other sites More sharing options...
Pikachu2000 Posted October 20, 2010 Share Posted October 20, 2010 Ok I commented out: if( $_POST['submitted'] == 'yes') { echo '<pre>'; print_r($_POST); echo '</pre><br>'; And All I can get it to print to email is: Array Why? You can't print an array with echo. You need to print it by referencing the index key of the element that the value you want to print is stored in. That is done like this: echo $array['element']; and can be done in a loop to echo them all recursively. foreach( $array as $key => $value ) { echo "The index with key: <b>$key<b> holds the value: <b>$value</b><br>"; } And with the above commented out i still get this printed on the page Index: 0 - Value: first Index: 1 - Value: second Index: 2 - Value: third Warning: Cannot modify header information - headers already sent by (output started at /home/richardi/public_html/test2.php:33) in /home/richardi/public_html/test2.php on line 93 You'll notice a foreach loop like the above in the example code I gave you that's outputting those values. As far as the header error goes, you can't use header() after there has been any output at all to the browser, or you'll get that error, and the header() call will fail. Link to comment https://forums.phpfreaks.com/topic/216109-need-to-include-radio-buttons-and-checkboxes/page/2/#findComment-1124377 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.