如何在Wordpress主题中添加Ajax [英] How to add ajax to wordpress theme

查看:62
本文介绍了如何在Wordpress主题中添加Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,现在已经困扰了好几天... 我正在尝试使用一个简单的ajaxPOST函数将数据发送到MySQL数据库(而不是WP数据库).

此代码位于主题的"single-post.php"中,因为必须在每次发布之前对其进行检查.

$.ajax({ url: 'library/functions/admin_checkuser.php',
     data: {action: userID},
     type: 'post',
     success: function(output) {
                  alert(output);
     }
});

我只是向"admin_checkuser.php"脚本发送一个变量,该脚本依次调用另一个对数据库执行操作的脚本.

这是"admin_checkuser"的代码:

$userid = $_POST['action'];

echo $userid;//for testing
$oMySQL = new MySQL();
$query = "Select * FROM videotable WHERE uid = '$userid'";
$oMySQL->ExecuteSQL($query);
$bb = $oMySQL->iRecords;
$aa = $oMySQL->aResult;
echo $bb;

if ($bb == 0){
$query = "INSERT INTO videotable VALUES ('','$userid','true')";
$oMySQL->ExecuteSQL($query);
echo 'true';
    exit();

}else{
$sharing = mysql_result($aa,0,"share");
echo $sharing;
exit();

}

但是我认为调用不会传递到脚本. 这些脚本已在WordPress之外进行了测试并可以正常工作,因此WordPress中一定有一些阻止ajax调用的脚本. 顺便说一句,我尝试将"admin_checkuser.php"放置在许多不同的文件夹中,但没有任何效果.

谢谢.

解决方案

您应检查自己的Ajax调用网址.

也许使用完整的URL而不是相对的URL.

可能是因为您的主题的位置使网址不正确.我假设您的Ajax代码位于主题文件夹中.

有一些wordpress函数可以获取主题目录.

例如,如果您在此页面http://yourwebpage/test/,则ajax调用将在此处http://yourwebpage/test/library/functions/admin_checkuser.php进行.我认为这是错误的位置.

这就是为什么您需要在脚本中添加绝对URL的原因.并且,如果它在您的主题中,则可以使用此方法get_template_directory_uri()来获取模板目录.

请参阅此处: http://codex.wordpress.org/Function_Reference/get_template_directory_uri

I've got a problem that I'm stuck on for days now... I'm trying to use a simple ajaxPOST function to send data to a MySQL database (not WP database).

This code is located within "single-post.php" in the theme, because it must be checked before every post.

$.ajax({ url: 'library/functions/admin_checkuser.php',
     data: {action: userID},
     type: 'post',
     success: function(output) {
                  alert(output);
     }
});

I'm simply sending a variable to a "admin_checkuser.php" script which in turn calls another script that take actions on the database.

This is the code for "admin_checkuser":

$userid = $_POST['action'];

echo $userid;//for testing
$oMySQL = new MySQL();
$query = "Select * FROM videotable WHERE uid = '$userid'";
$oMySQL->ExecuteSQL($query);
$bb = $oMySQL->iRecords;
$aa = $oMySQL->aResult;
echo $bb;

if ($bb == 0){
$query = "INSERT INTO videotable VALUES ('','$userid','true')";
$oMySQL->ExecuteSQL($query);
echo 'true';
    exit();

}else{
$sharing = mysql_result($aa,0,"share");
echo $sharing;
exit();

}

But I don't think the calls go through to the script. These scripts where tested outside of WordPress and did work, so it must be something in WordPress that blocking the ajax call. BTW, I tried placing the "admin_checkuser.php" in many diffrent folders but nothing worked.

Thanks in advance.

解决方案

You should check your url for your ajax call.

Maybe use the full url instead of the relative one.

It could be because of the location of your theme makes the url incorrect. I'm assuming your ajax code is in your theme folder.

There are some wordpress functions that get the theme directory.

For example if you at this page http://yourwebpage/test/ then the ajax call will go here http://yourwebpage/test/library/functions/admin_checkuser.php. This I assume would be the incorrect location.

This is why you need to add the absolute url to your script. And if it is in your theme, you can use this method get_template_directory_uri() to get the template directory.

See here: http://codex.wordpress.org/Function_Reference/get_template_directory_uri

这篇关于如何在Wordpress主题中添加Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