0

I have added a training application form in my website. Using Modal box. This is the code

<button type="submit" onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Apply Now</button>

<div id="id01" class="modal">
  <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
  <form method="POST" enctype="multipart/form-data" class="modal-content" action="sendemailcontact.php">
    <div class="container">
      <h1>Application Form</h1>
      <p>Please fill in this form to apply for the training.</p>
      <hr>
       <label for="name"><b>Name</b></label><br>
      <input type="text" placeholder="Enter name" name="name" id="name" required><br>
      <label for="email"><b>Email</b></label><br>
      <input type="text" placeholder="Enter Email" name="email" id="email" required><br>
      <label for="phone"><b>Phone number</b></label><br>
      <input type="text" placeholder="Enter phone number" name="phone" id="phone" required><br>
      
      <label for="location"><b>Location</b></label><br>
      <input type="text" placeholder="Enter location" name="location" id="location" required><br>
       <label for="college"><b>College</b></label><br>
      <input type="text" placeholder="Enter college" name="college" id="college" required><br>
<!--<label for="uploaded_file"><b>Upload resume</b></label>
      <input class="upl" type="file" name="uploaded_file" id="uploaded_file"  required>-->
      <div class="clearfix">
        
        <input style="width:30%;" type="submit" name="apply" id="apply" class="signupbtn" value="Apply">
      </div>
    </div>
  </form>
</div>

This form to my php code that send mail to my email address which the details received from each applicant from the above form. This is my php code

<?php 

//error_reporting(-1);
//ini_set('display_errors', 'On');
//set_error_handler("var_dump");


$errors = '';
$myemail = 'example@gmail.com';
if(empty($_POST['name'])  || 
   empty($_POST['email']) ||
   empty($_POST['phone']) ||
   empty($_POST['location']) ||
   empty($_POST['college']))
{
    $errors .= "\n Error: all fields are required";
}


$email_address = $_POST['email']; 
$name = $_POST['name']; 
$phno = $_POST['phone']; 
$location = $_POST['location']; 
$college = $_POST['college']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Career Oriented Training Form submission: $name";
    $email_body = "You have received a new applicant. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Phone: $phno  \n Location: $location \n College: $college"; 

    $headers = "From: $email_address\n"; 
    $headers .= "Reply-To: $myemail";

    mail($to,$email_subject,$email_body,$headers);

    header('Location:index.html');
} 
?>

When i click submit, its going to a blank page. Also i cannot receive any mail. Please help me

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
chinu
  • 13
  • 3
  • Your page is black because, when an email is sent, you don't output anything. (I tried to see if there was a syntax error, which can also cause a blank page, but I didn't see one). – KIKO Software Sep 29 '18 at 07:31
  • but i am not receiving any mail also.. – chinu Sep 29 '18 at 07:32
  • Where are you sending the mail to? Don't use GMail or Hotmail, as they will almost certainly reject your email. – KIKO Software Sep 29 '18 at 07:34
  • `var_dump(mail($to,$email_subject,$email_body,$headers))` to see what it returns. Even if it returns `true`, it still doesn't mean that your client successfully received it. See [here](http://php.net/manual/en/function.mail.php). See [possible options](https://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) from answers other than the accepted one. – nice_dev Sep 29 '18 at 07:53

1 Answers1

0

You need to have it echo back $errors as that will tell you why it's failing. You're stocking $errors and ignoring it other than checking that it's empty.

Also do not use regex to validate email addresses. See http://php.net/manual/en/filter.filters.validate.php specifically FILTER_VALIDATE_EMAIL

trollboy
  • 133
  • 7