Connecting Tech Pros Worldwide Forums | Help | Site Map

Refresh or close after form submission

Familiar Sight
 
Join Date: Sep 2007
Posts: 221
#1: Nov 7 '08
I have a page that I want to be refreshed after a user submits a form. I also have another page that I want to be closed after a user submits a form.

How would I go about doing this??

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Nov 7 '08

re: Refresh or close after form submission


could you explain that a bit more? what do you mean with closing a page - do you mean closing a window that displays a page? when you submit what happens then - do you call a php script that you control?

kind regards
Familiar Sight
 
Join Date: Sep 2007
Posts: 221
#3: Nov 10 '08

re: Refresh or close after form submission


I have a popup form that's used to submit comments. When the user clicks the submit button, I'd like the popup window to close once the submission takes place.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#4: Nov 10 '08

re: Refresh or close after form submission


just call a js-function onsubmit of your form that uses window.close()

kind regards
Familiar Sight
 
Join Date: Sep 2007
Posts: 221
#5: Nov 10 '08

re: Refresh or close after form submission


Quote:

Originally Posted by gits

just call a js-function onsubmit of your form that uses window.close()

kind regards

Excellent, I'll give that a try
Familiar Sight
 
Join Date: Sep 2007
Posts: 221
#6: Nov 12 '08

re: Refresh or close after form submission


How come if I try to do this:

onSubmit="window.location.reload();" in my form tag it's not submitting the form, it's just refreshing the screen. I want the screen to refresh after the form gets submitted to allow the user to enter a new form without having to jump between screens.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Nov 13 '08

re: Refresh or close after form submission


onsubmit is called as soon as the submit button is pressed, not after the submit takes place. Add the refresh to the submitted page instead.
Newbie
 
Join Date: Jun 2009
Posts: 20
#8: Jun 10 '09

re: Refresh or close after form submission


I have the same problem as I want to make the submit button refresh.

I have 3 frames on my page and need the page to refresh after a submit button is pressed for its changes to take effect (change style sheet)

However using


<input type="submit" onSubmit="window.location.reload();" value="Go">just submits and does not refresh.

I am using the http://php.about.com/od/finishedphp1...s_switcher.htm script.

Does anyone know how to make the button refresh the page as well?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Jun 11 '09

re: Refresh or close after form submission


onsubmit is a form event, not an input button event. In any case, that wouldn't work either. I assume the form is in a frame and the form submits to the same frame. What you'd need to do is refresh the whole page after the page has been submitted.
Newbie
 
Join Date: Jun 2009
Posts: 20
#10: Jun 11 '09

re: Refresh or close after form submission


Yes, I don't want my visitors to have to manualy do that.

I was thinking of putting js into my cookie.php which is what the form is linked to.
But don't know what to add?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#11: Jun 11 '09

re: Refresh or close after form submission


You'd need to reference the parent using 'parent' or 'top' and then reload. It is possible to change styles without reloading the page. One other thing: if possible, try to avoid frames.
Newbie
 
Join Date: Jun 2009
Posts: 20
#12: Jun 11 '09

re: Refresh or close after form submission


I tried targeting it with _Parent but it just loaded the same frame in the frame that was suppost to refresh.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#13: Jun 11 '09

re: Refresh or close after form submission


Do you only want to reload one frame or the page which contains the 3 frames?
Newbie
 
Join Date: Jun 2009
Posts: 20
#14: Jun 11 '09

re: Refresh or close after form submission


just 1 other frame would do, but either. If its possible to reload the whole page then i'd prefer that.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#15: Jun 12 '09

re: Refresh or close after form submission


To access the parent (the page containing the frames) and reload it:
Expand|Select|Wrap|Line Numbers
  1. parent.location.reload();
You would call this after page load in one of the frames. Is there any particular reason why you're using frames?
Newbie
 
Join Date: Jun 2009
Posts: 20
#16: Jun 12 '09

re: Refresh or close after form submission


Where would I put that thought.

Its part of the design.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#17: Jun 12 '09

re: Refresh or close after form submission


You can put it in the form action page:
Expand|Select|Wrap|Line Numbers
  1. window.onload = function() {
  2.     parent.location.reload();
  3. }
Newbie
 
Join Date: Jun 2009
Posts: 20
#18: Jun 13 '09

re: Refresh or close after form submission


Thank you very much.
Thats the one.. nearly, as this runs the whole page in a loop :P
I just added
Expand|Select|Wrap|Line Numbers
  1. framename.document.
  2.  
So the whole code reads

Expand|Select|Wrap|Line Numbers
  1. window.onload = function() { 
  2.     parent.framename.document.location.reload(); 
  3.  
Once again, thank you very much for your help, I do appreciate it. :)
Newbie
 
Join Date: Jun 2009
Posts: 20
#19: Jun 13 '09

re: Refresh or close after form submission


The only problem I have is that it makes the target frame a bit jumpy when it loads as it has to load twice the first time round (initial load, and refresh).

So is it possible to only run the script when the button is submitted?
I have tried the following but it did not work.
Expand|Select|Wrap|Line Numbers
  1. <input type="submit" value="Go" onSubmit="parent.framename.document.location.reload();">
  2.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#20: Jun 14 '09

re: Refresh or close after form submission


Use PHP code to decide if the code should exist. Only have the JavaScript code run if the form has been submitted, e.g.
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     if (isset(...)) {
  3. ?>
  4. <script type="text/javascript">
  5. ...
  6. </script>
  7. <?php
  8.     }
  9. ?>
Newbie
 
Join Date: Jun 2009
Posts: 20
#21: Jun 21 '09

re: Refresh or close after form submission


It does not refresh when clicked, what have I done wrong?

This is the code in the form frame.

Expand|Select|Wrap|Line Numbers
  1. <?php  
  2.     if (isset($var)) { 
  3. ?> 
  4. <script type="text/javascript"> 
  5. $var window.onload = function() {  
  6.     parent.frameName.document.location.reload();  
  7. }  
  8. </script> 
  9. <?php 
  10.     } 
  11. ?>
  12.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#22: Jun 21 '09

re: Refresh or close after form submission


No, it doesn't quite work like that. You need to replace $var with the posted variable in PHP, e.g. $_POST['submit']. Also remove $var on line 5. If you have any more problems on that front, ask in the PHP forum.
Reply