客户没有收到来自页面的电子邮件

时间:2018-09-06 19:34:07

标签: javascript php html forms email

我目前正在我的第一个网站上为客户工作,因此对PHP来说是全新的。该页面处于联机状态,但是电子邮件表单似乎什么也不做,只是刷新页面。我将条件文件放在php文件的末尾以回显成功或失败,但似乎无济于事。我无法指出我要去哪里。

html

   <form id="my_form" onsubmit="submitForm(); return false;">
    <p> <label>First Name</label>
    <input type="text" name="first_name" id="firstName" required> 
    </p>
    <p> <label>Last Name</label>
    <input type="text" name="last_name" id="lastName" required> </p>
    <p> <label>Phone Number</label>
    <input type="text" name="phone_number" id="phoneNumber" required> 
    </p>
    <p> <label>Email Address</label>
    <input type="email" name="email_address" id="emailAddress" 
     required> </p>
    <p class="full"> <label>Message</label>
    <textarea name="message" rows="5" id="message" required> 
    </textarea> </p>
    <p class="full">
    <button id="button" type="submit" value="Submit Form"><span 
    id="status"></span>Email</button>
    </p>
    </form>

JS

function _(id){ return document.getElementById(id); }
function submitForm(){
    _("button").disabled = true;
    _("status").innerHTML = 'please wait ...';
    var formdata = new FormData();
    formdata.append( "name", _("name").value );
  formdata.append( "lastName", _("lastName").value );
    formdata.append( "phoneNumber", _("phoneNumber").value );
  formdata.append( "emailAddress", _("emailAddress").value );
  formdata.append( "message", _("message").value );
    var ajax = new XMLHttpRequest();
    ajax.open( "POST", "index.php" );
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                _("my_form").innerHTML = '<h2>Thank you '+_("name").value +', your message has been sent.</h2>';
            } else {
                _("status").innerHTML = ajax.responseText;
                _("button").disabled = false;
            }
        }
    }
    ajax.send( formdata );
}

PHP

<?php
if( isset($_POST['name']) && isset($_POST['lastName']) && isset($_POST['phoneNumber']) && isset($_POST['emailAddress']) && isset($_POST['message']) ){
    $name = $_POST['name'];
  $lastName = $_POST['lastName'];
  $phoneNumber = $_POST['phoneNumber'];
    $emailAddress = $_POST['emailAddress'];
    $message = nl2br($_POST['message']);
    $to = "info@glaucomaspecialists.com";
    $from = $emailAddress;
    $subject = 'Schedule Inquiry';
    $message = "<b>Name:</b> '.$name.' <b>LastName:</b> '.$lastName.' <br><b>Phone:</b> '.$phoneNumber' <b>Email:</b> '.$emailAddress.' <p>'.$message.'</p>";
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    if( mail($to, $subject, $message, $headers) ){
        echo "success";
    } else {
        echo "The server failed to send the message. Please try again later.";
    }
}
?>

1 个答案:

答案 0 :(得分:1)

向后遍历代码-如果您的测试echo语句没有从if / else中写入任何内容,则永远不会执行它。这只能是因为您的陈述没有被评估为真:

isset($_POST['name']) && isset($_POST['lastName']) && isset($_POST['phoneNumber']) && isset($_POST['emailAddress']) && isset($_POST['message']) ){

因此您的AJAX根本没有发送任何数据,或者您的名称值有误。浏览代码中的HTML状态

<input type="text" name="first_name" id="firstName" required> 

您的JS说

formdata.append( "name", _("name").value );

您已不再使用firstNamename作为ID。

因此

isset($_POST['name'])

评估为false,并且您的代码永远不会运行。