Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PHP: What's a better way to process form data?
- function update_account() {
- global $current_user; get_currentuserinfo();
- require_once( ABSPATH . WPINC . '/registration.php' );
- $uid = $current_user->ID;
- // First Name
- if(isset($_POST['first_name']) && $_POST['first_name'] <> $current_user->first_name) {
- wp_update_user( array(
- 'ID' => $uid, 'first_name' => esc_attr($_POST['first_name'])
- ));
- }
- // ...and so on 43 more times...
- }
- // All the fields you wish to process are in this array
- $fields = array('first_name', 'last_name', 'others',...'others99');
- // Loop over the array and process each field with the same block
- foreach ($fields as $field) {
- if(isset($_POST[$field]) && $_POST[$field] != $current_user->{$field}) {
- wp_update_user( array(
- 'ID' => $uid, $field => esc_attr($_POST[$field])
- ));
- }
- }
- function update_account()
- {
- // initialize a new form class
- $form = new UserAccountInfoForm();
- // give the form to your view for rendering
- $this->view->form = $form;
- // check if form was posted [however your framework provides this check]
- if(!Is_Post())
- return $this->render('accountform.phtml');
- // check if posted form data validates
- if(!$form->isValid($_POST))
- {
- // if the form didn't validate re-display the form
- // the view takes care of displaying errors, with the help of its
- // copy of the $form object
- return $this->render('accountform.phtml');
- }
- // form validated, so we can use the supplied values and update the db
- $values = $form->getValues(); // returns an array of ['fieldname'=>'value']
- // escape the values of the array
- EscapeArrayValues($values);
- // update db
- wp_update_user($values);
- // inform the user of successful update via flash message
- $this->flashMessage('Successfully updated profile');
- // go back to main profile page
- $this->redirect('/profile');
- <form action='post'>
- <label>first name</label> <input class='<?=$this->form->getElement('first_name')->hasError() ? "invalid":""?>' type='text' name='first_name' value="<?=$this->form->getElement('first_name')->getValue()"/> <span class='errmsg'><?=$this->form->getElement('first_name')->getError()?></span><br/>
- <label>last name</label> <input class='<?=$this->form->getElement('last_name')->hasError() ? "invalid":""?>' type='text' name='last_name' value="<?=$this->form->getElement('last_name')->getValue()"/> <span class='errmsg'><?=$this->form->getElement('last_name')->getError()?></span><br/>
- <label>other</label> <input class='<?=$this->form->getElement('other')->hasError() ? "invalid":""?>' type='text' name='other' value="<?=$this->form->getElement('other')->getValue()"/> <span class='errmsg'><?=$this->form->getElement('other')->getError()?></span><br/>
- <input type='submit' value='submit'/>
- </form>
Add Comment
Please, Sign In to add comment