我是 PHP 和 WordPress 开发的新手。
在我的 WordPress 页面上,我想使用 AJAX 检查我页面查询字符串中的 aff_id
是否作为用户出现在我的 WordPress 数据库中。
下面是我使用“脚本和样式”插件放置在我的 WP 页面上的 JavaScript:
var affId = '';
affId = GetParameterValues('aff_id');
if (affId == undefined || affId == '')
affId = 'AKotawala';
const xhr = new XMLHttpRequest();
xhr.onload = function () {
alert(this.responseText);
};
xhr.open("POST", "../wp-includes/authenticate-user.php");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("username=" + affId);
function GetParameterValues(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return urlparam[1];
}
}
}
这是我的 PHP 代码
authenticate-user.php
<?php
$username = $_POST['username'];
if (my_username_exists($username))
echo "Exist";
else
echo "NotExist";
function my_username_exists($username) {
global $wpdb;
$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(user_login) FROM $wpdb->users WHERE user_login = %s", $username));
return empty( $count ) || 1 > $count ? false : true;
}
?>
当页面加载时,我的警告框是空的。
我做错了什么?
当我在我的 PHP 中硬编码 $username
变量并将文件作为 mydomain.com/wp-includes/authenticate-user.php
运行时,它返回空白页面。
我是否将 authenticate-user.php
放在了正确的文件夹中?
如果是,我应该在 JS 中传递给我的 AJAX 函数的 url 是什么?
最佳答案
尝试将此包含在您的 authenticate-user.php
文件中,并将该文件放在根目录中。
require_once(dirname(__FILE__) . '/wp-config.php');
或者给出当前目录的相对路径
require_once(dirname(__FILE__) . '../wp-config.php');
关于javascript - 正确文件夹中的 Wordpress、Javascript 和 Php : Am I placing authenticate-user. php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54166093/