Guest User

Untitled

a guest
Aug 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. PHP: What's a better way to process form data?
  2. function update_account() {
  3. global $current_user; get_currentuserinfo();
  4. require_once( ABSPATH . WPINC . '/registration.php' );
  5.  
  6. $uid = $current_user->ID;
  7.  
  8. // First Name
  9. if(isset($_POST['first_name']) && $_POST['first_name'] <> $current_user->first_name) {
  10. wp_update_user( array(
  11. 'ID' => $uid, 'first_name' => esc_attr($_POST['first_name'])
  12. ));
  13. }
  14.  
  15. // ...and so on 43 more times...
  16.  
  17. }
  18.  
  19. // All the fields you wish to process are in this array
  20. $fields = array('first_name', 'last_name', 'others',...'others99');
  21.  
  22. // Loop over the array and process each field with the same block
  23. foreach ($fields as $field) {
  24. if(isset($_POST[$field]) && $_POST[$field] != $current_user->{$field}) {
  25. wp_update_user( array(
  26. 'ID' => $uid, $field => esc_attr($_POST[$field])
  27. ));
  28. }
  29. }
  30.  
  31. function update_account()
  32. {
  33. // initialize a new form class
  34. $form = new UserAccountInfoForm();
  35. // give the form to your view for rendering
  36. $this->view->form = $form;
  37. // check if form was posted [however your framework provides this check]
  38. if(!Is_Post())
  39. return $this->render('accountform.phtml');
  40. // check if posted form data validates
  41. if(!$form->isValid($_POST))
  42. {
  43. // if the form didn't validate re-display the form
  44. // the view takes care of displaying errors, with the help of its
  45. // copy of the $form object
  46. return $this->render('accountform.phtml');
  47. }
  48.  
  49. // form validated, so we can use the supplied values and update the db
  50. $values = $form->getValues(); // returns an array of ['fieldname'=>'value']
  51. // escape the values of the array
  52. EscapeArrayValues($values);
  53. // update db
  54. wp_update_user($values);
  55. // inform the user of successful update via flash message
  56. $this->flashMessage('Successfully updated profile');
  57. // go back to main profile page
  58. $this->redirect('/profile');
  59.  
  60. <form action='post'>
  61. <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/>
  62.  
  63. <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/>
  64.  
  65. <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/>
  66.  
  67. <input type='submit' value='submit'/>
  68. </form>
Add Comment
Please, Sign In to add comment