WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


<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"/>


 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.';

JS (In header.php)

    var form = $("#Attendevent").serialize();

      url: "<?php echo admin_url('admin-ajax.php'); ?>",
      beforeSend: function() {
          action: "Attending",
          "data": form 
      dataType:  'html',
      success: function(data) {


[07-Mar-2013 17:47:09] PHP Notice: Undefined index: attend in /wp-content/plugins/event-calendar-reg/functions.php on line 198

share|improve this question
Are your JS alerts firing? And if so, what do they say? By the way, I wouldn't use 'form' as a JS variable name. That seems to be inviting problems. – vancoder Mar 7 '13 at 17:37
I'd also advise using Firebug to see what's going on. – vancoder Mar 7 '13 at 17:38
up vote 1 down vote accepted

You are serializing your form data before posting it to your PHP function, but you do not unserialize it at the other end. Therefore your check for $_POST['attend'] == 'yes'will always fail.

Try the following (untested).

    function eventAttend() {
    $fields = array( );
    parse_str( $_POST['data'], $fields );
               if($fields['attend'] == 'yes') {

global $wpdb;

                    $wpdb->insert('wp_event_attendants', array( 'event_id' => $fields['event_id'], 'user_id' => $fields['user_id'])); 

                    echo $wpdb->print_error();
share|improve this answer
Is the unserializing always required or only in wordpress? – MF1 Mar 7 '13 at 18:35
You always have to unserialize stuff that's serialized. serialization is just for storage/transit purposes. – vancoder Mar 7 '13 at 18:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.