# AJAX to submit form, but reload the whole page

**URL:** <https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647>\
**Category:** Rails\
**Created:** [January 5, 2006, 2:47am UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647 "2006-01-05T02:47:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 5, 2006, 2:47am UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/1 "2006-01-05T02:47:09Z")

</div>

I’m using ROR 1.0 and the included prototype.js to do some AJAX stuff.

I’m using the form\_remote\_tag to submit a login form using AJAX.  
So, any error/failed login will be displayed using AJAX. But, for  
successful login, I would want it to reload the wholepage, not just  
update the

.

How can I achieve this?

---

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 5, 2006, 3:18am UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/2 "2006-01-05T03:18:47Z")

</div>

On 1/4/06, ngising [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m using ROR 1.0 and the included prototype.js to do some AJAX stuff.
> 
> I’m using the form\_remote\_tag to submit a login form using AJAX.  
> So, any error/failed login will be displayed using AJAX. But, for  
> successful login, I would want it to reload the wholepage, not just  
> update the
> 
> .
> 
> How can I achieve this?

change the page with javascript. In the :success callback of your  
form\_remote\_tag, do something like “location.href = ‘foo’”

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 5, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/3 "2006-01-05T13:54:19Z")

</div>

ok I got it…

render :nothing =\> true, :status =\> 401

---

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 5, 2006, 1:06pm UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/4 "2006-01-05T13:06:09Z")

</div>

> change the page with javascript. In the :success callback of your  
> form\_remote\_tag, do something like “location.href = ‘foo’”

But what about the failure case ? Should I set a response of HTTP to  
something other than 200 in the controller?so that I can handle it via  
the  
failure callback of the Ajax class in the javascript?

---

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 5, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/5 "2006-01-05T14:19:18Z")

</div>

Hi Carlos,  
Thanks for the example…Thats what I was looking for .Just one small  
thing.  
Whats the false parameter you pass to window.location.reload(). I  
actually  
want to do something like redirecting to the user’s homepage if the  
password/username is correct.  
So I suppose something like this should work.  
document.href=’/matters\_crud/home’

Vivek

---

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 5, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/6 "2006-01-05T14:00:21Z")

</div>

Hi Vivek

I think the solution you are looking for is what Jamis B. teached me a  
few  
weeks ago:  
…

When I’ve needed something like this, I will typically manage things  
manually. Instead of specifying the :update element, I will handle  
all of that in a :complete handler by hand. The :complete handler  
checks the value of a custom header (‘X-What-To-Do’, for example). If  
the header is ‘error’, then I update the ‘ajax\_div’ element with the  
request.responseText data. If the header is ‘ok’, then I execute  
window.location.reload(false).

Does that make sense? If not, let me know and I can post a more  
detailed example.

- Jamis  
…

here’s a snippet from the hypothetical RHTML file:

 \<%= form\_remote\_tag :complete=\>'handleComplete(request)', :url=\>{:controller=\>'matters\_crud', :action =\>:update, :id =\> @matter} %\> \<%= render\_partial 'form' %\> \<%= submit\_tag 'Edit' %\> 

Then, in your controller, you just do:

def update  
@matter = Matter.find(params[:id])  
if @matter.update\_attributes(params[:matter])  
headers[‘X-Instruction’] = “ok”  
render :nothing =\> true  
else  
headers[‘X-Instruction’] = “error”  
render :partial =\> ‘error’  
end  
end

- Jamis  
…

Hope that helps

Okada.

2006/1/5, Vivek K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 29, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/7 "2006-01-29T16:00:11Z")

</div>

Hi all:

I’m doing something similar to this but for an account signup  
controller. So once the user successfully creates an account, the user  
is redirected to index.rhtml by

window.location.href = “[http://localhost:3000](http://localhost:3000)”;

I have a problem where the session hash gets deleted on the first time  
through the process, but not deleted on subsequent times. I’d like to  
preserve session[:user\_id] from a successful account creation/login, but  
it never works the first time. I can see session[:user\_id] = @user.id  
being set in irb, but after the redirect session[:user\_id] = nil. And  
as previously stated, this works when I create a new account during the  
same browser session.

Has anyone else had this problem?

Boram

---

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 30, 2006, 1:13am UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/8 "2006-01-30T01:13:29Z")

</div>

I would have thought that RJS provides a much cleaner solution.

update.rjs:

if @matter.valid?  
page \<\< ‘location.reload(false);’  
else  
page.replace\_html ‘ajax\_div’, :partial =\> ‘error’  
end

-Jonny.

---

<div class="post-metadata">

**Author:** ![ngising](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@ngising](https://www.ruby-forum.com/u/ngising)\
**Post date:** [January 29, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/ajax-to-submit-form-but-reload-the-whole-page/51647/9 "2006-01-29T16:02:53Z")

</div>

Clarification:

> as previously stated, this works when I create a new account during the  
> same browser session.

should read

> as previously stated, this works when I subsequently create new accounts during \> the same browser session.
