将变量从PHP传递到javascript并传递到html表单

我有一个非常小的问题,我使用jQuery,jQuery表单和PHP Mail在WordPress中设置的联系表单发送一个表单生成的电子邮件。

要替换我在联系表单页面中执行pHpvalidation的当前联系表单,然后使用PHP Mail发送,我设计了以下简单的html 5表单(可在此页面上看到: http://edge.donaldjenkins .net / contact ):

然后我使用jQuery validate [jquery.validate.js]和form [jquery.form.js]插件来执行客户端validation:

    $(function(){ $('#contact').validate({ submitHandler: function(form) { $(form).ajaxSubmit({ url: 'send-message.php', success: function() { $('#contact').hide(); $('#instructions').hide(); $('#status').show(); $('#popular-posts').show(); $('#status').append("

Thanks! Your request has been sent. One will try to get back to you as soon as possible.

") } }); } }); });

validation之后,上面的脚本使用jQuery.form的ajaxSubmit函数将日期提交到服务器PHP页面send-message.php(原因是javascript无法发送电子邮件)。 我使用这个PHP文件对数据进行第二次服务器端validation(尽管这可能是多余的,因为设置依赖于javascript将数据传递到服务器,可以安全地假设没有人会能够使用没有启用JavaScript的表单)。 它还对表单中添加的隐藏输入字段中的数据执行蜜jarvalidation码检查。 然后发送电子邮件:

 <?php //invoke wp_load in order to use WordPress wp_mail plugin function instead of mail for better authentification of the sent email require_once("/path/to/wp-load.php"); //Check to see if the honeypot captcha field was filled in if(trim($_POST['checking']) !== '') { $captchaError = true; $errors .= "\n Error: Captcha field filled in"; } else { //Check to make sure that the name field is not empty if(trim($_POST['name']) === '') { $errors .= "\n Error: Name field empty"; $hasError = true; } else { $name = strip_tags($_POST['name']); } //Check to make sure sure that a valid email address is submitted if(trim($_POST['email']) === '') { $emailError = 'You forgot to enter your email address.'; $hasError = true; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[AZ]{2,4}$", trim($_POST['email']))) { $errors .= "\n Error: Message field empty"; $hasError = true; } else { $email = strip_tags($_POST['email']); } //Check to make sure comments were entered if(trim($_POST['message']) === '') { $errors .= "\n Error: Message field empty"; $hasError = true; } else { $phone = strip_tags($_POST['phone']); $url = strip_tags($_POST['url']); if(function_exists('stripslashes')) { $message = stripslashes(trim($_POST['message'])); } else { $message = strip_tags($_POST['message']); } } //If there is no error, send the email if(!isset($hasError)) { // Send Message $emailTo = 'me@mydomain.com'; $subject = 'Contact Form Submission from '.$name; $body = "Name: $name \n\nEmail: $email \n\nPhone: $phone \n\nWebsite: $url \n\nMessage: $message"; $headers = 'From: My Site ' . "\r\n" . 'Reply-To: ' . $email; wp_mail($emailTo, $subject, $body, $headers); $alert = 'Thanks! Your request has been sent. One will try to get back to you as soon as possible.'; } else { $alert = $errors; } } ?> 

服务器会跟踪是否找到错误,或者是否在变量$ alert中成功发送了电子邮件。

在pHp函数完成之后,脚本会隐藏联系页面中的表单,并显示之前隐藏的html元素,它会向其附加警报消息。

我无法解决的问题是让javascript更改该警报消息的措辞以反映电子邮件是否成功发送,因为我不知道如何传递包含列表的必需的pHp变量($ alert)服务器PHP进程中的错误消息,以便在“联系表单”页面中插入脚本。 当然,这也是一个非常理论上的问题,因为出于上述原因,一个容易出错的消息甚至不可能首先进入PHP阶段。

我试过在PHP文件的末尾插入以下代码,但无济于事:

  alert = ;  

后一种尝试不会在Firebug中生成任何错误,但变量值不会被传递。 欢迎任何想法或想法。

更新 :我添加了此工作流程图以阐明此问题的设置: 在此处输入图像描述

在send-message.php中,将警报文本放在IDd

  

那么你在调用页面上的Javascript应该是这样的:

  

希望你能看到我如何使用参数success回调来检索AJAX请求响应的HTML内容,找到statusmsg div并将其附加到调用页面上的相关元素。

更优化的是,send-message.php会打印JSON而不是HTML,这使得这个过程更容易一些。 我会把它留作读者的练习..至少在这个问题中。

您希望在响应AJAX调用时返回电子邮件的成功或失败。

对于此示例示例,您只需返回falsetrue 。 如果您需要更多信息,请返回JSON字符串,例如…

 { "success": true, "something": ["something", "something-else"] } 

您可以使用json_encode()轻松地将数组转换为PHP中的JSON。