Php 提交前验证表格
我想提交一个包含php变量的表单以输入javascript,问题是这些变量只是在发布后才设置的,这对于它们在javascript中的响应来说太晚了。当然,如果我再次提交,这些变量已经被实现到javascript中,并且它可以正常工作。不过,我宁愿只提交一次。 是否有任何方法在提交前验证表格?以下是我的困境的一个例子:Php 提交前验证表格,php,javascript,html,Php,Javascript,Html,我想提交一个包含php变量的表单以输入javascript,问题是这些变量只是在发布后才设置的,这对于它们在javascript中的响应来说太晚了。当然,如果我再次提交,这些变量已经被实现到javascript中,并且它可以正常工作。不过,我宁愿只提交一次。 是否有任何方法在提交前验证表格?以下是我的困境的一个例子: <?php if(isset($_POST['submit'])){$name=$_POST['name'];} ?> <html> <
<?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>
如果验证正确,则绑定到onclick事件的函数必须返回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>
函数验证(){
//用javascript进行验证
//或者发出ajax请求,例如
///validate.php?name=somename&name=someothername
//让这个请求回答是真是假
//如果有效,则返回true,否则返回false
}
如果验证正确,则绑定到onclick事件的函数必须返回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>
函数验证(){
//用javascript进行验证
//或者发出ajax请求,例如
///validate.php?name=somename&name=someothername
//让这个请求回答是真是假
//如果有效,则返回true,否则返回false
}
男人对你很刻薄
基本上,您应该使用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>
男人对你很刻薄
基本上,您应该使用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>
您可以使用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请求,那么就更简单、更清晰了。您可以使用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*/}
});
<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>
我认为,如果使用跨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>
函数验证(){
name=document.getElementById('name')。值;
如果(名称==“”)
{
document.getElementById('message')。innerHTML='Please Fill Name';
返回false;
}
返回true;
}
函数验证(){
name=document.getElementById('name')。值;
如果(名称==“”)
{
document.getElementById('message')。innerHTML='Please Fill Name';
返回false;
}
返回true;
}
php不适合您的需要,JavaScript将适合您,尝试使用JavaScript代替这些双重提交检查此链接例如,您可以通过Jquery Ajax轻松验证表单。主要问题是您对php和JavaScript如何工作以及如何协同工作的概念。您需要了解PHP在服务器上运行,而Javascript在客户端(即浏览器)上运行。这意味着您需要向服务器发送请求,以便与PHP交互(普通请求或异步请求)。在你的例子中,我根本不明白PHP是如何进入其中的。您不需要任何服务器交互来完成您想要做的事情。只需使用JS,不要将表单发布到服务器上。如果关闭JS,它可能会被提交,因此您仍然需要有一个健壮的PHP后端验证过程。从可用性的角度来看,您是否希望返回带有提示的表单将取决于您以及您与潜在用户的关系、用户丢失的成本等。php不适合您的需要,JavaScript将适合您,尝试使用javascript代替这些双重提交检查此链接例如,您可以通过Jquery Ajax轻松验证表单主要问题是您对PHP和javascript如何工作以及如何协同工作的概念。您需要了解PHP在服务器上运行,而Javascript在客户端(即浏览器)上运行。这意味着您需要向服务器发送请求,以便与PHP交互(普通请求或异步请求)。在你的例子中,我根本不明白PHP是如何进入其中的。您不需要任何服务器交互来完成您想要做的事情。只需使用JS,不要将表单发布到服务器上。如果关闭JS,它可能会被提交,因此您仍然需要有一个健壮的PHP后端验证过程。从可用性的角度来看,你是否想返回带有提示的表单,取决于你和潜在用户的关系、用户损失的成本等。