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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。