shaunmacie Posted March 3, 2007 Share Posted March 3, 2007 I'm a PHP novice but I have been able to create simple forms on my html pages using a .php processor. For one I am working on now, I need to have two checkbox sections where the user can click multiple choices in each. It goes through my .php file and get's emailed. I need help with the code in the .php file on how to process two checkbox arrays and email the result. Here is the code I am working with: The form in my .html file ------------ <form name="student" id="student" method="post" action="student.php"> <A NAME="student"></a><p><span class="headline1">Student Section</span> Are you happy with your academic experience so far? If not, what would help to make it more enjoyable? <textarea name="student_01" cols="50" rows="5" class="form" id="student_01"></textarea> What area of academic life do you find most stressful? <textarea name="student_02" cols="50" rows="5" class="form" id="student_02"></textarea> Have you ever suffered from the following while in school: (Check all that apply) <input type="checkbox" name="student_03[]" value="depression">depression <input type="checkbox" name="student_03[]" value="anxiety">anxiety <input type="checkbox" name="student_03[]" value="insomnia">insomnia <input type="checkbox" name="student_03[]" value="stomachache">stress-related stomachaches <input type="checkbox" name="student_03[]" value="bodyimage">body image problems <input type="checkbox" name="student_03[]" value="overwhelm">overwhelm Are these problems better/worse when school's not in session? <textarea name="student_04" cols="50" rows="5" class="form" id="student_04"></textarea> Do you find the pressure to get good grades to be a major concern? <textarea name="student_05" cols="50" rows="5" class="form" id="student_05"></textarea> Have you ever become upset when you've received a non-A on quiz or paper? <textarea name="student_06" cols="50" rows="5" class="form" id="student_06"></textarea> How do you coach yourself to relax when and if this happens? <textarea name="student_07" cols="50" rows="5" class="form" id="student_07"></textarea> What is your single greatest worry about school in general? <textarea name="student_08" cols="50" rows="5" class="form" id="student_08"></textarea> Which area do you feel you need the most help with regarding your academic career? (Check all that apply) <input type="checkbox" name="student_09[]" value="stress_reduction">Stress reduction <input type="checkbox" name="student_09[]" value="career_advice">Career advice <input type="checkbox" name="student_09[]" value="overcoming_perfectionism">Overcoming perfectionism <input type="checkbox" name="student_09[]" value="time_management">Time management What would you like to see more of on the Campus Calm's website? <textarea name="student_10" cols="50" rows="5" class="form" id="student_10"></textarea> <input type="submit" name="submit" value="Submit Student Survey"></form> ----------- my .php file I need code for 4 places that I have marked to make the checkbox array process and then email ---------- <?php function checkOK($field) { if (eregi("\r",$field) || eregi("\n",$field)){ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">"; exit; } } $EmailTo = "[email protected]"; $Subject = "Campus Calm Student Survey"; $student_01 = Trim(stripslashes($_POST['student_01'])); checkOK($student_01); $student_02 = Trim(stripslashes($_POST['student_02'])); checkOK($student_02); //---->This is where I need code to process checkbox array "student_03"<------ $student_04 = Trim(stripslashes($_POST['student_04'])); checkOK($student_0); $student_05 = Trim(stripslashes($_POST['student_05'])); checkOK($student_05); $student_06 = Trim(stripslashes($_POST['student_06'])); checkOK($student_06); $student_07 = Trim(stripslashes($_POST['student_07'])); checkOK($student_07); $student_08 = Trim(stripslashes($_POST['student_08'])); checkOK($student_08); //---->This is where I need code to process checkbox array "student_09"<------ $student_10 = Trim(stripslashes($_POST['student_10'])); checkOK($student_10); // prepare email body text $Body = ""; $Body .= "student_01: "; $Body .= $student_01; $Body .= "\n"; $Body .= "student_02: "; $Body .= $student_02; $Body .= "\n"; //---->This is where I need code to email checkbox array "student_03"<------ $Body .= "student_04: "; $Body .= $student_04; $Body .= "\n"; $Body .= "student_05: "; $Body .= $student_05; $Body .= "\n"; $Body .= "student_06: "; $Body .= $student_06; $Body .= "\n"; $Body .= "student_07: "; $Body .= $student_07; $Body .= "\n"; $Body .= "student_08: "; $Body .= $student_08; $Body .= "\n"; //---->This is where I need code to email checkbox array "student_09"<------ $Body .= "student_10: "; $Body .= $student_10; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: Campus Calm"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=student_sent.html\">"; } ?> ---------- If anyone could help out I would be most grateful! I have been searching the net for 6 hours trying to find code that would work. Just about everything I find to process checkbox array statements has it processing on the same page and echoing the results. I need it to email the results out (this is a survey). I only know PHP basics, and this is a bit beyond my level! Thanks, Shaun [email protected] Link to comment https://forums.phpfreaks.com/topic/41045-solved-php-form-processor-and-checkbox-array-help/ Share on other sites More sharing options...
genericnumber1 Posted March 3, 2007 Share Posted March 3, 2007 would you mind removing the colors and putting them in the code tags? it makes it much easier to read, thanks! Link to comment https://forums.phpfreaks.com/topic/41045-solved-php-form-processor-and-checkbox-array-help/#findComment-198763 Share on other sites More sharing options...
LazyJones Posted March 3, 2007 Share Posted March 3, 2007 you can catch the array just like the other posted variables: e.g $student_03 = $_POST['student_03']; $student_03 is an array holding all the checked values Link to comment https://forums.phpfreaks.com/topic/41045-solved-php-form-processor-and-checkbox-array-help/#findComment-198769 Share on other sites More sharing options...
shaunmacie Posted March 3, 2007 Author Share Posted March 3, 2007 Here it is in code boxes: The form in my .html file ------------ form name="student" id="student" method="post" action="student.php"> <A NAME="student"></a><p><span class="headline1">Student Section</span> Are you happy with your academic experience so far? If not, what would help to make it more enjoyable? <textarea name="student_01" cols="50" rows="5" class="form" id="student_01"></textarea> What area of academic life do you find most stressful? <textarea name="student_02" cols="50" rows="5" class="form" id="student_02"></textarea> Have you ever suffered from the following while in school: (Check all that apply) <input type="checkbox" name="student_03[]" value="depression">depression <input type="checkbox" name="student_03[]" value="anxiety">anxiety <input type="checkbox" name="student_03[]" value="insomnia">insomnia <input type="checkbox" name="student_03[]" value="stomachache">stress-related stomachaches <input type="checkbox" name="student_03[]" value="bodyimage">body image problems <input type="checkbox" name="student_03[]" value="overwhelm">overwhelm Are these problems better/worse when school's not in session? <textarea name="student_04" cols="50" rows="5" class="form" id="student_04"></textarea> Do you find the pressure to get good grades to be a major concern? <textarea name="student_05" cols="50" rows="5" class="form" id="student_05"></textarea> Have you ever become upset when you've received a non-A on quiz or paper? <textarea name="student_06" cols="50" rows="5" class="form" id="student_06"></textarea> How do you coach yourself to relax when and if this happens? <textarea name="student_07" cols="50" rows="5" class="form" id="student_07"></textarea> What is your single greatest worry about school in general? <textarea name="student_08" cols="50" rows="5" class="form" id="student_08"></textarea> Which area do you feel you need the most help with regarding your academic career? (Check all that apply) <input type="checkbox" name="student_09[]" value="stress_reduction">Stress reduction <input type="checkbox" name="student_09[]" value="career_advice">Career advice <input type="checkbox" name="student_09[]" value="overcoming_perfectionism">Overcoming perfectionism <input type="checkbox" name="student_09[]" value="time_management">Time management What would you like to see more of on the Campus Calm's website? <textarea name="student_10" cols="50" rows="5" class="form" id="student_10"></textarea> <input type="submit" name="submit" value="Submit Student Survey"></form> ----------- my .php file I need code for 4 places that I have marked to make the checkbox array process and then email ---------- <?php function checkOK($field) { if (eregi("\r",$field) || eregi("\n",$field)){ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">"; exit; } } $EmailTo = "[email protected]"; $Subject = "Campus Calm Student Survey"; $student_01 = Trim(stripslashes($_POST['student_01'])); checkOK($student_01); $student_02 = Trim(stripslashes($_POST['student_02'])); checkOK($student_02); [color=red]//---->This is where I need code to process checkbox array "student_03"<------[/color] $student_04 = Trim(stripslashes($_POST['student_04'])); checkOK($student_0); $student_05 = Trim(stripslashes($_POST['student_05'])); checkOK($student_05); $student_06 = Trim(stripslashes($_POST['student_06'])); checkOK($student_06); $student_07 = Trim(stripslashes($_POST['student_07'])); checkOK($student_07); $student_08 = Trim(stripslashes($_POST['student_08'])); checkOK($student_08); [color=red]//---->This is where I need code to process checkbox array "student_09"<------[/color] $student_10 = Trim(stripslashes($_POST['student_10'])); checkOK($student_10); // prepare email body text $Body = ""; $Body .= "student_01: "; $Body .= $student_01; $Body .= "\n"; $Body .= "student_02: "; $Body .= $student_02; $Body .= "\n"; [color=red]//---->This is where I need code to email checkbox array "student_03"<------[/color] $Body .= "student_04: "; $Body .= $student_04; $Body .= "\n"; $Body .= "student_05: "; $Body .= $student_05; $Body .= "\n"; $Body .= "student_06: "; $Body .= $student_06; $Body .= "\n"; $Body .= "student_07: "; $Body .= $student_07; $Body .= "\n"; $Body .= "student_08: "; $Body .= $student_08; $Body .= "\n"; [color=red]//---->This is where I need code to email checkbox array "student_09"<------[/color] $Body .= "student_10: "; $Body .= $student_10; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: Campus Calm"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=student_sent.html\">"; } ?> ---------- Thanks, Shaun [email protected] Link to comment https://forums.phpfreaks.com/topic/41045-solved-php-form-processor-and-checkbox-array-help/#findComment-198776 Share on other sites More sharing options...
shaunmacie Posted March 3, 2007 Author Share Posted March 3, 2007 you can catch the array just like the other posted variables: e.g $student_03 = $_POST['student_03']; $student_03 is an array holding all the checked values If I do that, the form works but the emailed results for the checkbox fields come through and just say "ARRAY". Here is the email response that I get: student_01: test student_02: test student_03: Array student_04: testaadf student_05: test student_06: test student_07: tset student_08: test student_09: Array student_10: est I think I need to have something else in the .php where is creates the email, instead of: $Body .= "student_09: "; $Body .= $student_09; $Body .= "\n"; The response needs to list whatever items were checked, whether it's none, one, or all of the choices. Link to comment https://forums.phpfreaks.com/topic/41045-solved-php-form-processor-and-checkbox-array-help/#findComment-198786 Share on other sites More sharing options...
LazyJones Posted March 3, 2007 Share Posted March 3, 2007 it echoes 'Array' because it is an array... if you want to echo all the values in the array, use something like foreach($value in $student_03) { echo $value; } Link to comment https://forums.phpfreaks.com/topic/41045-solved-php-form-processor-and-checkbox-array-help/#findComment-198790 Share on other sites More sharing options...
shaunmacie Posted March 3, 2007 Author Share Posted March 3, 2007 it echoes 'Array' because it is an array... if you want to echo all the values in the array, use something like foreach($value in $student_03) { echo $value; } Please forgive my novice ignorance, but exactly where would I put that? I tried it a few times and it made the form stop working. Here is the current code in the .php file: <?php function checkOK($field) { if (eregi("\r",$field) || eregi("\n",$field)){ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">"; exit; } } $EmailTo = "[email protected]"; $Subject = "Campus Calm Student Survey"; $student_01 = Trim(stripslashes($_POST['student_01'])); checkOK($student_01); $student_02 = Trim(stripslashes($_POST['student_02'])); checkOK($student_02); $student_03 = $_POST['student_03']; checkOK($student_03); $student_04 = Trim(stripslashes($_POST['student_04'])); checkOK($student_0); $student_05 = Trim(stripslashes($_POST['student_05'])); checkOK($student_05); $student_06 = Trim(stripslashes($_POST['student_06'])); checkOK($student_06); $student_07 = Trim(stripslashes($_POST['student_07'])); checkOK($student_07); $student_08 = Trim(stripslashes($_POST['student_08'])); checkOK($student_08); $student_09 = $_POST['student_09']; checkOK($student_09); $student_10 = Trim(stripslashes($_POST['student_10'])); checkOK($student_10); // prepare email body text $Body = ""; $Body .= "student_01: "; $Body .= $student_01; $Body .= "\n"; $Body .= "student_02: "; $Body .= $student_02; $Body .= "\n"; $Body .= "student_03: "; $Body .= $student_03; $Body .= "\n"; $Body .= "student_04: "; $Body .= $student_04; $Body .= "\n"; $Body .= "student_05: "; $Body .= $student_05; $Body .= "\n"; $Body .= "student_06: "; $Body .= $student_06; $Body .= "\n"; $Body .= "student_07: "; $Body .= $student_07; $Body .= "\n"; $Body .= "student_08: "; $Body .= $student_08; $Body .= "\n"; $Body .= "student_09: "; $Body .= $student_09; $Body .= "\n"; $Body .= "student_10: "; $Body .= $student_10; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: Campus Calm"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=student_sent.html\">"; } ?> Link to comment https://forums.phpfreaks.com/topic/41045-solved-php-form-processor-and-checkbox-array-help/#findComment-198807 Share on other sites More sharing options...
shaunmacie Posted March 3, 2007 Author Share Posted March 3, 2007 it echoes 'Array' because it is an array... if you want to echo all the values in the array, use something like foreach($value in $student_03) { echo $value; } I got it!: foreach($_POST['student_03'] as $value) { $student_03 .= "Checked: $value\n"; } Link to comment https://forums.phpfreaks.com/topic/41045-solved-php-form-processor-and-checkbox-array-help/#findComment-198829 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.