php - 从我的网站接收来自申请表的邮件时遇到问题

标签 php html css

我在我的网站上添加了培训申请表。使用模态框。这是代码

<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>

此表格到我的 php 代码,该代码将邮件发送到我的电子邮件地址,其中从上述表格中收到了每个申请人的详细信息。这是我的 PHP 代码

<?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');
} 
?>

当我点击提交时,它会转到一个空白页面。我也收不到任何邮件。请帮助我

最佳答案

您需要让它回显 $errors,因为它会告诉您失败的原因。您存储 $errors 并忽略它,而不是检查它是否为空。

也不要使用正则表达式来验证电子邮件地址。参见 http://php.net/manual/en/filter.filters.validate.php特别是 FILTER_VALIDATE_EMAIL

关于php - 从我的网站接收来自申请表的邮件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52566187/

相关文章:

PHP 结果不显示,也没有错误消息

javascript - 滚动时的 jQuery 导航栏过渡。如何调整特定的过渡点

javascript - 在 IE 中淡入淡出父元素时,绝对定位的文本不会设置动画

android - 带有 fromHtml 的 TextView 中的 <img>

javascript - HTML5 'time' 标签未正确设置值

css - 如何在 .RPres R Presentation 中编辑标题幻灯片样式?

php - 从 php 添加一条 mysql 记录

php - max_execution_time 为 240 秒 - 超过 60 秒的超时

php - 帮助 PHP method_exists()

html - 按钮相对于内容的位置