March 15, 201214 yr Guy's need some help. I've got a contact form which works fine, including validation. I can't figure out how to get the information selected in my checkboxes sent with the rest of the email. This is bit of the code I have for my form; <label class="checkbox">UK Wide</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="UKWide" /> <label class="checkbox">Scotland</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="Scotland" /> And my PHP engine is; $EmailFrom = "info@mysite.co.uk"; $EmailTo = "info@mysite.co.uk"; $Subject = "Contact Form"; $CompanyName = Trim(stripslashes($_POST['CompanyName'])); $Email = Trim(stripslashes($_POST['Email'])); $Website = Trim(stripslashes($_POST['Website'])); $Description = Trim(stripslashes($_POST['Description'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "CompanyName: "; $Body .= $CompanyName; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Description: "; $Body .= $Description; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); Basically I have no idea how to retrieve the data from the checkboxes and as my PHP knowledge is pretty basic I'm really struggling with the info I'm finding on-line. Any help would be appreciated. Cheers.
March 15, 201214 yr Guy's need some help. I've got a contact form which works fine, including validation. I can't figure out how to get the information selected in my checkboxes sent with the rest of the email. This is bit of the code I have for my form; Basically I have no idea how to retrieve the data from the checkboxes and as my PHP knowledge is pretty basic I'm really struggling with the info I'm finding on-line. Any help would be appreciated. Cheers. Try this Checkbox
March 15, 201214 yr $_POST['Regions'] will contain an array of selected values. If you do this it should give you a better idea of what's going on: foreach($_POST['Regions'] as $region){ echo $regions; } Edited March 15, 201214 yr by TheSimpleHost-Nathan
March 15, 201214 yr I think you mean: foreach($_POST['Regions'] as $region){ echo $region; } +1 anyway.
March 15, 201214 yr I think you mean: foreach($_POST['Regions'] as $region){ echo $region; } +1 anyway. Whoops. That's what I get for C&Ping - cheers.
March 15, 201214 yr <label class="checkbox">UK Wide</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="UKWide" /> <label class="checkbox">Scotland</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Scotland" /> <input type='submit' name='submit' value='Get Value'> <?php foreach($_POST['Regions'] as $region){ echo $region; } ?>
March 15, 201214 yr <label class="checkbox">UK Wide</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="UKWide" /> <label class="checkbox">Scotland</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Scotland" /> <input type='submit' name='submit' value='Get Value'> <?php foreach($_POST['Regions'] as $region){ echo $region; } ?> No, the code I posted should go in the process.php file.
March 15, 201214 yr No, the code I posted should go in the process.php file. $Body = ""; $Body .= "CompanyName: "; $Body .= $CompanyName; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Description: "; $Body .= $Description; $Body .= "\n"; $Body .= "Regions: "; foreach($_POST['Regions'] as $region){ $Regions = $region . "\n"; $Body .= $Regions; $Body .= "\n"; }
March 15, 201214 yr Sorry got a bit confused over who was the OP. MWP you need this: <label class="checkbox">UK Wide</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="UKWide" /> <label class="checkbox">Scotland</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Scotland" /> <input type='submit' name='submit' value='Get Value'> AND IN YOUR PHP $Body = ""; $Body .= "CompanyName: "; $Body .= $CompanyName; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Description: "; $Body .= $Description; $Body .= "\n"; $Body .= "Regions: "; foreach($_POST['Regions'] as $region){ $Regions = $region . "\n"; $Body .= $Regions; $Body .= "\n"; } Nathan. The answer you commented on was not meant to be used as written but just a pointer in the general direction. Edited March 15, 201214 yr by GalaxyTramp
March 16, 201214 yr Author Really appreciate your help so far, but I still can't get this to work. I'll post the whole code in case something I missed off earlier is important. The form code; <form method="post" class="required-form" action="php/addcompanyengine.php"> <ul class="forms"> <li><label for="CompanyName"><em class="required">*</em> Company Name:</label> <input type="text" name="CompanyName" id="CompanyName" class="required" /></li> <li><label for="Email"><em class="required">*</em> Email</label> <input type="text" name="Email" id="Email" class="required" /></li> <li><label for="Website"><em class="required-form">*</em> Website:</label> <input type="text" name="Website" id="Website" class="required" /></li> </ul> <p class="Region">Please select the region/regions that your security services can cover</p><br /> <ol> <li><label class="checkbox">UK Wide</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="UKWide" /></li> <li><label class="checkbox">Scotland</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="Scotland" /></li> <li><label class="checkbox">North East</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="NorthEast" /></li> <li><label class="checkbox">North West</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="NorthWest" /></li> </ol> <ol> <li><label class="checkbox">Yorkshire</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="Yorkshire" /></li> <li><label class="checkbox">West Midlands</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="WestMidlands" /></li> <li><label class="checkbox">Wales</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="Wales" /></li> <li><label class="checkbox">East Midlands</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="EastMidlands" /></li> </ol> <ol> <li><label class="checkbox">East Anglia</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="EastAnglia" /></li> <li><label class="checkbox">London</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="London" /></li> <li><label class="checkbox">South West</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="SouthWest" /></li> <li><label class="checkbox">South East</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="SouthEast" /></li> </ol> <ol> <li><label class="checkbox">Northern Ireland</label> <input name="Regions" type="checkbox" class="checkbox" id="Regions[]" value="NorthernIreland" /></li><br /> </ol> <p class="Region">Please select the security services you provide</p><br /> <ol> <li><label class="checkbox">Keyholding</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="Keyholding" /></li> <li><label class="checkbox">CCTV</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="CCTV" /></li> <li><label class="checkbox">Close Protection</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="CloseProtection" /></li> <li><label class="checkbox">Door Supervisors</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="DoorSupervision" /></li> <li><label class="checkbox">Residential Security</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="ResidentialSecurity" /></li> </ol> <ol> <li><label class="checkbox">Security Dogs</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="SecurityDogs" /></li> <li><label class="checkbox">Security Guards</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="SecurityGuards" /></li> <li><label class="checkbox">Security Kit & Equipment</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="SecurityKit" /></li> <li><label class="checkbox">Surveillance & Investigations</label> <input name="Services" type="checkbox" class="checkbox" id="Services[]" value="Surveillance" /></li> <li><label class="checkbox">Training Companies</label> <input name="Services" class="checkbox" type="checkbox" id="Services[]" value="Training" /></li> </ol> <p class="Region"></p> <br /> <ul> <li><label for="Description">Description:<br /><br /> (Silver listings only <br /> <br /> Restricted to 40 words)</label> <textarea name="Description" cols="20" id="Description"></textarea> </li> <li><input type="submit" name="submit" value="Submit" class="submit-button" /></li> </ul> </form> </select> And my PHP Script is as follows <?php $EmailFrom = "info@mywebsite.co.uk"; $EmailTo = "info@mywebsite.co.uk"; $Subject = "Contact Form"; $CompanyName = Trim(stripslashes($_POST['CompanyName'])); $Email = Trim(stripslashes($_POST['Email'])); $Website = Trim(stripslashes($_POST['Website'])); $Description = Trim(stripslashes($_POST['Description'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "CompanyName: "; $Body .= $CompanyName; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Description: "; $Body .= $Description; $Body .= "\n"; $Body .= "Regions: "; foreach($_POST['Regions'] as $region){ $Regions = $region . "\n"; } $Body .= $Regions; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?> The e-mail I'm receiving after submit is hit has all of the information, followed by region: but then it doesn't show the selected region information? Edited March 16, 201214 yr by MWP
March 16, 201214 yr Author OK, I've managed to get one of the check boxes working. If I make multiple selections I only receive one of the selection values in my email. Any ideas? I've added the following code into my PHP which returns only one of the selected check boxes $Regions = Trim(stripslashes($_POST['Regions'])); So now my full PHP looks like this <?php $EmailFrom = "info@uksecurity-directory.co.uk"; $EmailTo = "info@uksecurity-directory.co.uk"; $Subject = "Contact Form"; $CompanyName = Trim(stripslashes($_POST['CompanyName'])); $Email = Trim(stripslashes($_POST['Email'])); $Website = Trim(stripslashes($_POST['Website'])); $Description = Trim(stripslashes($_POST['Description'])); $Regions = Trim(stripslashes($_POST['Regions'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "CompanyName: "; $Body .= $CompanyName; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Description: "; $Body .= $Description; $Body .= "\n"; $Body .= "Regions: "; foreach($_POST['Regions'] as $Regions){ $Regions = $Regions . "\n"; } $Body .= $Regions; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?>
March 16, 201214 yr Change $Regions = $Regions . "\n"; to $Body .= $Regions . "\n"; and remove $Body .= $Regions;
March 16, 201214 yr Author Change $Regions = $Regions . "\n"; to $Body .= $Regions . "\n"; and remove $Body .= $Regions; OK, have done that but now I'm back to getting no information from the regions. My mail received looks like this; CompanyName: Test Company Website: Test www. Email: Test@ Description: Testing Regions: Could it be something else in the form? I really have no idea when it comes to PHP other than what's needed for a basic contact form. Before changing the script as you suggested I would receive the email after filling in the form with two or more of my check boxes (i.e. UK Wide & Scotland) selected I received this information; CompanyName: Test Company Website: Test www. Email: Test@ Description: Testing Regions: UKWide (It only ever show's one of the checkbox values, not all the selected ones) Thanks for the help and time so far guys. Really appreciated.
March 16, 201214 yr $Regions = Trim(stripslashes($_POST['Regions'])); You cannot do this as you need to loop through the results.
March 16, 201214 yr Author $Regions = Trim(stripslashes($_POST['Regions'])); You cannot do this as you need to loop through the results. Ooops. I have actually removed that and it's still the same. I get no results from the region at all now using the code that Simple-Host Nathan suggested. Will have no hair left by the time I get this sorted, if I do!
March 16, 201214 yr Ooops. I have actually removed that and it's still the same. I get no results from the region at all now using the code that Simple-Host Nathan suggested. Will have no hair left by the time I get this sorted, if I do! OK lets see what is actually in the BODY Try this and see what the result is: <?php $EmailFrom = "info@uksecurity-directory.co.uk"; $EmailTo = "info@uksecurity-directory.co.uk"; $Subject = "Contact Form"; $CompanyName = Trim(stripslashes($_POST['CompanyName'])); $Email = Trim(stripslashes($_POST['Email'])); $Website = Trim(stripslashes($_POST['Website'])); $Description = Trim(stripslashes($_POST['Description'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } $Body = ""; $Body .= "CompanyName: "; $Body .= $CompanyName; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Description: "; $Body .= $Description; $Body .= "\n"; $Body .= "Regions: "; foreach ($_POST['Regions'] as $Regions){ $Body .= $Regions . "\n"; } echo $Body; ?>
March 16, 201214 yr Just realised what you are doing. You need to put the name into the array not the id: <ol> <li><label class="checkbox">UK Wide</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="UKWide" /></li> <li><label class="checkbox">Scotland</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Scotland" /></li> <li><label class="checkbox">North East</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="NorthEast" /></li> <li><label class="checkbox">North West</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="NorthWest" /></li> </ol> <ol> <li><label class="checkbox">Yorkshire</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Yorkshire" /></li> <li><label class="checkbox">West Midlands</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="WestMidlands" /></li> <li><label class="checkbox">Wales</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Wales" /></li> <li><label class="checkbox">East Midlands</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="EastMidlands" /></li> </ol> <ol>
March 16, 201214 yr And the same with services <label class="checkbox">UK Wide</label> <ol> <li><label class="checkbox">UK Wide</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="UKWide" /></li> <li><label class="checkbox">Scotland</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Scotland" /></li> <li><label class="checkbox">North East</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="NorthEast" /></li> <li><label class="checkbox">North West</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="NorthWest" /></li> </ol> <ol> <li><label class="checkbox">Yorkshire</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Yorkshire" /></li> <li><label class="checkbox">West Midlands</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="WestMidlands" /></li> <li><label class="checkbox">Wales</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="Wales" /></li> <li><label class="checkbox">East Midlands</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="EastMidlands" /></li> </ol> <ol> <li><label class="checkbox">East Anglia</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="EastAnglia" /></li> <li><label class="checkbox">London</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="London" /></li> <li><label class="checkbox">South West</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="SouthWest" /></li> <li><label class="checkbox">South East</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="SouthEast" /></li> </ol> <ol> <li><label class="checkbox">Northern Ireland</label> <input name="Regions[]" type="checkbox" class="checkbox" id="Regions" value="NorthernIreland" /></li><br /> </ol> <p class="Region">Please select the security services you provide</p><br /> <ol> <li><label class="checkbox">Keyholding</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="Keyholding" /></li> <li><label class="checkbox">CCTV</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="CCTV" /></li> <li><label class="checkbox">Close Protection</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="CloseProtection" /></li> <li><label class="checkbox">Door Supervisors</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="DoorSupervision" /></li> <li><label class="checkbox">Residential Security</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="ResidentialSecurity" /></li> </ol> <ol> <li><label class="checkbox">Security Dogs</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="SecurityDogs" /></li> <li><label class="checkbox">Security Guards</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="SecurityGuards" /></li> <li><label class="checkbox">Security Kit & Equipment</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="SecurityKit" /></li> <li><label class="checkbox">Surveillance & Investigations</label> <input name="Services[]" type="checkbox" class="checkbox" id="Services" value="Surveillance" /></li> <li><label class="checkbox">Training Companies</label> <input name="Services[]" class="checkbox" type="checkbox" id="Services" value="Training" /></li> </ol> Add this to PHP code $Body = ""; $Body .= "CompanyName: "; $Body .= $CompanyName; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Description: "; $Body .= $Description; $Body .= "\n"; $Body .= "Regions: "; foreach ($_POST['Regions'] as $Regions){ $Body .= $Regions . "\n"; } $Body .= "Services: "; foreach ($_POST['Services'] as $Services){ $Body .= $Services . "\n"; } Now you should be good to go
March 16, 201214 yr Author GalaxyTramp, THANK YOU!!!! Works perfectly, and such a simple mistake, doh!! Again, thanks to all who have given time to help. Really appreciate it.
Create an account or sign in to comment