再一次用 AJAX 把我的头发拉出来。这是我正在开发的一个简单的列表插件。我正在尝试使用 AJAX 回调将 JQUERY 添加到简码中。
最有趣的部分是 Javascript/JQuery 没有进入页面。例如,当在页面上调用短代码时应该会出现一个警告框(这样我就知道它正在工作),并且应该说“至少这是工作”。它应该在文件准备好后立即发生。但是由于某种原因,没有警告框,让我相信我永远不会到达 AJAX 回调,因为指定 AJAX 回调的原始 JQuery 代码不知何故被破坏了。
您可以在下面的第 34 行附近看到这一点。如果我能弄清楚为什么 javascript 无法正常工作,我敢打赌其余的都会排成一行。我尝试将 JQuery 放入一个变量中并返回它、回显它等。还尝试将 JQuery 代码放在代码中的表单之前和之后。还是不行。
这是代码:
/*
*
*
SHORTCODE
*
*
*/
add_shortcode('list-up-down', 'cb_lud_scfunc');
function cb_lud_scfunc() {
global $wpdb;
$cb_lud_prefix = $wpdb->prefix;
$cb_lud_table = $cb_lud_prefix . 'cb_list_up_down';
$cb_lud_homeurl = home_url();
$cb_lud_upimg = plugins_url('list-up-down/images/up-arrow.png', _FILE_);
$cb_lud_downimg = plugins_url('list-up-down/images/down-arrow.png', _FILE_);
$cb_lud_sample_query = $wpdb->query('SELECT * FROM '.$cb_lud_table);
$cb_lud_field1_name = $wpdb->get_col_info('name',1);
$cb_lud_field2_name = $wpdb->get_col_info('name',2);
/*
CREATE THE JAVASCRIPT/JQUERY
*/
//Create the Javascript and AJAX
add_action('init', 'cb_lud_action_javascript');
function cb_lud_action_javascript() {
?>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript' >
$(document).ready(function(){
alert('at least this is working');
//JQuery for the submission of a new list item.
$('#list-up-down-form').submit(function(e){
e.preventDefault(); //Works to prevent normal submission of the form.
var field1_input = $('#field1_input').val();
var field2_input = $('#field2_input').val();
var data = {
action: 'cb_lud_ajax_action',
field1: field1_input,
field2: field2_input,
};
$.ajax ({
type: 'POST',
url: ajaxurl,
data: data,
success: function() {
alert('It worked!');
}
});
});
});
</script>
<?php
//End Javascript function.
}
/*
CREATE THE FORM
*/
//Create the form to allow users to add records
$cb_lud_sc_form = '
<form id="list-up-down-form" name="list-up-down-form" action="" method="post">
<table border="0">
<tr>
<td><h2>'.$cb_lud_field1_name.':</h2><input id="field1_input" name="field1_input" type="text"></input></td>
<td><h2>'.$cb_lud_field2_name.':</h2><input id="field2_input" name="field2_input" type="text"></input></td>
<td valign="bottom"><input name="add_record" type="submit" value="Add Record" /></td>
</tr>
</table>
</form>
';
/*
DISPLAY THE LIST
*/
//Get the list from the database, and set the variables for display in the output.
$get_list = $wpdb->get_results('SELECT entry_ID, '.$cb_lud_field1_name.' AS "field1", '.$cb_lud_field2_name.' AS "field2", up_votes, down_votes, up_votes - down_votes AS "total_votes"
FROM '.$cb_lud_table.'
GROUP BY entry_ID
ORDER BY total_votes DESC
',OBJECT);
//Check if list is null, and if so, set a variable to display a warning. Otherwise, display the list.
if (empty($get_list)) {
$cb_lud_sc_output .= "<em>Warning: You don't seem to have any records for this list. Why don't you add some now?</em>";
$cb_lud_sc_output .= $cb_lud_sc_form;
return $cb_lud_sc_output;
}
else {
$cb_lud_sc_output .= $cb_lud_sc_form;
$cb_lud_sc_output .= '</br>';
$cb_lud_sc_output .= '<table border="1" cellpadding="10">';
$cb_lud_sc_output .= '<tr><td align="center"><strong>'.$cb_lud_field1_name.'</strong></td><td align="center"><strong>'.$cb_lud_field2_name.'</strong></td><td align="center"><strong>Score</strong></td></tr>';
foreach ($get_list as $list_items) {
$cb_lud_sc_output .= '<tr><td>'.$list_items->field1.'</td><td>'.$list_items->field2.'</td><td>'.$list_items->total_votes.'</td></tr>';
}
$cb_lud_sc_output .= '</table>';
return $cb_lud_sc_output;
}
return $cb_lud_sc_output;
/*
CREATE THE AJAX
*/
//Use the Wordpress AJAX functions
add_action('wp_ajax_cb_lud_ajax_action', 'cb_lud_ajax_callback');
add_action('wp_ajax_nopriv_cb_lud_ajax_action', 'cb_lud_ajax_callback');
//Create the function that executes the AJAX Callback
function cb_lud_ajax_callback() {
global $wpdb;
$prefix = $wpdb->prefix;
$cb_lud_table = $prefix . 'cb_list_up_down';
$field1_data = $_POST['field1'];
$field2_data = $_POST['field2'];
$up_votes = 0;
$down_votes = 0;
$new_data = array(
$cb_lud_field1_name => $field1_data,
$cb_lud_field2_name => $field2_data,
'up_votes' => $up_votes,
'down_votes' => $down_votes,
);
$format = array('%s', '%s', '%f', '%f');
/*$wpdb->insert(
$cb_lud_table, $new_data, $format
);*/
$wpdb->query('INSERT INTO
'.$cb_lud_table.' ('.$cb_lud_field1_name.', '.$cb_lud_field2_name.', up_votes, down_votes)
VALUES ('.$field1_data.', '.$field2_data.', '.$up_votes.', '.$down_votes.')
');
die(); // this is required to return a proper result
}
};
/*
END SHORTCODE
*/