I am using ajax in WordPress and it is not working so can someone please help me out of this unwanted situation.
Here is my php code..
<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>
And here is the hook part.
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
?>
Here is ajaxurl code...
<?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
}
And here is my jquery code..
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 }
});
});
});