Click submit button without refresh

Discussion in 'Javascript' started by SamuelXiao, Nov 3, 2009.

  1. SamuelXiao

    SamuelXiao Guest

    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()?
     
    SamuelXiao, Nov 3, 2009
    #1
    1. Advertisements

  2. SamuelXiao

    Erwin Moller Guest

    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
     
    Erwin Moller, Nov 3, 2009
    #2
    1. Advertisements

  3. SamuelXiao

    SamuelXiao Guest

    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";
     
    SamuelXiao, Nov 3, 2009
    #3
  4. SamuelXiao

    SAM Guest

    Le 11/3/09 3:14 PM, SamuelXiao a écrit :
    It's not exactly a "parent".
    if(window.opener) {
    window.opener.document.getElementById("input1").value = "hardcode";
    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)
    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.
    You don't need a physical submit button when you do :
    document.forms[0].submit()
    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');
    }
     
    SAM, Nov 3, 2009
    #4
  5. SamuelXiao

    SAM Guest

    Le 11/3/09 4:00 PM, SamuelXiao a écrit :
    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() {
    if(typeof mywindow == undefined || mywindow.closed)
    mywindow=window.open("xxx.jsp","mywindow","width=100,height=100,top=0,left=0");
    mywindow.focus();
    }
     
    SAM, Nov 3, 2009
    #5
  6. SamuelXiao

    Erwin Moller Guest

    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
     
    Erwin Moller, Nov 3, 2009
    #6
  7. SamuelXiao

    SamuelXiao Guest

    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.
     
    SamuelXiao, Nov 4, 2009
    #7
  8. SamuelXiao

    SamuelXiao Guest

    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.
     
    SamuelXiao, Nov 4, 2009
    #8
  9. SamuelXiao

    Erwin Moller Guest

    SamuelXiao schreef:
    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
     
    Erwin Moller, Nov 4, 2009
    #9
  10. SamuelXiao

    SAM Guest

    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);
    }
     
    SAM, Nov 4, 2009
    #10
  11. SamuelXiao

    SamuelXiao Guest

    thanks all, the problem has been solved.
     
    SamuelXiao, Nov 6, 2009
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.