Discussion:
Add Friend - Ajax - Failing & Don't See Why...
BenderisGreat
2013-10-31 17:10:10 UTC
Permalink
Alright, A button that triggers an update_user_meta function, which adds the
user ID of profile user user to the meta field 'friends' belonging to the
user_meta of the currently active user. I click add friend, and the
success action runs, but I get a "failed" alert. I dont see any errors
popping up in the console.

Any ideas? Also- Is there a way to view errors that occur off page, inside
funtions.php? That would be an immense help.

<?php //friends
echo "<button id='add_friend' value=".$_current_member->ID.">Add
Friend</button>";
?>

<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
$( '#add_friend').click( function( event ) {
var $button2 = $(this);
var profileID = $(this).attr('value');
var ajaxurl = '<?php echo admin_url('admin-ajax.php') ?>';

var data = {
'action': 'my_add_friend_action',
'profileID': profileID }

$.post(
ajaxurl,
data,
function ( response ) {
if ( ! response.success ) {
alert( 'Failure!' ); }

$button2.html("FRIEND ADDED");

}
);
});
});

and the wordpress function that handles this:

add_action('wp_ajax_my_add_friend_actionn', 'my_add_friend_action');
add_action('wp_ajax_nopriv_my_add_friend_action', 'my_add_friend_action');
function my_add_friend_action() {
global $wpdb, $current_user;
get_currentuserinfo();

$profileID = $_POST['profileID'];
$userID = $_POST['userID'];

// activity_tracker($current_user, 'added_friend', 'prof');
update_user_meta( $user->ID, 'friends', $profileID );

$response = array( 'success' => true );

wp_send_json_success($response);

die();
}



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Add-Friend-Ajax-Failing-Don-t-See-Why-tp42686.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
J.D. Grimes
2013-10-31 18:29:51 UTC
Permalink
Post by BenderisGreat
Is there a way to view errors that occur off page, inside
funtions.php? That would be an immense help.
Are you using WP_DEBUG? https://codex.wordpress.org/WP_DEBUG
Post by BenderisGreat
add_action('wp_ajax_my_add_friend_actionn', 'my_add_friend_action');
There's an extra 'n' at the end of the action name.

-J.D.
Gregory Lancaster
2013-10-31 19:38:15 UTC
Permalink
I do have debug on, but that doesnt show me any errors going on inside a
plugin file. Is there a way to var_dump variables inside a plugin file
like that?
Post by J.D. Grimes
Post by BenderisGreat
Is there a way to view errors that occur off page, inside
funtions.php? That would be an immense help.
Are you using WP_DEBUG? https://codex.wordpress.org/WP_DEBUG
Post by BenderisGreat
add_action('wp_ajax_my_add_friend_actionn', 'my_add_friend_action');
There's an extra 'n' at the end of the action name.
-J.D.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-10-31 19:54:10 UTC
Permalink
Debug does show/log errors inside plugin files. I guess maybe what you are asking is if there is a way to do a var_dump, but into the error log? Here is a function that I use for that:

/**
* Dump a variable to the error log.
*
* You can pass as many variables as you want!
*
* @param mixed $var
* @param mixed ...
*/
function var_log() {

ob_start();
call_user_func_array( 'var_dump', func_get_args() );
error_log( ob_get_clean() );
}

Usage:

var_log( $var, $var_2 );

-J.D.
Post by Gregory Lancaster
I do have debug on, but that doesnt show me any errors going on inside a
plugin file. Is there a way to var_dump variables inside a plugin file
like that?
Post by J.D. Grimes
Post by BenderisGreat
Is there a way to view errors that occur off page, inside
funtions.php? That would be an immense help.
Are you using WP_DEBUG? https://codex.wordpress.org/WP_DEBUG
Post by BenderisGreat
add_action('wp_ajax_my_add_friend_actionn', 'my_add_friend_action');
There's an extra 'n' at the end of the action name.
-J.D.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Continue reading on narkive:
Loading...