PHPJavaScript form validation? - Stack Overflow

I'm new with PHP and JavaScript. I eant to validate form, and if the form input fields are correct

I'm new with PHP and JavaScript. I eant to validate form, and if the form input fields are correct, then send it to php external script. I've tried something like this:

<head>
<script type="text/javascript">
  function Validate(){
    var x=document.forms["form"]["firstname"].value;
    var y=document.forms["form"]["lastname"].value;
    var z=document.forms["form"]["age"].value;
    if(x==null || x=="") {
      alert("Input name!");
    } else if(y==null || y=="") {
      alert("Input surname!");
    } else if(z==null || z=="") {
      alert("Input age!");
    } else {
  // IS IT POSSIBLE TO SEND FORM HERE FORM VARIABLES TO PHP SCRIPT?
    }
  }
</script>
</head>
<body>

  <form name="forma" action="validate.php" method="post" onSubmit="return Validate()">
    Firstname: <input type="text" name="firstname">
    Lastname: <input type="text" name="lastname">
    Age: <input type="text" name="age">
    <input type="submit">
  </form>
</body>
</html>

I'm new with PHP and JavaScript. I eant to validate form, and if the form input fields are correct, then send it to php external script. I've tried something like this:

<head>
<script type="text/javascript">
  function Validate(){
    var x=document.forms["form"]["firstname"].value;
    var y=document.forms["form"]["lastname"].value;
    var z=document.forms["form"]["age"].value;
    if(x==null || x=="") {
      alert("Input name!");
    } else if(y==null || y=="") {
      alert("Input surname!");
    } else if(z==null || z=="") {
      alert("Input age!");
    } else {
  // IS IT POSSIBLE TO SEND FORM HERE FORM VARIABLES TO PHP SCRIPT?
    }
  }
</script>
</head>
<body>

  <form name="forma" action="validate.php" method="post" onSubmit="return Validate()">
    Firstname: <input type="text" name="firstname">
    Lastname: <input type="text" name="lastname">
    Age: <input type="text" name="age">
    <input type="submit">
  </form>
</body>
</html>
Share Improve this question edited Mar 19, 2013 at 22:34 juco 6,3423 gold badges26 silver badges42 bronze badges asked Mar 19, 2013 at 22:18 Ivan PandžićIvan Pandžić 3735 silver badges14 bronze badges 2
  • after your alert enter: return false;. This way the form will not be submitted. Also make sure to check the variables again in your php script. – Green Black Commented Mar 19, 2013 at 22:22
  • You can skip those null checks – Sam Dufel Commented Mar 19, 2013 at 22:26
Add a ment  | 

4 Answers 4

Reset to default 1

Since you are not returning false from your function, the form should be being submitted anyway. You can stop it from being submitted by having your validate() function return false.

Your code looks correct, other than the fact you probably want to return false on failure to prevent the form from submitting. When not returning false, the form will simply continue to submit.

I also think you probably meant to name your form form not forma

You should validate the input server-side (php), even if you did it client-side (javascript).

You do this by checking on $_POST in validate.php:
Quite at the beginning of your script:

if (isset($_POST)) {  // form has been sent

    if (!isset($_POST['firstname']) || strlen($_POST['firstname'])==0) { // no entry 

        $e['firstname']='please enter a firstname!'; // prepare error-msg
    }

    if (!isset($e)) {  // no error, continue handling the data

        // write data to database etc.
    }
}

// in your form...

if (isset($e)) echo "there were errors: ". implode(',',$e);

You need to put this code:

function Validate(){
var x=document.forms["form"]["firstname"].value;
var y=document.forms["form"]["lastname"].value;
var z=document.forms["form"]["age"].value;
if(x==null || x=="") {
  alert("Input name!");
  return false;
} else if(y==null || y=="") {
  alert("Input surname!");
  return false;
} else if(z==null || z=="") {
  alert("Input age!");
  return false;
} else {
  return true;
}
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745157131a4614191.html

相关推荐

  • PHPJavaScript form validation? - Stack Overflow

    I'm new with PHP and JavaScript. I eant to validate form, and if the form input fields are correct

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信