1

I have a very simple plugin to update a user's name. All I'm doing is:

    $userId = craft()->userSession->id;

    $user = craft()->users->getUserById($userId);

    $post = craft()->request->getPost();

    $user->firstName = $post["firstName"];

    craft()->users->saveUser($user);

The problem I'm having is after the page reloads from the POST submit the output still shows the old name, but then if I refresh the page without resubmitting I see the new name. Point is the name is changing but it seems like maybe this data is cached somewhere? How do I fix this?

UPDATE

To be more clear, this does not happen on all field types. First name and last name do it. Custom plain text fields do not. Category fields do it.

UPDATE

Here is the code I'm using in one of my controllers. This will not be the final code but I don't feel like that matters. Just these few lines will produce the problem I'm talking about:

public function actionEditContactInfo()
{
    $userId = craft()->userSession->id;

    $user = craft()->users->getUserById($userId);

    $post = craft()->request->getPost();

    $user->firstName = $post["firstName"];

    craft()->users->saveUser($user);
}

For AJAX calls I'm doing a little bit more:

public function actionEditContactInfo()
{
    $userId = craft()->userSession->id;

    $user = craft()->users->getUserById($userId);

    $post = craft()->request->getPost();

    $user->firstName = $post["firstName"];

    craft()->users->saveUser($user);

    $editUser = $user->getContent();

    $template = craft()->templates->render("users/profile-form/_contact-info-form", array("editUser" => $editUser));

    $this->returnJson(array('template' => $template ));
}

In both cases not all fields display the updated content after a form submit is made. Custom plain text fields seem to be ok. The default user model first name and last name are problematic as are category fields. Not sure about other field types.

UPDATE

It has been brought to my attention that I should probably be more clear in my explanation of this problem. Here it goes from top to bottom:

I have a form with 2 fields:

  • A category field
  • A plain text field

The form submits data to the server by making an AJAX call to a controller. The controller updates the info as needed then renders a template representing the updated form. The template is then encapsulated in a JSON array and passed to a JavaScript function which updates the user's browser.

The problem I'm experiencing is not that the data is not saving, but that when the new form is re-rendered not all of the fields reflect the changes. But if I refresh the page after saving everything is fine.

Of the two fields mentioned above, plain text works as expected but the category field is problematic. Even if I dump() the user data in the re-rendered template I am given the old value for the category field even though it was updated. Here is my controller code:

public function actionEditNetworks()
{

    $userId = craft()->userSession->id;

    $user = craft()->users->getUserById($userId);

    $user->setContentFromPost('fields');

    // Save the user
    $result = craft()->users->saveUser($user);

    $editUser = $user->getContent();

    $template = craft()->templates->render("users/profile-form/_network-links-form", array("editUser" => $editUser));

    $this->returnJson(array('template' => $template));
}
  • Can you share your template code that has the form in it? – Aaron Berkowitz Apr 21 '15 at 19:39
  • Really all I'm doing is this: {{ user.firstName }}. It's pretty basic. I'm not doing anything out of the ordinary. Not using cache tags at all. – Matt V Apr 21 '15 at 19:52
1

If I had to guess, you need to add a $this->redirectToPostedUrl(); after your call to craft()->users->saveUser($user); to cause a proper redirect/page refresh after submission.

Update:

If you're doing this over AJAX, then you're going to be responsible for updating any DOM elements with their new values, or reloading/redirecting the page so they'll display.

In a non-AJAX context, a call to $this->redirectToPostedUrl(); will do the redirect for you.

| improve this answer | |
  • I thought about doing what you suggested myself, but that was before all of my form submits needed to become AJAX calls. Even with AJAX this is happening. – Matt V Apr 22 '15 at 12:48
  • Oh and to be more clear, this does not happen on all field types. First name and last name do it. Custom plain text fields do not. Category fields do it. – Matt V Apr 22 '15 at 12:51
  • I feel like I'm missing something... can you share the entire controller method? – Brad Bell Apr 22 '15 at 20:40
  • Brad. Thanks for your interest. Please see my update above for the code. – Matt V Apr 23 '15 at 13:00
  • Updated my answer. – Brad Bell Apr 23 '15 at 16:57

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.