fazlionline Posted February 20, 2010 Report Posted February 20, 2010 (edited) Contact us form Full Name: Persoanl Email: Operating System WindowsLinuxMobile Gender Male Female Languages: English Pashto Dari Message: File Attachment: Edited February 20, 2010 by fazlionline Quote
falkencreative Posted February 20, 2010 Report Posted February 20, 2010 From http://www.computing.net/answers/webdevel/php-amp-checkboxes/1122.html Remember that checkboxes with the same name work in a group, and the form will pass their value as an array. However, in your form you should tell explicitly that the checkboxes should pass their value as an array. For example: So in your case your script should loop through all the elements in the area to see if the box was checked or not. For example: $state=$_POST['state']; foreach ($state as $statename) { echo "$statename is checked"; } ?> Basically, the important bits are that you need to add "[]" to the end of your checkbox's name, so that PHP understands that it's dealing with an array of values, and then use a foreach loop to get each of those values within the PHP script. Quote
fazlionline Posted February 20, 2010 Author Report Posted February 20, 2010 falkencreative i have changes my script according to your suggestion now i get a messages after i submit form" English is checkedPashto is checkedDari is checkedMail sent! Yay PHP! but in my email, i get: language: Array Quote
fazlionline Posted February 20, 2010 Author Report Posted February 20, 2010 instead of Print, how i can store the loop value in a variable and send that variable through mail() Quote
falkencreative Posted February 20, 2010 Report Posted February 20, 2010 You weren't really supposed to simply copy/paste the code -- it contains the method you need to follow to get this working, not the solution itself. If you simply copy and paste everything, rather than understanding how it works, how are you going to learn? The checkboxes are already in a variable -- you just need to loop through and add them to your email. For example: $details = "\n URL - Form: $url\n Subject: $subject\n Name: $name\n email: $email\n system: $system \n gender: $gender \n language: "; foreach ($language as $lang) { $details .= "$lang "; } $details .= "\n"; $details .= "message: $message \n"; Quote
fazlionline Posted February 21, 2010 Author Report Posted February 21, 2010 (edited) thanks i read PHP foreach and PHP array in TIZAG.com thanks alot. now, i have two qestions: 1 - if a user do not click any of the optoin in langauges, i get error: Warning: Invalid argument supplied for foreach() in d:\Customers\user1092190\www\t6_e\contactus.php on line 21 Mail sent! Yay PHP! 2 - as i am working on Job Application Form, i need such more checkboxes group. for example, this one was for language, other may be for Interesets (Interest: PHP, JAVA, CSS), other may be for somthing else. so how i can make all together in my above PHP scrip? thanks Edited February 21, 2010 by fazlionline Quote
falkencreative Posted February 21, 2010 Report Posted February 21, 2010 1) Try replacing the foreach loop with this: if (isset($language) && $language != '') { foreach ($language as $lang) { $details .= "$lang "; } } I believe that should fix the error, though I haven't tested it. 2) You just need to repeat what I've shown you to do. Create other checkboxes that share the same name and include "[]" at the end of their name so PHP realizes they are an array, get the checkbox values using $_POST or $_REQUEST, and then loop through each the checkbox values using a foreach loop to add them to your $results variable so it gets emailed to you. Quote
fazlionline Posted February 21, 2010 Author Report Posted February 21, 2010 (edited) Thanks, now I do not get error if all checkboxes are left blank. I have added second group, interests. It also works fine but I get languages and interests values all in one line in my email. I have changed the code to this: if (isset($language) && $language != '') {foreach ($language as $lang){ $details .= "$lang - " ;}} $para = " "; $para; if (isset($interest) && $interest != '') {foreach ($interest as $int){ $details .= "$int - ";}} But didn?t worked How I can get them to appear each group in each line. Like ...Languages: English - Pashto - Dari Interests: PHP - Java - CSS ... My HTML Code is: Languages: English Pashto Dari Interests: PHP Java CSS Thanks Edited February 21, 2010 by fazlionline Quote
falkencreative Posted February 21, 2010 Report Posted February 21, 2010 Rather than trying to add tags, do this instead: $details .= "\n"; Quote
fazlionline Posted February 22, 2010 Author Report Posted February 22, 2010 (edited) thanks alot. can i store the value returend by foreach in a variable? foeexample: if (isset($interest) && $interest != '') {foreach ($interest as $int){ $details .= "$int - ";}} $SelectedInterests = $int ; becuse i want to add this varibale in $details : ... $details = "\n URL - Form: $url\n Subject: $subject\n Name: $name\n email: $email\n system: $system \n gender: $gender \n message: $message \n [b][color=red]Interest: $int \n[/color][/b] language: "; ... thanks Edited February 22, 2010 by fazlionline Quote
falkencreative Posted February 22, 2010 Report Posted February 22, 2010 You could. Something like this should work (placed in the area of your code that contains "$language = $_REQUEST['language'] ; $message = $_REQUEST['message'] ;" etc...): $selectedInterests = ''; $interest = $_REQUEST['interest'] ; //this assumes that your checkboxes are named "interest[]" if (isset($interest) && $interest != '') {foreach ($interest as $int){ $selectedInterests .= "$int - ";}} and then in when you create the details variable: $details = "\n URL - Form: $url\n Subject: $subject\n Name: $name\n email: $email\n system: $system \n gender: $gender \n message: $message \n Interest: $selectedInterests \n language: "; Quote
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.
Note: Your post will require moderator approval before it will be visible.