#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    PK
    Posts
    57
    Rep Power
    12

    Post form submit and JAVASCRIPT


    i am using javascipt to check whether the user has entered some data or not,,, but am unable to submit the form,,,

    =============================================

    <?PHP
    //Was the form submitted?
    if(isset($_POST['submit']))
    {
    // Yes, so process form
    echo $_POST['Name'];
    }
    // No it wasnt, show form
    else
    {
    ?>
    <html>
    <head>
    <title>Test</title>
    <script language="JavaScript">
    function formcheck(){
    var name = document.check.name.value;
    if (name == '') {
    alert ("Please enter data fields.");
    }
    else {
    document.check.submit();
    }
    }
    </script>
    </head>
    <body>
    <form method="POST" name="check" action="<?PHP $_SERVER['PHP_SELF'] ?>">
    <p>Name <input type="text" name="Name" size="20"></p>
    <p><input type=button value=Submit name=Submit onClick=formcheck();></p>
    </form>
    </body>
    </html>
    <?PHP
    }
    ?>

    =============================================
  2. #2
  3. learning to fly
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Caldwell, Idaho, USA
    Posts
    58
    Rep Power
    12
    I believe your type=button needs to be type=submit...

    s
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    PK
    Posts
    57
    Rep Power
    12

    Re:


    it is still not working, however when i delete the code of Javascript it is working fine,,, which is like this,,,

    <?PHP
    //Was the form submitted?
    if(isset($_POST['submit']))
    {
    // Yes, so process form
    echo $_POST['Name'];
    }
    // No it wasnt, show form
    else
    {

    ?>

    <html>
    <head>
    <title>Test</title>
    </head>

    <body>

    <form method="POST" action="<?PHP $_SERVER['PHP_SELF'] ?>">
    <p>Name <input type="text" name="Name" size="20"></p>
    <p><input type="submit" value="Submit" name="submit"></p>
    </form>

    </body>

    </html>

    <?PHP
    }
    ?>
  6. #4
  7. learning to fly
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Caldwell, Idaho, USA
    Posts
    58
    Rep Power
    12
    change your js to onSubmit instead of onClick
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    PK
    Posts
    57
    Rep Power
    12

    Angry Re:


    even onSubmit it is not working
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Posts
    790
    Rep Power
    15
    Try putting the onSubmit in the form tag rather than the button tag. You probably also need to change the button tag to a <input type="submit"> Then change the function a bit.

    Code:
    function formcheck(){
    var name = document.check.name.value;
    if (name == '') {
    alert ("Please enter data fields.");
    return false;
    }
    else {
    return true;
    }
    }
  12. #7
  13. No Profile Picture
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Indonesia
    Posts
    905
    Rep Power
    18
    PHP Code:
    <?
    if(isset($_POST['submit'])) {
      echo 
    $_POST['name'];
    } else {
    ?>
      <html>
      <head>
        <title>Test</title>
        <script language="JavaScript">
          function formcheck() {
            if (document.check.name.value == "") {
              alert ("Please enter data fields.");
              document.check.name.focus();
              return false;
            }
            return true;
          }
        </script>
      </head>
      <body>
      <form method="POST" name="check" action="<?=$_SERVER['PHP_SELF'];?>" onSubmit="[B]return[/B] formcheck()">
        <p>Name <input type="text" name="name" size="20"></p>
        <p><input type="submit" value="Submit" name="submit"></p>
      </form>
      </body>
      </html>
      <?
    }
    ?>
    PS. Try to have cosistency on variable's name (capitalization). In some case it won't be a problem. But it will in other case.
    Last edited by tommy.pr; May 26th, 2003 at 02:05 AM.

IMN logo majestic logo threadwatch logo seochat tools logo