• Okay, so I’ve posted this problem a few times now, updating it as I try new things and get closer, but I know I’m really close now, just need a bit of your AJAX expertise. Anyway, the background: I am trying to update a custom user profile field from a page on the site (in this case, I am storing the user’s experience in a rpg-type game) but no matter what I do it doesn’t seem to want to update the meta. So, here’s the code for my custom user field, in case you need to reference it:

    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    
    function my_show_extra_profile_fields( $user ) { ?>
    
    	<h3>Extra profile information</h3>
    
    	<table class="form-table">
    		<tr>
    			<th><label for="experience">Total Experience</label></th>
    			<td>
    				<input type="text" name="experience" id="experience" value="<?php echo esc_attr( get_the_author_meta( 'experience', $user->ID ) ); ?>" class="regular-text" /><br />
    				<span class="description">Your Total Experience</span>
    			</td>
    		</tr>
    	</table>
    <?php }
    
    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    function my_save_extra_profile_fields( $user_id ) {
    	if ( !current_user_can( 'edit_user', $user_id ) )
    		return false;
    	update_user_meta( $user_id, 'experience', $_POST['experience'] );
    }

    Here is my form that I am using to update the experience:

    <form id="yourprofile" action="" method="post">
    <p>
    <input type="text" name="checkuser_id" id="checkuser_id" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->ID; ?>" />
    </p>
    <div style="clear: both;">
    <h4>Total Experience</h4>
    <?php if ( get_the_author_meta( 'experience' ) ) { ?>
    <input name="userxp" id="userxp" value="<?php the_author_meta( 'experience' ); ?>" type="text" readonly>
    <?php } // End check for experience ?>
    </div>
    <input name="updatexp" id="updatexp" class="btnb" type="submit" value="Update Experience">
    </form>

    here is the javascript I added to the page as well:

    <script type="text/javascript">
    jQuery(document).ready(function(){
    	var userid = jQuery('#checkuser_id').val();
    	var exp = jQuery('#userxp').val();
    	jQuery("#yourprofile").submit(function()	{
    				var data = {
    					action: 'my_save_userxp',
    					userxp: exp,
    					checkuser_id: userid
    				};
    				jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", data,
    				function(response){
    					alert('Got this from the server: ' + response)
    				});
    	});
    });
    </script>

    and here is what I added to functions.php:

    add_action('wp_ajax_my_save_userxp', 'my_save_userxp_callback');
    function my_save_userxp_callback() {
    $user_id = $_POST['checkuser_id'];
    $userxp = $_POST['userxp'];
    global $wpdb;
    update_user_meta( $user_id, 'experience', $userxp );
    }

    Please, I am begging anyone there with ajax knowledge to give me any pointers. I am really at a dead end here and can’t continue with my project until I get this up and running. Thanks all in advance, hope to hear from you soon

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘AJAX Troubles’ is closed to new replies.