这是我第一次做ajax而且我必须承认我很困惑。我想要做的是取3选择html的值来向我的数据库发出请求,根据所需的选择给我一个分数。使用ajax的目的是我不想要提交按钮,我希望当我的一个选择更改的值只选择我的数据库中的结果根据新值更改时。所以母鸡我在我的选择中更改了一个值我调用了一个javascript,它从我的select中获取值并将ajax传递给我的function.php然后使用我的Selects函数的值发出请求,然后返回结果我的要求。
对不起我的英语,这种语言不是我的第一语言。
现在我的代码看起来像这样:
的javascript:
function showWidth($) {
// get the value of my 3 html select
var width1 = document.getElementById('width1').value;
var width2 = document.getElementById('width2').value;
var width3 = document.getElementById('width3').value;
if (width1!="aucun" && width2!="aucun" && width3!="aucun") {
$.ajax({
ObjectName.ajaxurl,
data: {
action:'script_handle',
width01 : width1,
width02 : width2,
width03 : width3
},
success:function(data) {
// This outputs the result of the ajax request
document.getElementById("result").innerHTML=console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}
}
PHP(在function.php中):
function widthcalculate(){
if( isset($_REQUEST) ) {
$width1 = $_REQUEST['width01'];
$width2 = $_REQUEST['width02'];
$width3 = $_REQUEST['width03'];
// Let's take the data that was sent and do something with it
global $wpdb;
$where = "'".$width1."' AND camber = '".$width2."' AND handrim = '".$width3."'";
$sql = $wpdb->get_results(
"SELECT result FROM mc_widthCalculatorResult WHERE wheel = ".$where.""
);
foreach ($sql as $element) {
echo $element->result;
}
echo $result;
//print_r($_REQUEST);
}
die();
}
add_action( 'wp_ajax_script_handle', 'widthcalculate' ); // Hook for the WordPress Dashboard.
add_action( 'wp_ajax_script_handle_nopriv', 'widthcalculate' ); // Hook for the user facing side of the site.
function register_scripts() {
wp_register_script( 'script-handle', $path_to_javascript_file, $dependencies, 'version', $in_footer = true );
wp_enqueue_script( 'script-handle' );
wp_localize_script( 'script-handle', 'ObjectName', array( 'ajaxurl' => admin_url( '/wp-admin/admin-ajax.php' ), 'parameter' => $whatever_you_want_to_pass ) );
}
add_action( 'enqueue_scripts', 'register_scripts' );
答案 0 :(得分:0)
在处理此函数之前,您需要将wp_enqueue_script和wp_localize_script绑定到正确的WordPress挂钩。
add_action( 'wp_enqueue_scripts', "FUNCTION_NAME");
FUNCTION_NAME将是一个只包含wp_enqueue_script和wp_localize_script的函数的名称。
您的wp_localize_script需要与wp_enqueue_script具有相同的slug。
这是我在WordPress中使用AJAX保留的样板:https://gist.github.com/theantichris/6600226
答案 1 :(得分:0)
我很困惑,有很多事情无法解释和错误。
使用wp_enqueue_scripts
过滤器加载这样的脚本。注意:不使用可以更改的绝对路径,正确传递最后两个参数:
function bones4_enqueue_scripts() {
wp_enqueue_script('widthcalculator', get_template_directory_uri() . '/js/widthCalculator.js', 'jquery', false, true);
}
add_action('wp_enqueue_scripts', 'bones4_enqueue_scripts');
你的ajax脚本应该是单独的,而不是在functions.php中,并且它不应该在一个动作钩子中,把它作为function.php是不好的做法,并且请求不会被处理为无论如何它都是。
您正在向完全不同的脚本发送ajax请求(wp-admin/admin-ajax.php
)
什么是wp_ajax_nopriv_widthcalculate
和wp_ajax_widthcalculate
过滤器 - 您是否已将这些名称添加到了? Wordpress什么时候打电话给他们?
您正在混合使用camelcase,noncamelcase,连字符和下划线命名约定 - 为什么?保持一致。
您正在使用wp_localize_script
传递脚本的路径,但之后不在javascript中使用它并使用绝对路径。至少你可以ajax到/wp-admin/admin-ajax.php
,但我不确定该路径是否正确,如果你发布到主题目录中的php文件,你应该将该目录的路径传递给javascript通过像wp_localize_script
这样的东西,或者通过自定义的东西。
正如我所说,你应该制作一个单独的ajax脚本,你在其中发布请求但不在任何动作钩子中你应该有if ( isset($_REQUEST) ) {
的东西。
您还没有解释您的脚本的用途是什么,因此我无法就更好的解决方案提出建议,只能解释现有代码中出现的错误。
看看它是如何发展的。
答案 2 :(得分:0)
这是我第一次做ajax而且我必须承认我很困惑。我想要做的是取3选择html的值来向我的数据库发出请求,根据所需的选择给我一个分数。使用ajax的目的是我不想要提交按钮,我希望当我的一个选择更改的值只选择我的数据库中的结果根据新值更改时。所以母鸡我在我的选择中更改了一个值我调用了一个javascript,它从我的select中获取值并将ajax传递给我的function.php然后使用我的Selects函数的值发出请求,然后返回结果我的要求。
对不起我的英语,这种语言不是我的第一语言。
现在我的代码看起来像这样:
的javascript:
function showWidth($) {
// get the value of my 3 html select
var width1 = document.getElementById('width1').value;
var width2 = document.getElementById('width2').value;
var width3 = document.getElementById('width3').value;
if (width1!="aucun" && width2!="aucun" && width3!="aucun") {
$.ajax({
ObjectName.ajaxurl,
data: {
action:'script_handle',
width01 : width1,
width02 : width2,
width03 : width3
},
success:function(data) {
// This outputs the result of the ajax request
document.getElementById("result").innerHTML=console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}
}
PHP(在function.php中):
function widthcalculate(){
if( isset($_REQUEST) ) {
$width1 = $_REQUEST['width01'];
$width2 = $_REQUEST['width02'];
$width3 = $_REQUEST['width03'];
// Let's take the data that was sent and do something with it
global $wpdb;
$where = "'".$width1."' AND camber = '".$width2."' AND handrim = '".$width3."'";
$sql = $wpdb->get_results(
"SELECT result FROM mc_widthCalculatorResult WHERE wheel = ".$where.""
);
foreach ($sql as $element) {
echo $element->result;
}
echo $result;
//print_r($_REQUEST);
}
die();
}
add_action( 'wp_ajax_script_handle', 'widthcalculate' ); // Hook for the WordPress Dashboard.
add_action( 'wp_ajax_script_handle_nopriv', 'widthcalculate' ); // Hook for the user facing side of the site.
function register_scripts() {
wp_register_script( 'script-handle', $path_to_javascript_file, $dependencies, 'version', $in_footer = true );
wp_enqueue_script( 'script-handle' );
wp_localize_script( 'script-handle', 'ObjectName', array( 'ajaxurl' => admin_url( '/wp-admin/admin-ajax.php' ), 'parameter' => $whatever_you_want_to_pass ) );
}
add_action( 'enqueue_scripts', 'register_scripts' );