www.webdeveloper.com
Results 1 to 7 of 7

Thread: Why won't my code work?

  1. #1
    Join Date
    Jul 2011
    Location
    Lost in space
    Posts
    7

    Why won't my code work?

    My code won't work! How do I combine action agruments? Can you please help?
    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    function confirmInput()
    {
    fname=document.forms[0].fname.value;
    alert("Hello, " + fname + "! You will now be redirected to the Monkeypuzzle Manor Home Page.");
    }
    </script>
    <script type="text/javascript">
    function validateForm()
    {
    var x=document.forms["myForm"]["question"].value
    if (x==null || x=="")
      {
      alert("Please write a message in the message field");
      return false;
      }
    }
    </script>
    <script type="text/javascript">
    function validateForm()
    {
    var x=document.forms["myForm"]["email"].value
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
      {
      alert("The e-mail address you submitted is not valid. Please enter a valid e-mail address.");
      return false;
      }
    }
    </script>
    </head>
    
    <body>
    <form name="myForm" method="post" action="mailto:MYEMAIL" action="http://babybear.us/" enctype="text/plain" onsubmit="confirmInput()" onsubmit="return validateForm()">
    <table style="border:0px">
    <tr>
    <td>Name: </td><td><input id="fname" name="fname" type="text" size="20"></td></tr>
    <tr>
    <td>E-mail: </td><td><input type="text" name="email" size="30"></td></tr>
    <tr>
    <td>Question Type: </td><td><select name="questiontype" size="1"> <option>General question about Monkeypuzzle Manor</option><option>Question about item(s)</option><option>Feedback for Monkeypuzzle Manor</option><option>Feedback about item(s) received</option><option>Other question/comment type</option></select></td></tr>
    <tr>
    <td valign="top">Message: </td><td><textarea name="question" rows="10" columns="20"></textarea></td></tr>
    <tr>
    <td valign="top">Overall, how would you rate Monkeypuzzle Manor?</td>
    <td valign="top"><input type="radio" name="rating" value="excellent" />Excellent<br />
    <input type="radio" name="rating" value="good" />Good<br />
    <input type="radio" name="rating" value="fair" />Fair<br />
    <input type="radio" name="rating" value="poor" />Poor<br />
    <input type="radio" name="rating" value="verypoor" />Very Poor<br /><br /></td>
    </tr>
    <tr><td valign="top"></td><td><input type="checkbox" name="yes" checked="checked" /> Yes, I would like to receive e-mail updates of Monkeypuzzle Manor's available items.</td>
    </table>
    <br />
    <input type="submit" value="Send Message"><input type="reset" value="Clear Fields">
    
    </form>
    </body>
    
    </html>
    I'm using babybear.us (my other site) in lieu of Monkeypuzzle Manor's (my current project's) home page.
    Thanks in advance!
    P.K.

  2. #2
    Join Date
    May 2011
    Posts
    194

    clean your code to find (logically) errors

    Hi,

    clean your code and make it shorter. Check logically structure of functions. Use arguments for function
    to replace values or references of global vars. Don't uses values that function not need for action.
    Thrn you find errors easier.

    function validateForm()
    {
    var x=document.forms["myForm"]["question"].value
    if (x==null || x=="")
    {
    alert("Please write a message in the message field");
    return false;
    }

    // and else ? no return ?
    }


    you mean


    function validateForm(stValue) // replace document.forms["myForm"]["question"].value
    // function only compare value ... and so not need a reference.
    {return ( (stValue!=null) && (stValue!=""));}


    if(!validateForm(document.forms["myForm"]["question"].value))
    {alert(alert("Please write a message in the message field");}

    -----------------------------------------------


    function confirmInput()
    {
    fname=document.forms[0].fname.value;
    alert("Hello, " + fname + "! You will now be redirected to the Monkeypuzzle Manor Home Page.");

    // alert do not confirm
    }


    you mean


    function confirmInput(stMessage1,stValue,stMessage2) // arguments not null and not empty
    {
    return (confirm(stMessage1 + stValue + stMessage2")); // true for OK button
    }

    if(confirmInput("Hello, ", document.forms[0].fname.value,stMessage, "! Do you want now to be redirected to the Monkeypuzzle Manor Home Page ?")
    {alert("user want");}

    ------------------------------------------------

    function validateForm()

    // same: no return inside else


    ------------------------------------------------

    a function should have ONE return
    store return value into var

    function validateForm(stValue) // argument must be !=null and != ""
    // stValue ingoing of function, a reference like document.forms["myForm"]["email"].value not needed for compare of positions
    {
    var atpos=stValue.indexOf("@");
    var dotpos=stValue.lastIndexOf(".");
    var boReturn=true; // for else and default

    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
    {boReturn=false;}

    return boReturn; // ONE return (outgoing of function)
    }

    if(!validateForm(document.forms["myForm"]["email"].value))
    {alert("The e-mail address you submitted is not valid. Please enter a valid e-mail address.");}

  3. #3
    Join Date
    Jul 2011
    Location
    Lost in space
    Posts
    7
    sry i didn't quite get that...you see, I'm only just learning JS, this is my 2nd website ever and I'm trying to "pick up" JS the same way I picked up HTML...copy, paste, tinker, repeat. Hey, it works...I have most transitional HTML 4.01 committed to memory lol. Now I know more about that than my dad- and he's a systems admin lol.

  4. #4
    Join Date
    May 2011
    Posts
    194

    try that ....

    Hi,

    Webpage editors support tools and functions to build in.

    Like that .... copy and paste

    http://www.wysiwygwebbuilder.com/getting_started.html
    http://www.wysiwygwebbuilder.com/form_wizard.html


    but it's not freeware.
    Try the trial. If good your dad should buy software with full support.


    Or see free editor http://www.coffeecup.com/free-editor/


    ----------------------------------------------------

    To test your code use browser like Opera.

    Browsers offers log windows for messages about error (kind of error, position).
    This informations you need ... for offers in forum too.

    No code ist perfect. Optimze your code after clean.

    If you not want use wysiwyg webbuilder what you see is what you get ...


    ---------------------------------------------------

    To create a webpage by hand you must learn content of HTML DOM.
    wysiwyg webbuilder use HTML DOM too, but offers prefedined functions
    like formular checker etc.

    For the way by hand take reference of Microsoft, see http://msdn.microsoft.com/en-us/library/ms533050.aspx

    All objects you use in your code you find in this reference.

    Try that way to find better arguments for your dad to buy wysiwyg webbuilder.
    Easy, quickly to get your webpage.

    Maybe, after some lessions in HTML by use wysiwyg webbuilder you want programming by hand.

    ----------------------------------------------------

    One of finest editors for HTML are Allaire Homesite and Macromedia Dreamwaver.

    Allaire is dead. My be you find an old version in use torrents network.

    Adobe.com hosts Dreamweaver. Complete solution CS too. Highes profession.

    Try that, but your dad must have enough money to buy it.

    Adobe CS 5 opens creative webdesing from html over images videos to mobil phones.
    Complete solutions.


    ------------------------------------------------------

    Ahm, maybe your internet provider offers tools to create a webpage.

  5. #5
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Your form has two action parameters specified and two onsubmit handlers. Also you have two functions called validateForm.
    One of each of the above only please.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  6. #6
    Join Date
    May 2011
    Posts
    101
    Yes, two handlers and two functions with same name !
    -
    Strangely, I would advise exactly the opposite to twseitex (Sorry, no
    offence meant).

    When you are learning (and beyond that stage) you need to fully grasp
    what your code is doing and how it is constructed. Using IDE tools which
    magically conjure up blocks of (often inefficient and non-compliant) HTML
    and Javascript teaches you little or nothing.

    Start with a good editor and a browser window open. Nothing more.
    By a 'good editor', I mean something like BBEdit which highlights code in
    a colour scheme, but even something like Notepad (Windoze) or
    TextEdit (Mac) is better for your learning process than Dreamweaver or
    the like.

    Products like FrontPage, which promise "a website in seconds' or some
    such nonsense, are not going to be helpful to your learning experience.
    They are toys for children.

    Chris
    Sometimes, when you re-invent the wheel, you end up with a better wheel.
    ---------------------------------------------
    One-on-One Scripting Tuition/Mentoring

  7. #7
    Join Date
    Jul 2011
    Location
    Lost in space
    Posts
    7
    Thank you guys!
    Why, yes! I am made of AWESOME!

    Visit my Web site at http://babybear.us/!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles