Validate Form with JavaScript and send to PHP

Discussion in 'JavaScript' started by piropeator, May 5, 2008.

  1. #1
    I want to validate my form but does not work.
    These are my files: form_input.html and form_view.php only to display.
    What is the problem?

    ******* form_input.html
    <html>
    <title>Input</title>
    <script>
    function valida_envia(){
    //validate name
    if (document.fvalida.name.value.length==0){
    alert("Write a name")
    document.fvalida.name.focus()
    return 0;
    } else {
    // It's OK
    alert("The name is OK!");
    document.fvalida.submit();
    }
    }
    </script>
    </head>

    <body>
    <form name="fvalida" onsubmit="return valida_envia();" method="POST" action="form_view.php">
    <table width="383" border="0" bgcolor="#FFFFFF">
    <tr>
    <th scope="row"><div align="right">Nombre :</div></th>
    <td><input type="text" name="name"/></td>
    </tr>
    <tr>
    <th colspan="2" scope="row">
    <input type="submit" value="Input"/> </th>
    </tr>
    </table>
    </form>
    </body>
    </html>

    ***** form_view.php

    <head>
    <title>EJEMPLO</title>
    </head>

    <body>
    The name is : <?php echo $_POST['name'] ?>
    </body>
    </html>
     
    piropeator, May 5, 2008 IP
  2. premium_coder

    premium_coder Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it's not return 0
    it's return false
     
    premium_coder, May 5, 2008 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    It works. Thank you.
    The syntax of Java is well?
     
    piropeator, May 6, 2008 IP