Form Confirmation Problem

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

    Form Confirmation Problem

    Hi,

    I'm having this problem and I can't seem to solve it.

    I've created a confirmation page. The page displays the form field
    data and has
    2 links - OK (to continue) and Cancel (go back to form).

    The OK button will continue to validate the form values, cancel to
    close window.
    The problem starts here. I get script error and the error message
    doesn't tell me
    what's the problem.

    Also how do I include the form action code?

    Can someone help me, pls? Your help/guidance is much appreciated!

    Rgds,
    Kelvin


    <SCRIPT LANGUAGE="JavaS cript">
    <!--
    function display()
    {
    var x;
    DispWin = window.open('', 'ConfirmPage',
    'toolbar=no,sta tus=no,width=30 0,height=200')
    message = "<ul><li><b>des tination: </b>" +
    document.form1. destination.val ue;
    message += "<li><b>Wei ght: </b>" + document.form1. Weight.value;
    message += "<li><b>departu re: </b>" + document.form1. departure.value
    + "<br>";

    message += "\nChoose OK to submit these values, or\n"
    message += "Cancel to return to the form. <br><br>";
    message += "<a href='javascrip t:onclick='open er.document.for m1.checkForm1() ;self.close();' '>OK</a>
    "; //ERROR ON THIS LINE.

    DispWin.documen t.write(message );
    }


    function checkForm1()
    {
    if(document.for m1.destination. value == "")
    {
    alert("Destinat ion must not contain numbers at all.");
    document.form1. destination.foc us();
    return false;
    }

    if(document.for m1.departure.va lue == "")
    {
    alert("Origin must not contain numbers at all.");
    document.form1. departure.focus ();
    return false;
    }

    if(document.for m1.station.valu e == "")
    {
    alert("Please provide the station.");
    document.form1. station.focus() ;
    return false;
    }
    }

    // End -->
    </script>



    <form method="post" name="form1">

    <input name="destinati on" type="text" value="US">

    <input name="Weight" type="text" value="456">

    <input name="departure " type="text" value="UK">

    <input name="submit" type="submit" value="Quote" onClick="displa y();">

    </form>
  • kaeli

    #2
    Re: Form Confirmation Problem

    In article <ca7f6001.04092 70111.52a86e6e@ posting.google. com>,
    kelvinteh@yahoo .com enlightened us with...[color=blue]
    > Hi,
    >
    > I'm having this problem and I can't seem to solve it.
    >[/color]

    You have incorrectly nested quotes, among other problems.
    [color=blue]
    >
    > <SCRIPT LANGUAGE="JavaS cript">[/color]

    <script type="text/javascript">
    The language attribute is deprecated.

    [color=blue]
    > <!--[/color]

    I don't think there is a single human being using a browser that doesn't
    understand script any more. You can drop the comments.
    There is a difference between disabled script and not understanding script.
    Browsers used to not understand script. I think the last browser that didn't
    get it was netscape 3 or something else very, very old. ;)
    [color=blue]
    > function display()
    > {
    > var x;
    > DispWin = window.open('', 'ConfirmPage',
    > 'toolbar=no,sta tus=no,width=30 0,height=200')[/color]

    You understand the risks with using window.open, right?
    [color=blue]
    > message = "<ul><li><b>des tination: </b>" +
    > document.form1. destination.val ue;[/color]

    Invalid HTML will be written to the new page if you don't stick the right
    tags in there. You seem to have forgotten the html, head, and body tags.
    [color=blue]
    > message += "<li><b>Wei ght: </b>" + document.form1. Weight.value;
    > message += "<li><b>departu re: </b>" + document.form1. departure.value
    > + "<br>";
    >
    > message += "\nChoose OK to submit these values, or\n"
    > message += "Cancel to return to the form. <br><br>";
    > message += "<a href='javascrip t:onclick='open er.document.for m1.checkForm1() ;self.close();' '>OK</a>
    > "; //ERROR ON THIS LINE.
    >[/color]

    You didn't nest the quotes right. Backslash-escape the singe quotes embedded
    in the single quotes.
    And you understand the risks with using javascript as an anchor target,
    right?

    message += "<a href='javascrip t:onclick=\'ope ner.document.fo rm1.checkForm1
    ();self.close() ;\''>OK</a>";
    [color=blue]
    > {
    > if(document.for m1.destination. value == "")
    > {
    > alert("Destinat ion must not contain numbers at all.");
    > document.form1. destination.foc us();
    > return false;
    > }
    >[/color]

    Returning false cancels form submission IF you put the validation in onSubmit
    with a return and the submission is done from a submit button. You aren't
    doing that, plus you close the window regardless of the return value.
    Drop the self.close all by itself. It will close the window even if the form
    didn't submit. Put it in the validation/submit routine and call only if
    everything passed validation.
    Don't bother returning any value if you're keeping this format.

    Do this instead:
    message += "<a href='javascrip t:onclick=\'ope ner.document.fo rm1.checkForm1( );
    \''>OK</a>";

    checkForm1: (pseudocode)
    do all the checks
    if (any are bad)
    return
    else
    set action
    submit form in opener
    close self
    end if

    --
    --
    ~kaeli~
    A lot of money is tainted - It taint yours and it taint mine.



    Comment

    • Michael Winter

      #3
      Re: Form Confirmation Problem

      On Mon, 27 Sep 2004 09:12:01 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
      wrote:
      [color=blue]
      > I think the last browser that didn't get it was netscape 3 or something
      > else very, very old. ;)[/color]

      According to the Netscape reference, Netscape 2.0 implemented JavaScript
      1.0. That means that you'd have to go back to the first version of
      Netscape to find a browser that couldn't execute a script. I think "very,
      very old" is an understatement!

      [snip]
      [color=blue]
      > In article <ca7f6001.04092 70111.52a86e6e@ posting.google. com>,
      > kelvinteh@yahoo .com enlightened us with...[/color]
      [color=blue][color=green]
      >> message = "<ul><li><b>des tination: </b>" +
      >> document.form1. destination.val ue;[/color]
      >
      > Invalid HTML will be written to the new page if you don't stick the
      > right tags in there. You seem to have forgotten the html, head, and body
      > tags.[/color]

      You also forgot to mention that when a script placed directly inside a
      HTML document contains the sequence, "</", it should be escaped to ensure
      no browser mistakes that for the end of the SCRIPT element. The line above
      should read:

      message = "<ul><li><b>des tination: <\/b>"
      + document.form1. destination.val ue;

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      Working...