I am unable to get this insert to db function to work using ajax and a form. When I execute this, I receive a blank alert box on success.
HTML
<div id="rsvpContent">
<form id="Attendevent" action="">
<input id="attend" name="attend" type="checkbox" value="yes" /><label for="attent">Yes, I plan to attend.</label>
<input type="hidden" value="<?php echo get_the_ID(); ?>" name="event_id"/>
<input type="hidden" value="<?php echo $current_user->ID; ?>" name="user_id"/>
</form>
</div>
PHP
add_action('wp_ajax_Attending', 'eventAttend');
add_action('wp_ajax_nopriv_Attending', 'eventAttend');
function eventAttend() {
global $wpdb;
$fields = array();
parse_str($_POST['data'], $fields);
if($fields['attend'] == 'yes') {
$wpdb->insert('wp_event_attendants', array( 'event_id' => $fields['event_id'], 'user_id' => $fields['user_id']));
echo $wpdb->print_error();
}
echo 'Not working.';
die();
}
JS (In header.php)
$("#attend").click(function(){
var form = $("#Attendevent").serialize();
$.ajax({
url: "<?php echo admin_url('admin-ajax.php'); ?>",
beforeSend: function() {
alert(form);
},
type:'POST',
data:{
action: "Attending",
"data": form
},
dataType: 'html',
success: function(data) {
alert(data);
}
});
});
DEBUGGING INFO
[07-Mar-2013 17:47:09] PHP Notice: Undefined index: attend in /wp-content/plugins/event-calendar-reg/functions.php on line 198