我试图使用Ajax通过我创建的web表单发送电子邮件,但我迷路了。我不知道Ajax在Wordpress中是如何工作的。
我首先创造了一个动作
add_action( 'wp_ajax_siteWideMessage', 'wpse_sendmail' );应该检索数据并发送邮件的函数是:
function wpse_sendmail()
{
$for = like_escape($_POST['forwhat']);
$email = like_escape($_POST['email']);
$headers = 'From: '.$email ."\r\n".'Reply-To: '.$email;
$message = like_escape($_POST['message_message']);
$respond = like_escape($_POST['message_email']);
wp_mail( "support@ontrgt.net", "(OTN) Support: ".$support, $message, $headers);
}最后,js是这样的:
$("#contact-send").click(function(){
var forwhat = $("#contact-for").val();
var name = $("#contact-name").val();
var email = $("#contact-email").val();
$.post( "<?php echo esc_js( site_url() ) ?>", { siteWideMessage:"null", forwhat: forwhat, name: name, email:email }, function(){
console.log("success");
});
});我不太确定我在这里错过了什么。有人能帮我理解Wordpress的Ajax过程吗?
更新
到目前为止,我更新了我的代码:
PHP
add_action( 'wp_ajax_siteWideMessage', 'wpse_sendmail' );
add_action( 'wp_ajax_nopriv_siteWideMessage', 'wpse_sendmail' );
function wpse_sendmail()
{
$for = $_POST['forwhat'];
$email = $_POST['email'];
$headers = 'From: '.$email ."\r\n".'Reply-To: '.$email;
$message = $_POST['message_message'];
$respond = $_POST['message_email'];
/*if(wp_mail( "support@ontrgt.net", "(OTN) Support: ".$for, $message, $headers))
{
echo "WOOHOO";
}*/
if($for)
{
//Just to see if there is any response.
echo "Whoohoo";
}
die();
}Js
$("#contact-send").click(function(){
var forwhat = $("#contact-for").val();
var name = $("#contact-name").val();
var email = $("#contact-email").val();
var data = { action:'siteWideMessage', forwhat:forwhat, name:name, email:email };
$.post('<?php echo admin_url("admin-ajax.php"); ?>', data, function(response) {
alert(response);
});
});Wordpress仍然没有响应我的AJAX命令。我正在使用inspect元素,并且没有看到数据正在传递。
发布于 2014-10-06 02:31:03
首先,您需要添加两个操作,一个用于显式需要的非登录用户,以使其工作,例如,如下所示(基本上在functions.php文件中):
add_action( 'wp_ajax_siteWideMessage', 'wpse_sendmail' );
add_action( 'wp_ajax_nopriv_siteWideMessage', 'wpse_sendmail' );然后,您需要向admin-ajax.php发出请求,因此在jQuery函数中,您可以使用如下内容:
$("#contact-send").click(function(e){
e.preventDefault(); // if the clicked element is a link
//...
var data = { 'action':'siteWideMessage', 'more':'values' };
$.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
console.log(response);
});
});例如,确保将exit/die放在服务器端handler的末尾:
function wpse_sendmail()
{
// Process the post data...
if(wp_mail(...)) {
echo 'success';
}
else {
echo 'failed';
}
die();
}在success回调中,response变量将获得从服务器端发送的响应,即success/failed。有更好的方法来做到这一点(使用脚本等)。读这篇详细文章。另外,如果您想返回一个json响应,那么您可以使用$.json('url', data, func)。
如果您感到困惑,那么让我告诉您,您应该向admin-ajax.php发出请求,并将请求传递给action,在本例中是siteWideMessage,因此WordPress将调用使用add_action钩子注册的handler,在您的示例中是wpse_sendmail.Eid Mubarak :-)
发布于 2017-06-10 12:40:04
我不得不做一些稍微不同的事情才能让这件事起作用。
首先,我在模板页面上添加了一个隐藏的输入,并给它提供了如下ajax url的值:
<input type="hidden" id="ajax_url" value="<?php echo admin_url('admin-ajax.php'); ?>"/>然后我在我的functions.php里放了这个
add_action( 'wp_ajax_siteWideMessage', 'wpse_sendmail' );
add_action( 'wp_ajax_nopriv_siteWideMessage', 'wpse_sendmail' );
function wpse_sendmail() {
echo "hewwo world";
die();
}在单独的js文件中,我的js是这样的:
$("#submit-form").click(function(e){
e.preventDefault(); // if the clicked element is a link
$.post($("#ajax_url").val(), {
action: 'siteWideMessage',
// and the rest
}, function(response) {
// handle a successful response
console.log(response)
});
});这对我有用。
发布于 2014-10-06 02:09:55
查看(行动)的文档,您只需将action参数设置为"siteWideMessage“即可。例如..。
$.post(<?= json_encode(admin_url('admin-ajax.php')) ?>, {
action: 'siteWideMessage',
// and the rest
}, function(response) {
// handle a successful response
});https://stackoverflow.com/questions/26188865
复制相似问题