Pett Posted November 2, 2016 Share Posted November 2, 2016 Hey all... Am working on a PHP form which has got radio buttons, checkbox and text-area fields. When I hit the submit button, I get all the values in my email EXCEPT the checkbox values. Kindly help me rectify the error? PHP code <?php //INITIALIZE VARIABLES $name = $phone = $email = $value = $package = ""; $nameError = $phoneError = $emailError = $checkboxError = $packageError = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $valid = true; //checking name and validate using regexp if (empty($_POST["name"])) { $valid = false; $nameError = "Fill out this field"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ,.-]*$/", $name)) { $valid = false; $nameError = "*Please insert Letters and white-spaces only*"; } } //check phone and validate if (empty($_POST["phone"])) { $valid = false; $phoneError = "Please fill out this field"; } else { $phone = test_input($_POST["phone"]); if (!preg_match("/^[0-9 ,+.-]*$/", $phone)) { $valid = false; $phoneError = "*Only numbers are allowed*"; } } //check validity of email if (empty($_POST["email"])) { $valid = false; $emailError = "Please fill out this field"; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $valid = false; $emailError = "*Invalid email address*"; } } // Check the values from checkbox and POST them to email if(!empty($_POST["checkbox"])) //check if the user has selected a CHECKBOX or NOT { $checkbox =$_POST["checkbox"]; //Array of values from the checkbox foreach($checkbox as $value) //loop to store and display values of individual checkboxes {echo $value;}} //Display selected checkbox else {test_input($_POST["value"]);} //POST values to email //check package & validate if (empty($_POST["package"])) { $packageError = "Select one of these options"; } else { $package = test_input($_POST["package"]);} require ("PHPMailer/PHPMailerAutoload.php"); //Instantiate the Phpmailer class $mail = new PHPMailer(); $mail->IsSMTP(); // mailer to use SMTP $mail->SMTPDebug = 2; $mail->Host = "smtp.gmail.com"; // main and backup server $mail->SMTPSecure = "tls"; // TLS connection $mail->Port = 587; //Gmail SMTP port $mail->SMTPAuth = true; // turn on SMTP authorization $mail->Username = "gmail_address"; //username $mail->Password = "****************"; // password $mail->From = "$email"; //sender's email $mail->FromName = "$name"; //name $mail->AddAddress("gmail_address", "Pettans"); //email address of recepient and name $mail->AddReplyTo($_POST["email"], $_POST["name"]); //Address to which recepient will reply $mail->Subject = "Graphics Design Form"; //subject of email $mail->WordWrap = 120; //word wrap to 100 characters $mail->IsHTML(true); //email format to HTML $mail->Body = "Name: " . $_POST["name"] . "<br>Phone: " . $_POST["phone"] . "<br>Email: " . $_POST["email"] . "<br>Checkbox: " . $_POST["value"] . "<br>Package: " . $_POST["$package"]; if ($valid) { if (!$mail->Send()) { echo 'Form could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; } else { header('Location:thanks.html'); } } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> HTML code <!DOCTYPE html> <html lang= "en-US"> <head> <meta name= "robots" content= "noindex, nofollow"> <meta charset="utf-8"> <meta name= "author" content= "Pettans Designers"> <meta name= "viewport" content= "width=device=width, initial scale= 1.0"> <link href= "style.css" rel= "stylesheet" type= "text/css"> <title> Graphics Design Form </title> </head> <body> <image src= "images/logo.gif" border= "0" width= "240px" height="160px"> <br> <div class="form2"> <form method= "post" action= "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> <font color="white"> <h4>Please fill the form below to kick start your project. <br> <br> <font size="4.5px" color="red"> *required field </font> </h4> <br> Your name? <input type= "text" class= "input" name= "name" placeholder= "e.g Pettans Designers" value="<?php echo $name; ?>" required> <span class= "error"> * <br> <?php echo $nameError; ?> </span> <br> <br> Your phone number? <input type= "text" class= "input" name= "phone" value="<?php echo $phone; ?>" placeholder= "e.g 0722222222" required> <span class= "error"> * <br> <?php echo $phoneError; ?> </span> <br> <br> Your email address? <input type= "email" class= "input" name= "email" placeholder= "will not be published" value="<?php echo $email; ?>" required> <span class= "error"> * <br> <?php echo $emailError; ?> </span> <br> <br> What project would you like for your institution? <span class= "error"> * </span> <br> <br> <input type="checkbox" class="input" name="checkbox[]" value="Graphics Design"> Graphics Design (Book, Posters, Flyers, Banners, Magazines, T-Shirts) <br> <input type="checkbox" class="input" name="checkbox[]" value="Logo Design" > Professional logo Design <br> <input type="checkbox" class="input" name="checkbox[]" value="Branding" > Branding <br> <br> How did you know about our institution? <span class= "error"> * </span> <br> <br> <input type ="radio" class= "input" name="package" <?php if (isset($package) && $package == "mouth") echo "checked";?> value= "mouth" required> Word of Mouth <br> <input type ="radio" class= "input" name="package" <?php if (isset($package) && $package == "reputation") echo "checked";?> value= "website"> Reputation <br> <input type ="radio" class= "input" name="package" <?php if (isset($package) && $package == "newspaper") echo "checked";?> value= "newspaper"> Newspaper <br> <br> <button class="submit" type= "reset" value= "Reset"> Reset Form </button> <button class="submit" type= "submit" value= "Submit"> Submit Form </button> </font> </form> </div> </body> </html> Link to comment Share on other sites More sharing options...
dsonesuk Posted November 2, 2016 Share Posted November 2, 2016 Because you are reproducing exactly the same mistake from this topic http://w3schools.invisionzone.com/index.php?showtopic=56010&page=2 Link to comment Share on other sites More sharing options...
Pett Posted November 2, 2016 Author Share Posted November 2, 2016 Yah, I referred to the query since the member had a similar problem as mine. Am an online learner of PHP in W3Schools website and envatotuts and I refer to the tuts & queries when stack. Could you help me figure the problem out? Link to comment Share on other sites More sharing options...
Ingolme Posted November 2, 2016 Share Posted November 2, 2016 You're not adding the checkbox values to the message body. Instead of echo $value you should append $value to the message body. You have to properly indent your code. It's unreadable. Here are the rules: "Level of indentation" refers to either one tab or a fixed number of spaces. Multply the level of indentation by the number of spaces or tabs you have chosen as your indentation width and put it preceding the line of code. The first line of your code must have no indentation, level zero. Whenever you have an opening curly brace "{" you add one level of indentation on the lines that follow Whenever you have a closing curly brace "}" you remove one level of indentation on that line and all the lines that follow Closing curly braces "}" must go on their own line. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 2, 2016 Share Posted November 2, 2016 You would need to loop through each $_POST['checkbox'] value, send it to be validated/sanitized, then add each to a passed validation array, then use implode to show all values in array to message body of email. Link to comment Share on other sites More sharing options...
Pett Posted November 3, 2016 Author Share Posted November 3, 2016 Much appreciations for your help, I have taken into consideration your advice. But I need a helping hand concerning appending $value to the message body: PHP // Check the values from checkbox and POST them to email If (isset($_POST["checkbox"]) && !empty($_POST["checkbox"])) { $checkbox =$_POST["checkbox"]; //Array of values from the checkbox values $value= implode(' , ', $checkbox); //implode values and separate by a comma echo $value; } else { test_input($_POST["value"]); //POST values to email } Message body for the email $mail->Body = "Name: " . $_POST["name"] . "<br>Phone: " . $_POST["phone"] . "<br>Email: " . $_POST["email"] . "<br>Checkbox: " . $_POST["value"] . "<br>Package: " . $_POST["$package"]; HTML Form <br> <br> What project would you like for your institution? <span class= "error"> * </span> <br> <br> <input type="checkbox" class="input" name="checkbox[]" value="Graphics Design"> Graphics Design (Book, Posters, Flyers, Banners, Magazines, T-Shirts) <br> <input type="checkbox" class="input" name="checkbox[]" value="Logo Design" > Professional logo Design <br> <input type="checkbox" class="input" name="checkbox" value="Branding" > Branding Link to comment Share on other sites More sharing options...
dsonesuk Posted November 3, 2016 Share Posted November 3, 2016 Like I explained to you and the topic you borrowed this from, you first need to loop through each checkbox value first to validate and santize, before inserting in email message body. Link to comment Share on other sites More sharing options...
Pett Posted November 3, 2016 Author Share Posted November 3, 2016 Here's what I've got: // Check the values from checkbox and POST them to email If (isset($_POST["checkbox"]) && !empty($_POST["checkbox"])) { $checkbox = array("Graphics Design", "Logo Design","Branding"); //loop through each checkbox value $value= implode(' , ', $checkbox); //implode values and separate by a comma } else { test_input($_POST["value"]); //POST values to email } Would you mind explaining using an example? Link to comment Share on other sites More sharing options...
dsonesuk Posted November 3, 2016 Share Posted November 3, 2016 You are not looping, you are not checking values from $_POST["checkbox"] but! for some reason I do not know why? have decided to check a array of ALL checkbox values? looping http://www.w3schools.com/php/php_looping_for.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now