SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Two OnSubmit Functions.

    First of all, I am newbie to Java, if you can offer me some help, please help with code, not suggestions.

    ==================================
    Trying to validate and then open the submission result in a pop up
    window:


    <form name=contact action="contact.cgi" method="POST" target="foo"
    onsubmit="return checkFields(); window.open
    ('', 'foo', 'width=450,height=300,status=yes,resizable=yes,scrollbars
    =yes');"

    It validates fine but doesn't open the pop up window instead it goes
    for a normal new window. If remove the validation part "return
    checkFields", it works fine for pop up alone.

    Any options that I can perform both functions at the same time?

    ############## Validation Code in Head ############
    <SCRIPT LANGUAGE="JavaScript"

    <!-- Begin
    function checkFields() {
    missinginfo = "";
    if (document.contact.name.value == "") {
    missinginfo += "\n - Name";
    }
    if ((document.contact.email.value == "") ||
    (document.contact.email.value.indexOf('@') == -1) ||
    (document.contact.email.value.indexOf('.') == -1)) {
    missinginfo += "\n - Email address";
    }
    if(document.contact.message.value == "") {
    missinginfo += "\n - Message";
    }

    if (missinginfo != "") {
    missinginfo ="_____________________________\n" +
    "You failed to correctly fill in your:\n" +
    missinginfo + "\n_____________________________" +
    "\nPlease re-enter and submit again!";
    alert(missinginfo);
    return false;
    }
    else return true;
    }
    // End --
    </script>

  2. #2
    SitePoint Enthusiast Tygatur's Avatar
    Join Date
    Apr 2006
    Location
    Germany
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, I am newbie to Java, if you can offer me some help, please help with code, not suggestions.
    Java is not JavaScript ! And I think it's a bad idea just to ask for the code. You won't learn JavaScript this way. You need the knowlegde to change code the way you want it to be.

    <form name=contact action="contact.cgi" method="POST" target="foo"
    onsubmit="return checkFields(); window.open
    ('', 'foo', 'width=450,height=300,status=yes,resizable=yes,scrollbars
    =yes');"
    1. Always use "quotes" for attributes!
    2. JavaScript has no multi-line-support. You need to write javascript commands into one line.
    3. "return" makes the script stop and returns a value that is beeing calculated by checkFields(); So it's a bad idea to use return and then write another JavaScript command into the event handler.
    4. the first parameter of window.open() is the URL of the document to be loaded.

    <SCRIPT LANGUAGE="JavaScript"

    <!-- Begin
    5. Don't copy code. Write you own. Or look for a better tutorial.
    6. Write HTML Tags in lowercase
    7. instead of <script language="JavaScript"> write <script type="text/javascript">
    8. Don't use HTML comments in the JavaScript area. Use the following:
    <script type="text/javascript">/* <!CDATA[ */

    /* ]]> */</script>

    I know. It's hard to start with a new programming language. It appears to me that you are just copying most of the time. The bad news is that there is very much legacy code out there from a time where other JavaScript-methods were common. A good way to detect such legacy code is for example to look for <script language="... and for if(document.layers)
    If you see one of these then don't use the code!
    to code or not to code ?
    that's too much of a question for a signature.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •