Click submit button without refresh

S

SamuelXiao

Hi,

I am trying to submit a form in a parent window by using its child
window.
Given there are a parent window and a child window, child window has
code like this:
function submitOpenerForm(){
var ope = window.opener.document.getElementById("input1").value =
"hardcode";
window.opener.document.getElementById("btn1").click();
window.close();
}

The code works for submitting but the parent window always refresh
after that, my question is, how can it click the button, processing
the form submit, but the parent page will not refresh?
For some reason, I can't use window.opener.document.getElementById
('form1').sumbit() bla bla to submit that form.

I know Ajax can submit form without refresh the page, but it needs a
url, and those data, my case is that, I am not sure those data are (or
are from which fields,input,so on), but I do know there is an input
element needed to be assigned value and there is only a submit
button.

Is there any way I can prevent it refreshing the page after click()?
 
E

Erwin Moller

SamuelXiao schreef:
Hi,

I am trying to submit a form in a parent window by using its child
window.
Given there are a parent window and a child window, child window has
code like this:
function submitOpenerForm(){
var ope = window.opener.document.getElementById("input1").value =
"hardcode";
window.opener.document.getElementById("btn1").click();
window.close();
}

The code works for submitting but the parent window always refresh
after that, my question is, how can it click the button, processing
the form submit, but the parent page will not refresh?
For some reason, I can't use window.opener.document.getElementById
('form1').sumbit() bla bla to submit that form.

I know Ajax can submit form without refresh the page, but it needs a
url, and those data, my case is that, I am not sure those data are (or
are from which fields,input,so on), but I do know there is an input
element needed to be assigned value and there is only a submit
button.

Is there any way I can prevent it refreshing the page after click()?

Hi,

Did you put a target="nameofchildwindow" in the form on your parentpage?

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
S

SamuelXiao

SamuelXiao schreef:








Hi,

Did you put a target="nameofchildwindow" in the form on your parentpage?

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

No, for calling child window in the parent, I use a function like the
following:

function mypopup()
{
mywindow = window.open ("xxx.jsp",
"mywindow","width=100,height=100");
mywindow.moveTo(0,0);
}

when click a button "popup";
 
S

SAM

Le 11/3/09 3:14 PM, SamuelXiao a écrit :
Hi,

I am trying to submit a form in a parent window

It's not exactly a "parent".
by using its child window.
Given there are a parent window and a child window, child window has
code like this:
function submitOpenerForm(){

if(window.opener) {
var ope = window.opener.document.getElementById("input1").value =
"hardcode";

window.opener.document.getElementById("input1").value = "hardcode";
window.opener.document.getElementById("btn1").click();
window.close();
}

}

The code works for submitting but the parent window always refresh

Normal, it's the default behavior.

In the parent :

<form id="form1" name="form1" target="other" action="mytest.php">

that will send the form in a new window (same window at each new try)

