Jump to content

Patwan

Members
  • Posts

    34
  • Joined

  • Last visited

Everything posted by Patwan

  1. Ok, so there isn`t any other error in the coding section apart from the PHPMailer part?
  2. Which else condition are you talking about. Could you expound further using examples in coding, so that I can understand. My aim is that the form:validates, sends an email to my Gmail account if there is correct user input & then redirect to another page called "thank_you.html". In the code above, the form validates properly using Regexp, but fails to send an email to me, then later redirect to "thank_you.html" page ? Any help please?
  3. When there is correct user input, it displays at the top of the form "name is valid, phone is valid, email is valid, business is valid" When there is incorrect input, it validates properly & displays error messages on the form and at the top of the form " name did not pass regexp " etc. But the problem still persists, it doesnt redirect to the next page or send email?
  4. Hey, I adjusted the following code as you advised me: <?php //initialize variables and set to empty values $name = $phone = $email = $business= " "; $nameErr = $phoneErr = $emailErr = $businessErr = " "; ini_set('display_errors', 1); error_reporting(E_ALL); if ($_SERVER["REQUEST_METHOD"] == "POST") {$valid = true; //check if name contains letters and white-space if (empty($_POST["name"])) {$nameErr = "Please fill out this field"; $valid =false; echo 'name is empty<br>'; } else {$name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ,.-]*$/", $name)) {$nameErr = "*Only Letters and white-spaces are allowed*"; echo 'name did not pass regexp<br>';} else{echo'name is valid<br>';}} /check if phone contains numbers if (empty($_POST["phone"])) {$phoneErr = "Please fill out this field"; $valid =false; echo'phone is empty<br>';} else {$phone = test_input ($_POST["phone"]); if (!preg_match("/^[0-9 ,+.-]*$/", $phone)) {$phoneErr = "*Only numbers are allowed*"; echo 'phone did not pass regexp<br>';} else{echo' phone is valid<br>'; }} //check if email is valid if (empty($_POST["email"])) {$emailErr = "Please fill out this field"; $valid =false; echo'email is empty<br>';} else {$email = test_input ($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {$emailErr = "*Invalid email address*"; echo 'email did not pass regexp<br>';} else{echo'email is valid<br>';}} //check if business field is valid if (empty($_POST["business"])) {$businessErr = "Please fill out this field"; $valid =false; echo 'business is empty<br>'; } else {$business = test_input($_POST["business"]); if (!preg_match("/^[a-zA-Z-.+:; ]*$/", $business)) {$businessErr= "*Only letters and spaces are allowed*"; echo 'business did not pass regexp<br>';} else {echo 'business is valid<br>';}} require ("PHPMailer_5.2.4/class.phpmailer.php"); //including phpmailer class $mail = new PHPMailer(); $mail->IsSMTP(); // set mailer to use SMTP $mail->SMTPDebug =0; $mail->Host = "smtp.gmail.com"; // specify main and backup server $mail->SMTPSecure = "ssl"; // Connect using a TLS connection $mail->Port = 465; //Gmail SMTP port $mail->SMTPAuth = true; // turn on SMTP authorization $mail->Username = "my gmail account"; // SMTP username $mail->Password = " ************** "; // SMTP password $mail->From = $email; //email of sender $mail->FromName = $name; //name of the sender $mail->AddAddress("my gmail account", "Patwan"); //email address of recepient and name $mail->AddAddress("my gmail account", "Test email"); // name is optional $mail->AddReplyTo($_POST["email"], $_POST["name"]); //Address to which recepient will reply $mail->WordWrap = 100; // set word wrap to 100 characters $mail->IsHTML(true); // set email format to HTML $mail->Subject = "Contact Form"; //subject of email $mail->Body = "Name: " .$_POST['name'] . "<br>Phone: " . $_POST['phone'] . "<br>Email: " . $_POST['email'] . "<br>Business: " . $_POST['business'] ; if($valid){ if(!$mail->Send()) { echo 'Form could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; } else {header('Location:thank_you.html');}} } function test_input($data) {$data = trim($data); $data = stripslashes ($data); $data = htmlspecialchars ($data); return $data; } ?>
  5. Thank you, let give it a try...
  6. Duly noted & thank-you for your patience ~ How do I code $valid=false part. Kindly assist on rectifying that, I have tried to recode it by following some of W3 tutorials more than five times today but nothing happens?
  7. Pardon me, I forgot to tell you that it displays errors when there is wrong user input after pressing Submit button. But if there is correct input it only displays them on the Form fields without redirecting to the next page or sending a mail in my inbox. Could there be a problem in my coding?
  8. It doesn't redirect to the next page. It only displays user input in the form & no message appears in my Gmail inbox.
  9. Hey guys, am a newbie in PHP pprogramming. I have created a Contact form on my website which validates through Regex(preg_match function) but can`t send the email to my Gmail account. I have already enabled "less secure apps" in my Gmail account settings. I am using PHPMailer class to send the messages via XAMPP sever. Kindly assist me in rectifying any error in the code below. ~ Kind regards <?php //initialize variables and set to empty values $name = $phone = $email = $business= " "; $nameErr = $phoneErr = $emailErr = $businessErr = " "; if ($_SERVER["REQUEST_METHOD"] == "POST") {$valid = true; //check if name contains letters and white-space if (empty($_POST["name"])) {$nameErr = "Please fill out this field"; $valid =false;} else {$name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ,.-]*$/", $name)) {$nameErr = "*Only Letters and white-spaces are allowed*";}} //check if phone contains numbers if (empty($_POST["phone"])) {$phoneErr = "Please fill out this field"; $valid =false;} else {$phone = test_input ($_POST["phone"]); if (!preg_match("/^[0-9 ,+.-]*$/", $phone)) {$phoneErr = "*Only numbers are allowed*";}} //check if email is valid if (empty($_POST["email"])) {$emailErr = "Please fill out this field"; $valid =false;} else {$email = test_input ($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {$emailErr = "*Invalid email address*";}} //check if business field is valid if (empty($_POST["business"])) {$businessErr = "Please fill out this field"; $valid =false;} else {$business = test_input($_POST["business"]); if (!preg_match("/^[a-zA-Z-.+:; ]*$/", $business)) {$businessErr= "*Only letters and spaces are allowed*";}} require ("PHPMailer_5.2.4/class.phpmailer.php"); //including phpmailer class $mail = new PHPMailer(); $mail->IsSMTP(); // set mailer to use SMTP $mail->SMTPDebug =0; $mail->Host = "smtp.gmail.com"; // specify main and backup server $mail->SMTPSecure = "ssl"; // Connect using a TLS connection $mail->Port = 465; //Gmail SMTP port $mail->SMTPAuth = true; // turn on SMTP authorization $mail->Username = "my gmail account"; // SMTP username $mail->Password = " ************** "; // SMTP password $mail->From = $email; //email of sender $mail->FromName = $name; //name of the sender $mail->AddAddress("my gmail account", "Patwan"); //email address of recepient and name $mail->AddAddress("my gmail account", "Test email"); // name is optional $mail->AddReplyTo($_POST["email"], $_POST["name"]); //Address to which recepient will reply $mail->WordWrap = 100; // set word wrap to 100 characters $mail->IsHTML(true); // set email format to HTML $mail->Subject = "Contact Form"; //subject of email $mail->Body = "Name: " .$_POST['name'] . "<br>Phone: " . $_POST['phone'] . "<br>Email: " . $_POST['email'] . "<br>Business: " . $_POST['business'] ; if($valid){ if(!$mail->Send()) { echo 'Form could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; } else {header('Location:thank_you.html');}} } function test_input($data) {$data = trim($data); $data = stripslashes ($data); $data = htmlspecialchars ($data); return $data; } ?> <!DOCTYPE html> <html> <html lang= "en-US"> <head> <meta name= "robots" content= "noindex, nofollow"> <meta charset="UTF-8"> <meta name= "author" content= "Kelly James"> <link href= "style.css" rel= "stylesheet" type= "text/css"> <title> Contact form </title> </head> <body> <image src= "images/logo.gif" border= "0" width= "240px" height="160px"> <br> <div class="form"> <form method= "post" action= "<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>" > <h4 id="h4form"> Please fill the form below to kick start your project. <br> <br> <font color="red"> *required field </font> </h4> <br> What is your name? <input type= "text" class= "input" name= "name" placeholder= "e.g Nelson Mandela" value="<?php echo $name;?>" required> <span class= "error"> * <br> <?php echo $nameErr;?> </span> <br> <br>What is 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 $phoneErr;?> </span> <br> <br> What is 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 $emailErr;?> </span> <br> <br> What is the name of your business or institution? <input type= "text" class= "input" name="business" placeholder= "e.g Microsoft Corporation" value= "<?php echo $business;?>" required> <span class= "error"> * <br> <?php echo $businessErr;?> </span> <br> <br> <button class="submit" type= "submit" value= "Submit"> Submit Form </button> </form> </div> </body> </html>
×
×
  • Create New...