将表单数据从php传递到ajax

时间:2017-09-01 03:30:59

标签: javascript php jquery ajax

我试图将表单数据传递给php并将其传递给ajax,以便为我的发送提供真实的加载。

form.html

<form method="POST" name="myemailform" action="form-to-email.php">
                <div class="row">
                    <div class="col-md-6">
                        <h3 class="section-title">Our Address</h3>
                        <ul class="contact-info">
                            <li><i class="icon-location-pin"></i><span style="color:#fdfdfd;">Brisbane QLD, Australia</span></li>
                            <li><i class="icon-phone2"></i><span style="color:#fdfdfd;">Call Us at </span><a href="tel:0431397033"><span style="color:#FFA500;"><strong>0431397033</strong></span></a><span style="color:#fdfdfd;"> or </span><a href="tel:0405254333"><span style="color:#FFA500;"><strong>0405254333</strong></span></a></li>
                            <li><i class="icon-mail"></i><span style="color:#fdfdfd;">Email Us Here </span><a href="mailto:jennifergill1980@gmail.com"><span style="color:#FFA500;"><strong>Click Here</strong></span></a></li>
                        </ul>
                    </div>
                    <div class="col-md-6">
                        <div class="row" style="padding-top: 20px; background-color: rgba(0, 0, 0, 0.5);">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="text" name="name" id="name" class="form-control" placeholder="Name">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="text" name="emailaddress" id="emailaddress" class="form-control" placeholder="Email">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input type="text" name="subject" id="subject" class="form-control" placeholder="Subject">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <textarea name="comment" id="comment" class="form-control" id="" cols="30" rows="7" placeholder="Message"></textarea>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input type="submit" id="submit" name="submit" value="Send Message" class="btn btn-primary btn-lg">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

这是我发送电子邮件功能的php

send.php

<?php
    $errors = '';
    $myEmail = "paul.aliassistance@gmail.com";
    // "ashbeautyhealthyclinic@gmail.com";

    $name = $_POST['name'];
    $emailaddress = $_POST['emailaddress'];
    $subject = $_POST['subject'];
    $comment = $_POST['comment'];

    // validate first

    if(empty($emailaddress) || empty($name)){
        echo "name and email are mandatory";
        exit;
    }

    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
    $emailaddress)){
        $errors .="\n Error: Invalid Email Address";
    }

    $to = $myEmail;
    $email_subject =  $subject ;

    $email_body = "You have received a new message.\n" .
    "Here are the details: \n Name: $name \n " .
    "Email: $emailaddress\n Message: \n $comment";


    $headers[] ="MIME-Version: 1.0";
    $headers[] ="Content-type:text/html; charset=ISO-8859-1";
    $headers[] ="Content-Transfer-Encoding: base64";

    $headers = "From: ". $emailaddress. "\n";
    $headers .="Reply-To: jennifergill1980@gmail.com";

    $result = mail($to,$email_subject,$email_body,$headers);

     if($result){
        echo "check";
    } else {
        echo "wrong";
    }
    ?>

和我的html按钮,我的html按钮有一个函数phpsendmail

function phpsendmail(){
$('#message').html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>');
var name = $('#name').val();
var email = $('#emailaddress').val();
var subject = $('#subject').val();
var comment = $('#comment').val();

var datum = {
    name: name,
    email:email,
    subject:subject,
    comment:comment
}

$.ajax({  
    type: 'GET',
    url: 'form-to-email.php', 
    data: datum,
    success: function(response) {
        if (response=='check') {
            $('#message').html("<i class='fa fa-check' aria-hidden='true'></i>");
        }else{
            $('#message').html("<i class='fa fa-times' aria-hidden='true'></i>");
        }
    }
});
} 

4 个答案:

答案 0 :(得分:1)

您只需在代码/文件中进行4次更改 -

1 - 从表单中删除操作并添加onsubmit,即onsubmit =“return phpsendmail();”

2 - 根据Max的建议将ajax类型更改为POST也将你的ajax网址更正为send.php

3 - 确保您已在form.html中包含jQuery文件

将$ emailaddress = $ _POST ['emailaddress']替换为send.php中的$ emailaddress = $ _POST ['email']

答案 1 :(得分:0)

在你的ajax中你使用&#34; get&#34;发送数据。

但是在你的PHP中你使用&#34; post&#34;获取数据:

$.ajax({  
    type: 'post',
    url: 'form-to-email.php', 
    data: datum,
    success: function(response) {
        if (response=='check') {
            $('#message').html("<i class='fa fa-check' aria-hidden='true'></i>");
        }else{
            $('#message').html("<i class='fa fa-times' aria-hidden='true'></i>");
        }
    }
});

所以改变它,我认为它会起作用。

答案 2 :(得分:0)

只是一个建议,我不太了解AJAx但是如果你想使用纯PHP,那么请在你的行动页面中尝试这个:

<?php if(isset($_POST['nameofbutton'])){
         echo $_POST['nameoffields'];
         .
         .
         .
}?>

答案 3 :(得分:0)

你可以通过在表单中​​使用id来简化js并使用下面的js代码:

dealloc

});