Results 1 to 4 of 4

Thread: Javascript Form Validation

  1. #1
    Join Date
    Mar 2011
    Posts
    1,931
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default Javascript Form Validation

    If the field is empty it should make an alert message saying you need to fill in your first name. Else it should submit the form. The alert message works but even if you do put something into the text input the form won't submit?

    Any help would be great.

    HTML Code:
    <?php
    require "database.php";
    if (isset($_POST['submit'])) 
    
    {
    echo "hello";
    }
    else
    
    {
    ?>
    
    <html>
    <head>
    <script type="text/javascript">
    function validateForm()
    {
    var x=document.forms["myForm"]["fname"].value;
    if (x==null || x=="")
      {
      alert("First name must be filled out");
    return false;
      }
      
    }
    
    </script>
    </head>
    
    <body>
    <form name="myForm" action="<?php echo $_SERVER['PHP_SELF']?>" onsubmit="return validateForm()" method="post">
    First name: <input type="text" name="fname">
    <input type="submit" value="Submit">
    </form>
    </body>
    
    </html>
    
    <?php
    }
    ?>
    Last edited by keyboard; 09-22-2011 at 01:20 AM.

  2. #2
    Join Date
    Dec 2004
    Posts
    177
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default

    Try adding:

    Code:
       alert("First name must be filled out");
       return false;
      }
      return true;
    }
    Verzeihung!

  3. The Following User Says Thank You to Minos For This Useful Post:

    keyboard (09-21-2011)

  4. #3
    Join Date
    Mar 2011
    Posts
    1,931
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default

    I changed what you suggested but it still won't work. However, if I change the target of the form to an external php processing page it works fine? Any help?

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your script should and does work as originally written. If the fname field is null or empty, you get the alert and the submission returns false so doesn't occur. If there's something in the fname field, there's no alert and the function returns undefined, so the submission goes through. You can observe this because the page reloads, evidenced by the fact that the fname field empties. After all $_SERVER['PHP_SELF'] is the action, so that's what one would expect.

    Ah, but you might say - I was expecting a successful submission to show a page with only:

    hello
    on it, because in the PHP it has:

    Code:
    if (isset($_POST['submit'])) 
    
    {
    echo "hello";
    }
    else
    
    {
    Well, $_POST['submit'] will never be set because there is no input in the form with the name 'submit'. If you add it to the submit button:

    Code:
    <form name="myForm" action="<?php echo $_SERVER['PHP_SELF']?>" onsubmit="return validateForm()" method="post">
    First name: <input type="text" name="fname">
    <input name="submit" type="submit" value="Submit">
    </form>
    Then things will perform as I imagine is expected.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    keyboard (09-22-2011)

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
  •