0

我想提交一个带有 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

4 回答 4

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

于 2013-01-14T14:26:17.990 回答
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>
于 2013-01-14T14:05:10.160 回答
1

您可以使用 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 请求,它会更容易更清晰

于 2013-01-14T14:32:11.563 回答
1
<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>
于 2013-01-14T14:43:55.757 回答