duanli8577 2015-11-27 11:05
浏览 30
已采纳

在我的联系表格中使用ajax [复制]

This question already has an answer here:

screenshot of error

enter image description here

Hey i'm trying to build a contact form for my contact page. I can't seem to get the form to send me an email of the inputs from the form when the "Submit Form" button is pressed.

...This is my html

<form class="send-with-ajax" method="post" action="send_form_email.php">

            <div class="row">
                <div class="three columns alpha">
                    <label for="first_name">First Name    <span>required</span></label>                
                    <label for="last_name">Last Name <span>required</span></label>
                </div>
                <div class="seven columns omega">
                    <input type="text" required placeholder="First Name" id="first_name" name="name">
                    <input type="text" required placeholder="Last Name" id="last_name" name="name">
                </div>
            </div>

            <div class="row">
                <div class="three columns alpha">
                    <label for="email">Your Email <span>email required</span></label>
                </div>
                <div class="seven columns omega">   
                    <input type="email" required placeholder="your@email" id="email" name="email">
                </div>
            </div>

            <div class="row">           
                <div class="three columns alpha">
                    <label for="comments">Message</label>
                </div>
                <div class="seven columns omega">   
                    <textarea placeholder="Enter you message, and please include your artist name. you can put either or choose between Pervis or Duijuan" id="comments" name="comments"></textarea>
                </div>
            </div>

            <div class="seven columns offset-by-three">
                <button type="submit">Submit Form</button>
                <div class="ajax-response"></div>
            </div>
</form>

...This is my javascript

$("form.send_form_email").submit(function(e) { 
    e.preventDefault();
    var form = $(this),
    validationErrors = false;

$('.form-error-msg').remove();

form.find("input[type='email'], input[required]").each(function(index){
        var inputValue = $(this).val(),
            inputRequired = $(this).attr('required'),
            inputType = $(this).attr('type');

$(this).removeClass('form-error');
        if(inputRequired && inputValue == '') {

$(this).after('<div class="form-error-msg">Please fill out this field.</div>');

$(this).addClass('form-error');
            validationErrors = true;
        } else if(inputType == 'email' && !isValidEmail(inputValue)) {

$(this).after('<div class="form-error-msg">Please enter an email address.</div>');
            $(this).addClass('form-error');
            validationErrors = true;
        }
    });

if (!validationErrors) {
        form.find('button:submit, input:submit').html('Sending...');
        form.find('.ajax-response').empty();
        $.post(form.attr('action'), form.serialize(),
            function(data) {
                if (data.bFormSent) {
                    form.find('.ajax-response').empty().html(data.aResults[0]);
                    form.find('button:submit, input:submit').html('Submit Form');
                    form.find('#email, #first_name, #last_name #comments').val('');
                } else {
                    form.find('.ajax-response').empty().wrapInner('<div class="form-error-msg"></div>');
                    form.find('.ajax-response div').html(data.aErrors[0]);
                    form.find('button:submit, input:submit').html('Try Again');
                }
            }, 'json');
    }
});

...This is my PHP

<?php

if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "myemail";

$email_subject = "this subject";

function died($error) {

    // your error code can go here

    echo "We are very sorry, but there were error(s) found with the form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();

}

// validation expected data exists

if(!isset($_POST['first_name' ]) ||

    !isset($_POST['last_name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['telephone']) ||

    !isset($_POST['comments'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');       

}

$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['telephone']; // not required

$comments = $_POST['comments']; // required

 $error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp, $email_from)) {

$error_message .= 'The Email Address you entered does not appear to be   valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {

$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}

if(!preg_match($string_exp,$last_name)) {

$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}

if(strlen($comments) < 2) {

$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}

if(strlen($error_message) > 0) { 

died($error_message);
} 

$email_message = "Form details below.

";

function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."
";

$email_message .= "Last Name: ".clean_string($last_name)."
";

$email_message .= "Email: ".clean_string($email_from)."
";

$email_message .= "Telephone: ".clean_string($telephone)."
";

$email_message .= "Comments: ".clean_string($comments)."
";

// create email headers

$headers = 'From: '.$email_from."
".

'Reply-To: '.$email_from."
" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>
</div>
  • 写回答

1条回答 默认 最新

  • douying3251 2015-11-27 11:52
    关注

    Ah man! Just now I saw your screenshot. You are not executing the PHP file using a server! You need to run it using Apache / PHP Server.

    How to run php files on my computer

    You have to run a web server (e.g. Apache) and browse to your localhost, mostly likely on port 80.

    What you really ought to do is install an all-in-one package like XAMPP, it bundles Apache, MySQL PHP, and Perl (if you were so inclined) as well as a few other tools that work with Apache and MySQL - plus it's cross platform (that's what the 'X' in 'XAMPP' stands for).

    Once you install XAMPP (and there is an installer, so it shouldn't be hard) open up the control panel for XAMPP and then click the "Start" button next to Apache - note that on applications that require a database, you'll also need to start MySQL (and you'll be able to interface with it through phpMyAdmin). Once you've started Apache, you can browse to http://localhost.

    Again, regardless of whether or not you choose XAMPP (which I would recommend), you should just have to start Apache.

    Another one

    In short:

    1. Install WAMP

    2. Put this file to C:\wamp\www\ProjectName\filename.php

    3. Go to browser: http://localhost/ProjectName/filename.php

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

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥15 对于这个问题的算法代码
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题