Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有JS验证的PHP联系人表单不发送电子邮件_Javascript_Php_Forms_Email - Fatal编程技术网

Javascript 带有JS验证的PHP联系人表单不发送电子邮件

Javascript 带有JS验证的PHP联系人表单不发送电子邮件,javascript,php,forms,email,Javascript,Php,Forms,Email,我在这里做错了什么联系人表单没有发送和电子邮件我收到一条错误消息“对不起,错误发生在这次发送您的消息。”当我填写表单并单击发送 index.php 您的email.php在请求全局中有特定的电子邮件地址,因此它会查找您的电子邮件地址的已发布字段。如果您想发送到特定的电子邮件地址,只需在字符串中列出,如下所示: <?php $subject = $_POST['subject'] . ' Ajax HTML Contact Form : Demo'; $to = 'omar@cinqomed

我在这里做错了什么联系人表单没有发送和电子邮件我收到一条错误消息“对不起,错误发生在这次发送您的消息。”当我填写表单并单击发送

index.php


您的email.php在请求全局中有特定的电子邮件地址,因此它会查找您的电子邮件地址的已发布字段。如果您想发送到特定的电子邮件地址,只需在字符串中列出,如下所示:

<?php
$subject = $_POST['subject'] . ' Ajax HTML Contact Form : Demo';
$to = 'omar@cinqomedia.com'; //Recipient's E-mail

$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "From: " . $_POST['email'] . "rn"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";

$message = 'Name: ' . $_POST['name'] . "<br>";
$message .= $_POST['message'];

if (@mail($to, $subject, $message, $headers))
{
    // Transfer the value 'sent' to ajax function for showing success message.
    echo 'sent';
}
else
{
    // Transfer the value 'failed' to ajax function for showing error message.
    echo 'failed';
}

另外,在开始使用$message变量之前,它还没有定义,所以在第一次使用它时不需要“.=”而只需要一个“=”


您可能想考虑向发布的数据添加一些过滤器。

您是什么环境?using@anubhav简单php您可以检查实时版本,假设您的JS是正确的,您的php脚本返回
失败
。我猜当你输入自己的电子邮件时,你的收件箱里什么都没有?也许您应该尝试手动执行它并检查输出或日志,以了解
mail
语句失败的原因。是否有错误?检查您的邮件头?@anubhav这是我收到的错误“抱歉,这次发送邮件时出错了。”不起作用,它仍然会显示“抱歉,这次发送邮件时出错了”。当我单击“发送”时,您是否正在使用本地机?如果是这样,您的计算机可能无法发送电子邮件,您需要设置类似于hMailServer的内容,这里有一个教程:(或者在您的服务器上测试它)
$subject = $_REQUEST['subject'] . ' Ajax HTML Contact Form : Demo';
$to = $_REQUEST['omar@cinqomedia.com']; //Recipient's E-mail

$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "From: " . $_REQUEST['email'] . "rn"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";

$message .= 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= $_REQUEST['message'];

if (@mail($to, $subject, $message, $headers))
{
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
}
else
{
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
 $(document).ready(function(){
    $('#send_message').click(function(e){

        //Stop form submission & check the validation
        e.preventDefault();

        // Variable declaration
        var error = false;
        var name = $('#name').val();
        var email = $('#email').val();
        var subject = $('#subject').val();
        var message = $('#message').val();

        // Form field validation
        if(name.length == 0){
            var error = true;
            $('#name_error').fadeIn(500);
        }else{
            $('#name_error').fadeOut(500);
        }
        if(email.length == 0 || email.indexOf('@') == '-1'){
            var error = true;
            $('#email_error').fadeIn(500);
        }else{
            $('#email_error').fadeOut(500);
        }
        if(subject.length == 0){
            var error = true;
            $('#subject_error').fadeIn(500);
        }else{
            $('#subject_error').fadeOut(500);
        }
        if(message.length == 0){
            var error = true;
            $('#message_error').fadeIn(500);
        }else{
            $('#message_error').fadeOut(500);
        }

        // If there is no validation error, next to process the mail function
        if(error == false){
           // Disable submit button just after the form processed 1st time successfully.
            $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });

            /* Post Ajax function of jQuery to get all the data from the submission of the form as soon as the form sends the values to email.php*/
            $.post("email.php", $("#contact_form").serialize(),function(result){
                //Check the result set from email.php file.
                if(result == 'sent'){
                    //If the email is sent successfully, remove the submit button
                     $('#submit').remove();
                    //Display the success message
                    $('#mail_success').fadeIn(500);
                }else{
                    //Display the error message
                    $('#mail_fail').fadeIn(500);
                    // Enable the submit button again
                    $('#send_message').removeAttr('disabled').attr('value', 'Send');
                }
            });
        }
    });    
});
<?php
$subject = $_POST['subject'] . ' Ajax HTML Contact Form : Demo';
$to = 'omar@cinqomedia.com'; //Recipient's E-mail

$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "From: " . $_POST['email'] . "rn"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";

$message = 'Name: ' . $_POST['name'] . "<br>";
$message .= $_POST['message'];

if (@mail($to, $subject, $message, $headers))
{
    // Transfer the value 'sent' to ajax function for showing success message.
    echo 'sent';
}
else
{
    // Transfer the value 'failed' to ajax function for showing error message.
    echo 'failed';
}