Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Script not working upon submit?

    This scripts runs at first but then it brings up an error message when I hit submit. Any help is appreciated.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>The Result of the course</title>
    <script language="JavaScript">

    function trim(s) {
    while (s.charAt(0) == ' ')
    s = s.substring(1);
    while (s.charAt(s.length - 1)== ' ')
    s = s.substring(0, s.length - 2);
    return s;

    }

    function check(name)
    {
    var x = document.getElementById(ALastName);
    var s = new String(x.value);
    s = trim(s);
    if(s == "")
    {
    alert("Please enter your " + LastName);
    javascript:history.go(0);
    }
    var x = document.getElementById(AFirstNname);
    var s = new String(x.value);
    s = trim(s);
    if(s == "")
    {
    alert("Please enter your " + FirstName);
    javascript:history.go(0);
    }
    else
    x.value = s;
    }
    </script>
    </head>

    <body>
    <form action="http://matrix.csis.pace.edu/~badii/Exercise12.php" method="post">
    <label for="lname">Last name: </label>
    <input type="text" name="LastName" id="ALastName" size="20"><br><br>
    <label for="fname">First name: </label>
    <input type="text" name="FirstName" id="AFirstName" size="20"><br><br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;
    <input type="submit" name="submit" id="submit" value=" Submit "
    onclick = "check('LastName'); check('FirstName')">
    </form>
    </body>
    </html>

    Thanks
    Tom

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    you're code is full of undefined variables:

    ALastName
    AFirstNname
    LastName
    FirstName

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yes and it looks rather a simple validate code, so that the main error is that the function is called onclick, not on submit, as it should. Let's simplify the code:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    var names=['LastName','FirstName']
    function check(e){
    for(var i=0;i<names.length;i++){
    var re=e[names[i]].value.match(/^\s*$/);//check if the field has any characters
    if(re){
    alert('Please enter your '+names[i]);
    e[names[i]].focus();return false;
    }
    }
    }
    </script>
    </head>
    <body>
    <form onsubmit="return check(this.elements)">
    <label for="lname">Last name: </label>
    <input type="text" name="LastName" id="ALastName" size="20"><br><br>
    <label for="fname">First name: </label>
    <input type="text" name="FirstName" id="AFirstName" size="20"><br><br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;
    <input type="submit" name="submit" id="submit" value=" Submit ">
    </form>
    </body>
    </html>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Mar 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The script needs to be able to contact that matrix server to pick up a .php file to return a message after the click on submit. I still have to add the file to the server, but it needs to have that form string in the code.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by digic25
    The script needs to be able to contact that matrix server to pick up a .php file to return a message after the click on submit. I still have to add the file to the server, but it needs to have that form string in the code.
    ? why? I don't get it... My code will force user to input a string (but not one full only with empty spaces - some users do this trick). Unless the condition is not fulfiled, the submit is stoped. If condition, the submit goes... isn't that what you want?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    Mar 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whats supposed to happen is when you click submit a greeting will come with your name. The php consists of the greeting with the users name inputed via the javascript.


  •  

    Posting Permissions

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