Notice: that way of doing is deprecated (If I'm not in error)
after that, my question is, how can it click the button, processing
the form submit, but the parent page will not refresh?
For some reason, I can't use window.opener.document.getElementById
('form1').sumbit() bla bla to submit that form.

The form has it an ID ?
(and that id is named 'form1' ?)

Try :
document.forms['form1'].submit();
instead, if the form has only a name.
I know Ajax can submit form without refresh the page, but it needs a
url, and those data, my case is that, I am not sure those data are (or
are from which fields,input,so on), but I do know there is an input
element needed to be assigned value and there is only a submit
button.

You don't need a physical submit button when you do :
document.forms[0].submit()
Is there any way I can prevent it refreshing the page after click()?

The file called by the action of the form must be send somewhere :
- in same window (default)
- a new window ( target="_blank" -or- target="noname" -or- JS function)
- a frame or iframe

Or you avoid the action :
<form onsubmit="return false;" blah... >
but it isn't your purpose, no ?


function submitOpenerForm(){
if(window.opener) {
var ope = window.opener.document.forms["form1"];
ope.elements["input1"].value = "hardcode";
ope.target = 'other';
ope.submit();
window.opener.focus(); // probably not useful
self.close();
}
else alert('Mother is closed');
}
 
S

SAM

Le 11/3/09 4:00 PM, SamuelXiao a écrit :
No, for calling child window in the parent, I use a function like the
following:

there is no importance the way you call your popup
(for the mother's form submitting)

It what asked if the form of the *opener* has an attribute 'target' !
Has it ?
function mypopup()
{
mywindow = window.open ("xxx.jsp",
"mywindow","width=100,height=100");
mywindow.moveTo(0,0);
}

when click a button "popup";

function mypopup() {
if(typeof mywindow == undefined || mywindow.closed)
mywindow=window.open("xxx.jsp","mywindow","width=100,height=100,top=0,left=0");
mywindow.focus();
}
 
E

Erwin Moller

SamuelXiao schreef:
No, for calling child window in the parent, I use a function like the
following:

function mypopup()
{
mywindow = window.open ("xxx.jsp",
"mywindow","width=100,height=100");
mywindow.moveTo(0,0);
}

when click a button "popup";

Hi,

As SAM wrote: That isn't important.
What is important is where the form should be posted.
If you don't give a target for the form, the form ALWAYS posts to the
window that contains that form.
If you want it to post somewhere else (or better formulated: "If you
want the response from the server-that-processes-the-form to appear
somewhere else") you SHOULD use target.
The value you give to target should be the name of that window/frame.

Mind you that using a target is a sin if you use strict doctype.

Regards,
Erwin Moller



--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
S

SamuelXiao

Le 11/3/09 3:14 PM, SamuelXiao a écrit :
I am trying to submit a form in a parent window

It's not exactly a "parent".
by using its child window.
Given there are a parent window and a child window, child window has
code like this:
function submitOpenerForm(){

if(window.opener) {
   var ope = window.opener.document.getElementById("input1").value =
"hardcode";

window.opener.document.getElementById("input1").value = "hardcode";
   window.opener.document.getElementById("btn1").click();
   window.close();

    }
The code works for submitting but the parent window always refresh

Normal, it's the default behavior.

In the parent :

<form id="form1" name="form1" target="other" action="mytest.php">

that will send the form in a new window (same window at each new try)

Notice: that way of doing is deprecated (If I'm not in error)
after that, my question is, how can it click the button, processing
the form submit, but the parent page will not refresh?
For some reason, I can't use window.opener.document.getElementById
('form1').sumbit() bla bla to submit that form.

The form has it an ID ?
(and that id is named 'form1' ?)

Try :
    document.forms['form1'].submit();
instead, if the form has only a name.
I know Ajax can submit form without refresh the page, but it needs a
url, and those data, my case is that, I am not sure those data are (or
are from which fields,input,so on), but I do know there is an input
element needed to be assigned value and there is only a submit
button.

You don't need a physical submit button when you do :
   document.forms[0].submit()
Is there any way I can prevent it refreshing the page after click()?

The file called by the action of the form must be send somewhere :
- in same window (default)
- a new window ( target="_blank" -or- target="noname" -or- JS function)
- a frame or iframe

Or you avoid the action :
   <form onsubmit="return false;" blah... >
but it isn't your purpose, no ?

function submitOpenerForm(){
  if(window.opener) {
     var ope = window.opener.document.forms["form1"];
     ope.elements["input1"].value = "hardcode";
     ope.target = 'other';
     ope.submit();
     window.opener.focus(); // probably not useful
     self.close();
     }
  else alert('Mother is closed');

}

Actually, in my case, the opener window doesn't have a form element
(no <form>). For submitting data, it has buttonhandler (a javascript
function) for those button (submit button). So "target" is not able to
be used in the opener window's "form".

But the way, thanks for your idea, now I make a copy of the opener
window's input fields in the "child" window, set it in a form, then
submit by using target, but it will popup another window "other", then
close. Is there anyway not allowing a new window popup by
target="other"? I am not allowed to use iframe.
 
S

SamuelXiao

SamuelXiao schreef:









Hi,

As SAM wrote: That isn't important.

What is important is where the form should be posted.
If you don't give a target for the form, the form ALWAYS posts to the
window that contains that form.
If you want it to post somewhere else (or better formulated: "If you
want the response from the server-that-processes-the-form to appear
somewhere else") you SHOULD use target.
The value you give to target should be the name of that window/frame.

Mind you that using a target is a sin if you use strict doctype.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare- Hide quoted text -

- Show quoted text -

Hi, do you know how I can submit to a target that will not popup a new
window and not the current window? Iframe seems to be the solution,
but I am not allowed to use it.
 
E

Erwin Moller

SamuelXiao schreef:
Hi, do you know how I can submit to a target that will not popup a new
window and not the current window? Iframe seems to be the solution,
but I am not allowed to use it.

Hi,

A hidden frame maybe?
Make some frameset, make 1 window in there 0 pixels height, let the
other be your current window, and post to the hidden one.
It is an old solution.
That is how I communicated with a server without a pagereload in the
days before XMLHTTPrequest (AJAX). ;-)

In case you are not allowed to use frames/Iframes and no new windows, I
think you must use a XMLHTTPRequest.
If that is impossible too, well you'll have to resort to things like
Java-applets maybe.

What is it excatly you are trying to achieve?
Can't you simply use XMLHTTPRequest object instead?


Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
S

SAM

Le 11/4/09 1:30 PM, SamuelXiao a écrit :
Actually, in my case, the opener window doesn't have a form element
(no <form>).

So, on my idea, you can't 'click' that button by JS.
(only cickable elements of forms are JS clickable)

For submitting data, it has buttonhandler (a javascript
function) for those button (submit button). So "target" is not able to
be used in the opener window's "form".

Why won't you launch directly that function declared in the mother,
instead ?

if(opener &&
opener.myFunction)
opener.myFunction(document.getElementById('myValue').value);

But the way, thanks for your idea, now I make a copy of the opener
window's input fields in the "child" window, set it in a form, then
submit by using target, but it will popup another window "other", then
close. Is there anyway not allowing a new window popup by
target="other"? I am not allowed to use iframe.

in the popup
you need no target as you're function is supposed to close it.
(just think to put a little delay before to close)

if no function or no self-closing in end of it :

<form action="mytest.php" onsubmit="setTimeout('self.close()',100)">
Answer : <input type="text" name="answer">
<input type="submit">
</form>


if(opener &&
opener.myFunction)
{
opener.myFunction(document.getElementById('myValue').value);
setTimeout('self.close()',500);
}
 
S

SamuelXiao

Le 11/4/09 1:30 PM, SamuelXiao a écrit :




So, on my idea, you can't 'click' that button by JS.
(only cickable elements of forms are JS clickable)


Why won't you launch directly that function declared in the mother,
instead ?

if(opener &&
    opener.myFunction)
opener.myFunction(document.getElementById('myValue').value);


in the popup
you need no target as you're function is supposed to close it.
(just think to put a little delay before to close)

if no function or no self-closing in end of it :

<form action="mytest.php" onsubmit="setTimeout('self.close()',100)">
Answer : <input type="text" name="answer">
<input type="submit">
</form>

if(opener &&
    opener.myFunction)
  {
   opener.myFunction(document.getElementById('myValue').value);
   setTimeout('self.close()',500);
  }

thanks all, the problem has been solved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top