提交前验证表格

时间:2013-01-14 13:59:54

标签: php javascript html

我想提交一个带有php变量的表单输入到javascript中,问题是变量只能在发布后设置,这对他们来说太迟了,无法回复到javascript中。当然,如果我再次提交,变量已经实现到javascript中,它可以正常工作。但是,我宁愿只提交一次。 提交前有没有办法验证表格?这是我的困境的一个例子:

<?php
      if(isset($_POST['submit'])){$name=$_POST['name'];}
?>

<html>
 <div id="message"></div>
    <form action="home.html" method="POST">
       <input type="text" name="name">
       <input type="submit" name="submit" value="conditions-met" 
              onclick=<?php if($name=="bob"){echo"start();";}?>>
    </form>
</html>

 <script type="text/javascript">

  function start(){
       document.getElementById('message').innerHTML = 'hello bob';
  return true;
}
 </script>

4 个答案:

答案 0 :(得分:2)

男人是你的意思!!

基本上,您应该使用JavaScript验证字段。

单击提交按钮时,执行检查。继续成功,在失败时显示错误消息。

示例

<form id="myForm" onsubmit="return ValidateFields(this)" name="myForm" accept-charset="UTF-8" enctype="multipart/form-data"" action="myPhpScript.php" method="POST">

// html fields


</form>


<script>

function ValidateFields(formObject)
{

   var FieldsValid = true;

   //Validate the fields and if one of them is not valid set FieldsValid to false

if(!FieldsValid )
{
   //show an error message

return false; // Returing false will prevent the form from submitting

}

return true;// the form will be submitted
}

</script>

要成为忍者,请阅读以下内容:

http://www.script-tutorials.com/form-validation-with-javascript-and-php/

http://www.webcredible.co.uk/user-friendly-resources/dom-scripting/validate-forms-javascript.shtml

http://www.w3schools.com/js/js_form_validation.asp

答案 1 :(得分:2)

您可以使用Ajax和 beforeSubmit 函数与Jquery一起使用:

$.ajax({
            url: "your async page in php",
            cache: true,
            data: $("#formID").serialize(),
            type: 'POST',
            beforeSend: function(){ 
                         /* You can validate your input here*/
            },
            success: function(response){
                /* Based on the response from php you can handle the message to the user*/
            },
            error: function(response){/*Error Handler*/},
            complete: function(){/*If it's complete than you can reset the form*/}
        });

如果您使用跨Ajax / PHP请求,我认为这很容易也很清楚

答案 2 :(得分:2)

<html>
<div id="message"></div>
<form action="home.html" method="POST" onsubmit="return validate()">
<input type="text" name="name" id="name">
<input type="submit" name="submit" value="conditions-met" >
</form>
</html>

<script type="text/javascript">

function validate(){

name=document.getElementById('name').value;
if(name == "")
{
document.getElementById('message').innerHTML = 'Please Fill Name';
return false;
}
return true;
}
</script>

答案 3 :(得分:1)

如果验证正确,绑定到onclick-event的函数必须返回true,否则返回false。

同样的功能必须在javascript中。你不能在其中调用php函数。如果你想要php,试试ajax。

<input type="submit" onsubmit="return validate();" />

<script>
    function validate() {
        // do validation in javascript
        // or make an ajax request e.g. to 
        //  /validate.php?name=somename&surname=someothername 
        // and let this request answer with true or false

        // return true if valid, otherwise false
    }
</script>