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

<p><strong>Update:</strong></p>

<p>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.</p>

<p>In a non-AJAX context, a call to <code>$this-&gt;redirectToPostedUrl();</code> will do the redirect for you.</p>

CC BY-SA 3.0
2015-04-22T12:48:04.413Z
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
CC BY-SA 3.0
2015-04-22T12:51:13.057Z
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
CC BY-SA 3.0
2015-04-22T20:40:28.373Z
I feel like I'm missing something... can you share the entire controller method?
Brad Bell
CC BY-SA 3.0
2015-04-23T13:00:51.907Z
Brad. Thanks for your interest. Please see my update above for the code.
Matt V
CC BY-SA 3.0
2015-04-23T16:57:27.997Z
Updated my answer.
Brad Bell
CC BY-SA 3.0
2015-04-23T17:54:27.260Z
Hey Brad, thanks for the reply. I am updating the DOM after the AJAX call is returned. Let me take a step back a recap all of this. See my new update above.
Matt V
CC BY-SA 3.0
2016-05-14T09:05:34.247Z
I can confirm that i am also experiencing the same issue. I have a plain text field which i update, i then immediately afterwards query the user using getUserById, the field i updated is still set to the old one. Yet when i view the field in Craft CP, i can see the new value. Only after a page refresh that it takes the new value. Its like getUserById is working from a cache rather than querying the database directly.
BillyMedia
CC BY-SA 3.0
2015-04-21T22:23:41.980Z
2015-04-23T16:57:21.543Z
2015-04-23T16:57:21.543Z
Brad Bell
<p>I have a very simple plugin to update a user's name. All I'm doing is:</p>

<pre><code>    $userId = craft()-&gt;userSession-&gt;id;

    $user = craft()-&gt;users-&gt;getUserById($userId);

    $post = craft()-&gt;request-&gt;getPost();

    $user-&gt;firstName = $post["firstName"];

    craft()-&gt;users-&gt;saveUser($user);
</code></pre>

<p>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?</p>

<p><strong>UPDATE</strong></p>

<p>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.</p>

<p><strong>UPDATE</strong></p>

<p>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:</p>

<pre><code>public function actionEditContactInfo()
{
    $userId = craft()-&gt;userSession-&gt;id;

    $user = craft()-&gt;users-&gt;getUserById($userId);

    $post = craft()-&gt;request-&gt;getPost();

    $user-&gt;firstName = $post["firstName"];

    craft()-&gt;users-&gt;saveUser($user);
}
</code></pre>

<p>For AJAX calls I'm doing a little bit more:</p>

<pre><code>public function actionEditContactInfo()
{
    $userId = craft()-&gt;userSession-&gt;id;

    $user = craft()-&gt;users-&gt;getUserById($userId);

    $post = craft()-&gt;request-&gt;getPost();

    $user-&gt;firstName = $post["firstName"];

    craft()-&gt;users-&gt;saveUser($user);

    $editUser = $user-&gt;getContent();

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

    $this-&gt;returnJson(array('template' =&gt; $template ));
}
</code></pre>

<p>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.</p>

<p><strong>UPDATE</strong></p>

<p>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:</p>

<p>I have a form with 2 fields:</p>

<ul>
<li>A category field</li>
<li>A plain text field</li>
</ul>

<p>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.</p>

<p>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. </p>

<p>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:</p>

<pre><code>public function actionEditNetworks()
{

    $userId = craft()-&gt;userSession-&gt;id;

    $user = craft()-&gt;users-&gt;getUserById($userId);

    $user-&gt;setContentFromPost('fields');

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

    $editUser = $user-&gt;getContent();

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

    $this-&gt;returnJson(array('template' =&gt; $template));
}
</code></pre>

CC BY-SA 3.0
2015-04-21T19:39:53.113Z
Can you share your template code that has the form in it?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-21T19:52:37.737Z
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
<plugin-development><cache>
Possible cache issue? Not sure
# Possible cache issue? Not sure

(Asked by: username_0 on 2015-04-21)

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));
}

**Tags:** plugin-development, cache

> **Comments:**
> **username_1**: Can you share your template code that has the form in it?  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_2 on 2015-04-21. Score: 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.

> **Comments:**
> **username_0**: 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.  
> **username_0**: 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.  
> **username_2**: I feel like I'm missing something... can you share the entire controller method?  
> **username_0**: Brad. Thanks for your interest. Please see my update above for the code.  
> **username_2**: Updated my answer.  
> **username_0**: Hey Brad, thanks for the reply. I am updating the DOM after the AJAX call is returned. Let me take a step back a recap all of this. See my new update above.  
> **username_3**: I can confirm that i am also experiencing the same issue. I have a plain text field which i update, i then immediately afterwards query the user using getUserById, the field i updated is still set to the old one. Yet when i view the field in Craft CP, i can see the new value. Only after a page refresh that it takes the new value. Its like getUserById is working from a cache rather than querying the database directly.  

---

