duanlin1931 2016-10-05 14:38
浏览 7
已采纳

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

I am interested to retrieve the name and the email of the user from my contact form and use it to display something like this: "Thank you for your message $name, we will reply to the following email: $email" I want to make changes to the .done function

Here is the 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>
\
     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条回答 默认 最新

  • dongxiong5546 2016-10-05 14:49
    关注

    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>
    \
                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();
    });

    Since you already have the name and email information in the form I dont understand why you just dont use that. You dont even need to ask for that information from the server. However I suggest that in the sendemail.php file you return an object containing status if the email could be sent or not. This way you can display an error message if something went wrong serverside. For instance like this:

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

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算