SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS Validation w/PHP Form

    Please tell me why this isn't working. I need for JS to validate the form first and then pass it along to the PHP to process it, but even if the fields aren't filled in, it just pops up the alert and goes right to the PHP. I got it to work before, but can't seem to make it work now!

    PHP Code:
    <html> 
     <head> 
      <title>PHP/JavaScript Validation</title> 
      <script type="text/javascript"> 
       function validate() { 
        if(document.form.test.value == '') { alert("Error!  Fill in field."); return false; } 
        else { return true; } 
       } 
      </script> 
     </head> 
     <body> 
      <?php if($_POST['test'] != '') { echo('Validate Successful!'); } ?> 
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form"> 
       <input type="text" size="20" name="test" /> 
       <input type="submit" onclick="javascript:validate();" /> 
      </form> 
     </body> 
    </html>
    Help would be appreciated
    - Saj

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <form onsubmit="return validate()" ... >
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Duh! Forgot the return. Thanks very much beetle
    - Saj


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
  •