dongtaoxue4674 2013-02-01 17:55
浏览 27
已采纳

Jquery post()从表单抛出php发送消息

I'm using jquery to validate a form and then php to send the email with all the form content.

HTML - contact.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="scripts/jquery-1.9.0.min.js" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8" src="message_validator.js" charset="utf-8"></script>
</head>
<body>
<form id="form_contacto" charset="utf-8">
  <input type="text" id="name" class="textbox" name="name" maxlength="12" />
  <input type="text" id="last_name" class="textbox" name="last_name" maxlength="12" />
  <input type="text" id="email" class="textbox" name="email" maxlength="40" />
  <input type="text" id="title" class="textbox_2" name="title" maxlength="40" />
  <textarea id="message" name="message" ></textarea>
  <input type="text" id="about" class="textbox_3" name="about" maxlength="40" />
  <input type="text" id="validation" class="textbox_4" name="validation" maxlength="40" />
  <input type="submit"  class="submit" value="Send">
</form>
</body>
</html>

JQUERY - message_validator.js

// JavaScript Document
$(document).ready(function() {
    $('.submit').click(function(){
        if($('.error_display').css("height")!="0"){
            $('.error_display').empty();
        }
        var errorlist = [];
        errorlist.length = 0;
        var email_check = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i;
        var name = $('input#name').val();
        var email = $('input#email').val();
        var message = $('textarea#message').val();
        var validation = $('input#validation').val();

        if(name == '' && email == '' && message == ''){
            errorlist.push("- ERROR ALL");
        }else{
            if (name == '') {
                errorlist.push("-ERROR");
            }else if(name.length <= 2){
                errorlist.push("-ERROR");
            }
            if (email == ''){
                errorlist.push("-ERROR");
            }else if(!email_check.test(email)){
                errorlist.push("-ERROR");
            }
            if (message == '') {
                errorlist.push("-ERROR");
            }else if(message.length <= 5){
                errorlist.push("-ERROR");
            }
            if (validation == '') {
                errorlist.push("-ERROR");
            }else if(validation != 8){
                errorlist.push("-ERROR");
            }
        }

         if(errorlist.length >= 1){
            $('.error_display').animate({'height':errorlist.length*22}, {queue:false, duration:500});
            for(var i = 0; i < errorlist.length; i++) {
                $('.error_display').append(errorlist[i]+"<br/>");
                //errorlist[i].text.appendTo($('.error_display'));
                //$('.error_display').append(errorlist[i].val());
            } 
         }else{
            $("form#form_contacto").submit();
            $('.error_display').animate({'height':0}, {queue:false, duration:500});
            $('.success_display').animate({'height':75}, {queue:false, duration:500});
            $('#form_contacto').animate({'opacity':0.25}, {queue:false, duration:500});
            $('.submit').attr("disabled", true);
         }
    });
});

PHP - message_handler.php

<?php
  $name = $_POST['name'];
  $last_name = $_POST['last_name'];
  $visitor_email = $_POST['email'];
  $title = $_POST['title'];
  $message = $_POST['message'];
  $about = $_POST['about'];
  $validation = $_POST['validation'];


  $email_from = 'example@example.com';
      $email_subject = "New Message";
      $email_body = "Received a new message from $name $last_name"."Visitor email: $visitor_email


"."Title: $title
"."Message sent:
 $message

";


      $to = "example@example.com";

      $headers = "From: $email_from 
";

      $headers .= "Reply-To: $visitor_email 
";

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

The message is sent normally if I change the button to submit and put method="post" action="message_handler.php" in the form tag. But when I use that method it goes to the page message_handler.php and display a blank page and I want it only to send if the validation comes true from the message_validator.js so thats why I'm trying to use jquery to use the post() method.

I already tried in the last else the following:

else{
$("form#form_contacto").submit();

}

and

else{
$.post("message_handler.php", {name:name, email:email, last_name:last_name, title:title, message:message}

}
  • 写回答

1条回答 默认 最新

  • doutenglou6588 2013-02-01 18:34
    关注

    message_handler.php

    WARNING: You should protect this page from spam. In its current state, you are extremely vulnerable

    change your call to the mail function to this

      if(mail($to,$email_subject,$email_body,$headers)) {
        echo "success";
      }
      else {
        echo "fail";
      }
    

    message_validator.js

    Add this to your else block

    $.post(
        "message_handler.php",
        {
            name:name,
            email:email,
            last_name:last_name,
            title:title,
            message:message
        },
        function(result){
            // log to console for debug 
            console.log(result);
            if(result == 'success') {
                // do something here
            }
            else {
                // do something on failure
            }
    
    });
    

    contact.php

    Change the type of the submit to button to prevent form submission

    <input type="button" class="submit" value="Send">

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错