最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

PHPJavaScript form validation? - Stack Overflow

programmeradmin0浏览0评论

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;
}
}
发布评论

评论列表(0)

  1. 暂无评论