簡體   English   中英

提交前驗證表格

[英]validate form before submition

我想提交一個帶有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>

男人們對你很卑鄙!

基本上,您應該使用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

如果驗證正確,則綁定到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>

您可以使用Ajax和JQuery的beforeSubmit函數來實現:

$.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請求,這將變得更加簡單明了

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM