我在WordPress中使用了Ajax,但无法正常工作,因此有人可以帮助我解决这种不必要的情况。
这是我的PHP代码。
<div class="Publish">
<h2>Published Pages</h2>
<?php
$pages = new WP_Query(array('post_type'=>'page','post_status'=>'publish','posts_per_page'=>'-1','order'=>'ASC'));
?>
<ul id="dashboard-page">
<?php if($pages -> have_posts()){
while($pages -> have_posts()){
$pages->the_post(); ?>
<li id="<?php the_ID();?>" class="dashboard-item"><?php echo the_title(); ?></li>
<?php }
} ?>
</ul>
<input class="button-primary" id="submit" type="submit" name="submit" value="Click Me" />
</div>
这是挂钩部分。
function ajax_action_stuff() {
// doing ajax stuff
update_option('vipin');
die(); // stop executing script
}
add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' ); // ajax for logged in users
add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' ); // ajax for not logged in users
?>
这是ajaxurl代码...
<?php
add_action('wp_head','pluginname_ajaxurl');
function pluginname_ajaxurl() {
?>
<script type="text/javascript">
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
<?php
}
这是我的jquery代码。
jQuery(document).ready(function(){
jQuery('#submit').click(function(){
var i = 0;var a = [];
jQuery('#dashboard-page li').each(function(){
a[i] = jQuery(this).attr('id');
str = a.toString();
i++;
});
jQuery.ajax({
method: "POST",
url: ajax_object.ajax_url,
data: { action: "ajax_action", pageorder: str }
});
});
});
我忘了在我的update_option()函数中添加$ _POST ['str']。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句