Help! Submitting to FormMail through JavaScript Submit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Margaret Werdermann

    Help! Submitting to FormMail through JavaScript Submit

    Hi all:

    I'm having a nasty time with a particularly difficult piece of code
    and was hoping someone might be able to help me. I have a FormMail
    form that originally worked perfectly.

    Then, I had to add a JavaScript function to the Submit button to make
    a server function run when the form was submitted. Unfortunately,
    this JavaScript wouldn't run when the button was designated as a
    Submit, so I changed the button and placed a JavaScript submit into my
    JavaScript function. Great! The JavaScript worked
    perfectly...BUT ...that broke my original FormMail submit, so now I'm
    not getting e-mails anymore!

    Here's the JavaScript function:

    function autoURL (realname) {
    window.document .forms[0].submit();
    window.location .href="http://ceridianet.ceri dian.ca/applications/accreditation.n sf/MarkPreComplete ?Openagent&test =Base
    Builder Pre-test &pname=" + realname;
    }

    ....and here's the Submit button's code:

    <input type="button" value="Submit" id="feedback" name="Submit"
    onclick="autoUR L(document.form s[0].realname.value );">

    Can anyone help me with this? I'm about to tear my hair out over it!
  • Ivo

    #2
    Re: Help! Submitting to FormMail through JavaScript Submit

    "Margaret Werdermann" coded[color=blue]
    > Here's the JavaScript function:
    >
    > function autoURL (realname) {
    > window.document .forms[0].submit();
    >[/color]
    window.location .href="http://ceridianet.ceri dian.ca/applications/accreditati
    on.nsf/MarkPreComplete ?Openagent&test =Base[color=blue]
    > Builder Pre-test &pname=" + realname;
    > }[/color]

    You 're giving two contradicting commands here. The submit() loads whatever
    url is specified as the form's action, after sending the form data to the
    server. location.href just loads a different page. Can't go to two places at
    the same time.
    [color=blue]
    > ...and here's the Submit button's code:
    >
    > <input type="button" value="Submit" id="feedback" name="Submit"
    > onclick="autoUR L(document.form s[0].realname.value );">[/color]

    If you specify both an id and a name for a form element, according to
    standards they should be identical.
    If this replaces the <input type="submit">, you end up with a form with no
    submit button. That in itself may cause attempts to submit() to fail. I
    suspect this is why the first line did not execute in your above function;
    if the form had submitted, the second would not have run and you would not
    have ended up at specified url.

    The onclick event handler can be set on that button (sometimes even if it is
    hidden) to add script to that specific event: clicking the submit-button.
    But browsers generally allow users to submit forms by hitting the return key
    in an ordinary text-input. In code, we need to tackle the onsubmit event of
    the form element instead.

    Like so:
    <form onsubmit=" return doSomething(thi s); ">
    <input type="text" value="type here and hit enter">
    <input type="submit" style="display: none;">
    </form>

    This function should return either true or false (or anything that may be
    interpreted as such - anything). If this function were to return false, the
    form would not be submitted, the page would remain. With a return value of
    true the data are sent to the server, which results in a new page being
    loaded.

    Like so:
    function doSomething(){
    return confirm('Do you want to submit the form?');
    }

    Or so:
    function doSomething( el ){
    var x=el.elements[0].value.length;
    if ( x < 3 ) {
    alert('At least three characters, please..');
    return false;
    } else {
    return true;
    }
    }

    The function can do all sorts of things before returning its value (if it
    doesn't return anything, this is interpreted as true) including spawning and
    closing windows. This appears to be a worst-case solution looking at what
    you were trying to do. (The real problem is often not javascript but
    back-end related.)
    It should be clear that is makes no sense (probably results in awful
    crashes) to call form.submit() in this function because it only runs when
    this event is already happening. The submit() method would be useful for
    initiating this process by any other means than those provided by the form
    itself.
    To be of more specific help, some more code would help identify the idea,
    what you are actually trying to achieve, and the problem.

    HTH
    Ivo



    Comment

    • Margaret Werdermann

      #3
      Re: Help! Submitting to FormMail through JavaScript Submit

      Thanks so much Ivo! We did finally get it to work. You were right on
      with your explanation. Here's what we did to make it work:

      First, we changed the JavaScript function completely to run our
      automatic script from a new window, like this:

      function autoURL (realname) {
      var urladd="URL to run the automatic script" + realname;
      var urltit="suitabl e title bar text";
      var urlopt="width=6 00, height=600, toolbar=yes, resizable=yes,
      scrollbars=yes, status=yes";
      window.open(url add,urltit,urlo pt)
      return true;
      }

      Then, we placed the call to the JavaScript function into an onSubmit in
      the form, like this:

      <form action="URL to our cgi bin" method="POST" name="MailTest"
      id="MailTest" onsubmit="retur n
      autoURL(documen t.forms[0].realname.value )">

      Then we could go back to just a regular HTML Submit button, like this:

      <input type="submit" name="Submit" value="Submit">

      That did it! The automatic script ran and the form was submitted
      successfully through FormMail. I hope this helps someone else.

      Thanks again,

      Margaret

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      Working...