在我的插件中,我在后端使用了两个ajax调用。这很有效。但为了在前端提供相同的功能,我用另一个名称加载了这两个函数的副本,我的代码是:
在我的插件主文件中:
function my_action_callback(){
global $wpdb;
if (@$_POST[\'id\']) {
$daty = $wpdb->get_results("select eemail_id,eemail_content,eemail_subject from " . WP_eemail_TABLE . " where 1=1 and eemail_status=\'YES\' and eemail_id=" . $_POST[\'id\']);
//echo "select eemail_id,eemail_content,eemail_subject from ".WP_eemail_TABLE." where 1=1 and eemail_status=\'YES\' and eemail_id=".$_POST[\'id\'];
if (!empty($daty)) {
foreach ($dat as $datt) {
echo $datt->eemail_content . "_" . $datt->eemail_subject;
die();
}
}
}
}
add_action(\'wp_ajax_nopriv_my_action\', \'my_action_callback\');
在我的设置中。js公司function showEntryfront(id)
{
jQuery(document).ready(function($) {
var data = {
action: \'my_action\',
id: id
};
jQuery.post(ajaxurl, data, function(response) {
//alert(\'Got this from the server: \' + response);
var n=response.split("_");
jQuery(\'textarea#mail_contents\').text(n[0]);
$(\'#mail_subject\').val(n[1]);
// jQuery(\'textbox#mail_subject\').text(n[1]);
});
});
}
触发此showEntryfront()的插件页面是:<select name="eemail_subject_drop" id="eemail_subject_drop" onchange="showEntryfront(this.value)">
<option value="">Select Email subject</option>
<?php echo $eemail_subject_drop_val; ?>
</select>
<input type="hidden" name="send" value="" id="send" /> </td>
<tr height="60px;"> <td>
<textarea name="mail_contents" id="mail_contents" rows="4" cols="40" ></textarea>
</td></tr>
第二个功能也是如此。只有db表不同。我的firebug控制台仍然显示
ReferenceError: ajaxurl is not defined
jQuery.post(ajaxurl, data, function(response) {
而且textarea没有填充WP\\u eemail\\u表中的db数据。请不要认为此功能在wp admin后端工作。我知道admin中的ajax调用将由admin ajax自动加载。php。但我如何在前端实现同样的功能呢。我的代码有错误吗??