如何从PHP检索信息并在javascript中使用它

时间:2016-10-05 14:38:04

标签: javascript php

我有兴趣从我的联系表单中检索用户的姓名和电子邮件,并使用它来显示如下内容:"感谢您的留言$ name,我们将回复以下电子邮件:$电子邮件"我想更改.done函数

这是javascript:

       $.ajax({
        type: 'POST',
        url: "sendemail.php",
        data: {
            Name: $form.find("input[name='name']").val(),
            Subject: $form.find("input[name='subject']").val(),
            Email: $form.find("input[name='email']").val(),
            message: $form.find("textarea[name=message]").val(),
        },
        beforeSend: function () {
            form.append(form_status.html('<p style="font-weight:bold;color:white;"><i class="fa fa-spinner w3-text-white w3-spin" style="font-size:50px"></i> Email is sending...</p>').fadeIn());



        }
    }).done(function (data) {
        //here is the function i want to insert the information 
        form_status.html('<p style="color:white;" class="text-success w3-animate-fading"><i class="fa fa-check fa-2x" aria-hidden ="true"></i>\n\
     Message sent. Thank you for contacting us phpName, we will reply to the following email: phpEmail <i class="fa fa-smile-o fa-2x" aria-hidden="true"></i>  </p>')
                .delay(7000).fadeOut();



    });

1 个答案:

答案 0 :(得分:2)

var name = $form.find("input[name='name']").val();
var email = $form.find("input[name='email']").val();

$.ajax({
    type: 'POST',
    url: "sendemail.php",
    data: {
        Name: name,
        Subject: $form.find("input[name='subject']").val(),
        Email: email,
        message: $form.find("textarea[name=message]").val(),
    },
    beforeSend: function() {
        form.append(form_status.html('<p style="font-weight:bold;color:white;"> < i class = "fa fa-spinner w3-text-white w3-spin" style = "font-size:50px" > < /i>Email is sending... < /p>').fadeIn());
    }
}).done(function(data) {
    //here is the function i want to retrieve the data from php
    form_status.html('<p style="color:white;" class="text-success w3 - animate - fading "><i class="fa fa - check fa - 2 x " aria-hidden ="true "></i>\n\
            Message sent. Thank you for contacting us ' + name + ', we will reply to your email shortly. <i class="fa fa - smile - o fa - 2 x " aria-hidden="true "></i></p>')
        .delay(7000).fadeOut();
});

由于您已经拥有表格中的姓名和电子邮件信息,我不明白为什么您不使用它。您甚至不需要从服务器请求该信息。但是我建议在sendemail.php文件中,如果可以发送电子邮件,则返回包含状态的对象。这样,如果出现问题,您可以显示错误消息。例如:

{
    success: true,
    message: '',
    errorCode: ''
}