WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Ajax for Plugins (1 post)

  1. bmd91
    Member
    Posted 1 year ago #

    I have the code in a file save_author_description.js:

    $(document).ready(function() {
            $('#save_edit_author_description').click(function(){
            var description = $('#edit_author_description').val();
            console.log(description);
              	$.ajax({
                type: 'POST',
                url: '/wp-admin/admin-ajax.php',
                data: {
                    action: 'update_user_description',
                    description: description,
                },
                success: function(data){
                    $('#author_description_saved').html('').append(data);
                },
                error: function(){
                    alert('error');
                }
            });
        });
    });

    in a js file. And in functions.php I have :

    function add_save_author_description(){
    	wp_enqueue_script('save_author_description.js', get_bloginfo('template_directory'). "/scripts/save_author_description.js", array('jquery'));
    }
    add_action('init','add_save_author_description');
    
    function update_user_description(){
    	global $wpdb;
    	$description = $_POST['description'];
    	if(isset($description)){echo $description;}else{echo 'nothing';}
    	die($description);
    }
    add_action( 'wp_ajax_nopriv_update_user_description', 'update_user_description' );
    add_action( 'wp_ajax_update_user_description', 'update_user_description' );

    and html of:

    <textarea style="display:none;" id="edit_author_description">Tell Us a Little About Yourself</textarea>
    	<button style="display:none;" id="save_edit_author_description">Save</button>
    	<div id="author_description_saved"></div>

    However, when I click on the button with id of 'save_edit_author_description' literally nothing happens. Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags