1

I'm having trouble figuring out what's going wrong with my code. I created a meta box for a custom post type (project) that adds people to the project via the edit project page. The box also displays the users who have been added to the projects, but the POST method doesn't seem to be working so none of the code that inserts the data into the database occurs. I tried to echo a value from the form but there is no result so I know the post method is not working. I checked for syntax errors as well.

Here is the code for the POST method once form is submitted -

<?php
// Process add form if sent
          if(isset($_POST['mro_add_user'])) {
              $_POST = stripslashes_deep($_POST);
              $add_username = $_POST['mro_add_user'];
              if (username_exists($add_username) != NULL) {
                // Check not already registered
                $add_userid = username_exists($add_username);
                $extra_userdata = get_userdata($add_userid);
                $add_firstname = $extra_userdata->first_name;
                $add_lastname =  $extra_userdata->last_name;
                $a_registered = $wpdb->get_var("SELECT user_id FROM ".$wpdb->prefix."mro_attendees WHERE event_id = ".$project_id." AND user_id = ".$add_userid);
                // If not already registered
                if ($a_registered == "") {
                    $user_comment = $_POST['mro_add_comment'];
                    $wpdb->insert($wpdb->prefix.'mro_attendees', array( 'event_id' => $project_id, 'user_id' => $add_userid, 'first_name' => $add_firstname, 'last_name' => $add_lastname, 'user_comment' => $user_comment ) );
                    ?>
                    <div class="updated"><p><strong><?php _e('User ' . $add_username . ' added.' ); ?></strong></p></div>
                    <?php
                } else {
                    ?>
                    <div class="updated"><p><strong><?php _e('User ' . $add_username . ' already on list.' ); ?></strong></p></div>
                    <?php
                }
            } elseif(username_exists($add_username) == NULL) {
                ?>
                <div class="updated"><p><strong><?php _e('User ' . $add_username . ' not found.' ); ?></strong></p></div>
                <?php
            }

          }
          ?>

And here is the form that has the action both of these are in the same code document.

<form id="add_user" name="add_user" method="post" action="post.php?post=<?php echo $project_id; ?>&action=edit">
            <p>
              <label for="mro_add_user"></label>
              Username<br />
  <input name="mro_add_user" type="text" id="mro_add_user" size="40" maxlength="150" />
            </p>
            <p>Comment<br />
              <label for="mro_add_comment"></label>
              <input name="mro_add_comment" type="text" id="mro_add_comment" size="40" maxlength="40" />
</p>
            <p>
              <input type="submit" name="mro_add_user_submit" id="mro_add_user_submit" value="Add User" />
            </p>
  </form>

I have a GET method for something else in this code that does work.

<a href="post.php?post=<?php echo $project_id; ?>&action=edit&remove_attendee=<?php echo $user->id; ?>&place=<?php echo $num; ?>">Remove User</a>

I'm just confused as to why the GET method works but POST doesn't? Is this something that meta box can't do? Is there another way I can do this in the meta box? Any help or insight would be greatly appreciated!

1 Answer 1

0

Firstly, have you inspected the meta box html from the browser?

You may find that the form element that you have above is missing. This is because you cannot have nested form elements:

<form>
      <form>
      </form>
</form>

All of the meta boxes are already inside of a form element, so you cannot specify another form element inside.

One solution would be to use a Javascript AJAX function to submit your adding of usernames:

Javascript Part:

            jQuery.ajax({
                url: 'http://yoursite.com/wp-admin/admin-ajax.php',
                dataType: 'json',
                //type:'POST',
                data: {
                   'action':'your_ajax',
                   'fn': 'add_users_to_project',
                   'data': data
                   },
                success: function(results){
                    //console.log(results);
                    //display your added usernames
                },// end of success
                error: function(errorThrown){console.log(errorThrown);}
            });// end of ajax

PHP part:

class your_ajax{

//add actions and filters in constructor
function __construct(){

    //admin or front end?
    //add_action('wp_ajax_nopriv_your_ajax', array( $this, 'ajax_function' ) );
    add_action('wp_ajax_your_ajax', array( $this, 'ajax_function' ) );

}

//handle ajax calls from 
function ajax_function(){
   // the first part is a SWTICHBOARD that fires specific functions
   // according to the value of Query Var 'fn'

    //feel free to use this file for any wordpress ajax that needs to be done
    switch($_REQUEST['fn']){

        case 'add_users_to_project':
            //$output = $_REQUEST['data'];
            $output = $this->add_users_to_project( $_REQUEST['data'] );
        break;

        case 'delete_users_from_project':
            //$output = $_REQUEST['data'];
            $output = $this->delete_users_from_project( $_REQUEST['data'] );
        break;

        default:
            $output = 'No function specified, check your jQuery.ajax() call';
        break;
    }

   // Now, convert $output to JSON and echo it to the browser
   // That way, we can recapture it with jQuery and run our success function
        ob_clean();
        $output=json_encode($output);
        if(  is_array( $output )  ):

            print_r( $output );
        else:

            echo $output;
        endif;
        die();

}


function add_users_to_project( $data ){
    //do whatever
    return $response;
}

function delete_users_from_project( $data ){
    //delete whatever
    return $response;
}


}//end your_ajax class

$your_ajax = new your_ajax();

Another option would be to add a function to the save_post action hook which will fire when the project post is saved. Then simply display all the users in your meta box.

add_action( 'save_post', 'your_save_function' );
function your_save_function(){

//do stuff with global $post and $_POST

} 

Sorry for the long answer...Hope you find a solution suitable to your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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