电子邮件提交表单错误

时间:2015-03-19 00:20:21

标签: javascript php html ajax forms

我的页面上有一个用于提交电子邮件的表单,但我无法找到导致其退回不成功提交内容的原因。以下是有关表格的代码部分。我绕过了js,直接使用了php脚本和发送的邮件,所以这似乎不是问题所在。有任何想法吗?

JS:

    $('#button-send').click(function(event){
        $('#button-send').html('Sending E-Mail...');
        event.preventDefault();

        $('html, body').scrollTo( $('#contact'), 'fast' );
        $.ajax({
            type: 'POST',
            url: 'send_form_email.php',
            data: $('#contact_form').serialize(),
            success: function(html) {
                if(html.success == '1')
                {
                    $('#button-send').html('Send E-Mail');
                    $('#success').show();
                }
                else
                {
                    $('#button-send').html('Send E-Mail');
                    $('#error').show();
                }                   
            },
            error: function(){
                $('#button-send').html('Send E-Mail');
                $('#error').show();
            }
        });

    });


});


function valemail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

这是PHP代码:

<?php 
// EDIT THE 2 LINES BELOW AS REQUIRED
$send_email_to = "me@myemail";
$email_subject = "New Message";
function send_email($name,$email,$email_message)
{
  global $send_email_to;
  global $email_subject;
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$email. "\r\n";
  $message = "<strong>Email = </strong>".$email."<br>";
  $message .= "<strong>Name = </strong>".$name."<br>";  
  $message .= "<strong>Message = </strong>".$email_message."<br>";
  @mail($send_email_to, $email_subject, $message,$headers);
  return true;
}

function validate($name,$email,$message)
{
  $return_array = array();
  $return_array['success'] = '1';
  $return_array['name_msg'] = '';
  $return_array['email_msg'] = '';
  $return_array['message_msg'] = '';
  if($email == '')
  {
    $return_array['success'] = '0';
    $return_array['email_msg'] = 'email is required';
  }
  else
  {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
      $return_array['success'] = '0';
      $return_array['email_msg'] = 'enter valid email.';  
    }
  }
  if($name == '')
  {
    $return_array['success'] = '0';
    $return_array['name_msg'] = 'name is required';
  }
  else
  {
    $string_exp = "/^[A-Za-z .'-]+$/";
    if (!preg_match($string_exp, $name)) {
      $return_array['success'] = '0';
      $return_array['name_msg'] = 'enter valid name.';
    }
  }

  if($message == '')
  {
    $return_array['success'] = '0';
    $return_array['message_msg'] = 'message is required';
  }
  else
  {
    if (strlen($message) < 2) {
      $return_array['success'] = '0';
      $return_array['message_msg'] = 'enter valid message.';
    }
  }
  return $return_array;
}

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];


$return_array = validate($name,$email,$message);

if($return_array['success'] == '1')
{
    send_email($name,$email,$message);
}
header('Content-type: text/json')
echo json_encode($return_array);
die();
?>

和我页面上的HTML:

        <form method="post" action="send_form_email.php" id="contactform">
            <div>
                <label for="name">Enter your name</label>
                <input type="text" class="input-field" id="name" name="name" value="">
            </div>
            <div>
                <label for="email">Enter your e-mail</label>
                <input type="text" class="input-field" id="email" name="email" value="">
            </div>
            <div>
                <label>Type Your Message</label>
                <textarea id="message" name="message"></textarea>
            </div>
            <a id="button-send" href="#" title="Send Email" class="button" style="width:100%;">Send E-Mail</a>
            <div id="success">Your message has been successfully!</div>
            <div id="error">Unable to send your message, please try later.</div>
        </form>

2 个答案:

答案 0 :(得分:0)

像这个标题一样放置分号(&#39;内容类型:text / json&#39;);

答案 1 :(得分:0)

我刚刚花了一些时间对这段代码进行故障排除,这是我之前从eGrappler下载的HTML5主题,所以为了节省时间,就像我那样偶然发现这篇文章,示例代码似乎还有另一个错误,可能无法正常工作。

在.js中声明

  

&#34;数据:$(&#39; #contact_form&#39;)。serialize(),&#34;

然而,HTML中表单的ID是&#34; contactform&#34;没有下划线

  

ID =&#34;联系形式&#34;&GT;&#34;

这些需要匹配,否则帖子不会包含姓名,电子邮件地址或消息,而且电子邮件也无法发送。

希望这可以节省其他人的调试工作..最后到达那里,但这是一个学习曲线!