I am trying to validate a form using a jQuery form validation plug-in. It's working well in a HTML page. But when I copy-paste the same code into a form in a PHP page whose action is PHP_SELF
form validation isn't working.
我正在尝试使用jQuery表单验证插件验证表单。它在HTML页面中运行良好。但是当我将相同的代码复制粘贴到PHP页面中的表单时,其操作是PHP_SELF,表单验证不起作用。
My code:
<form id='registerForm' name='registerForm' method='post' action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" />
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" />
</div>
<div>
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
<script type="text/javascript" src="jq213.js"></script>
<script type="text/javascript" src="validate.min.js"></script>
<script>
$("#registerForm").validate({
rules:{
name:{
required:true,
minlength:2
},
email:{
required:true,
email:true
},
},
messages:{
name:{
required:"pls enter your name",
minlength:"minimum 2 char for name"
},
email:{
required:"required",
email:"Enter a valid email"
}
}
});
</script>
Why is it not working in a PHP self referencing form? If any more information is needed please tell me.
为什么它不能在PHP自引用表单中工作?如果需要更多信息,请告诉我。
5 个解决方案
#1
<script type="text/javascript" src="jq213.js"></script>
<script type="text/javascript" src="validate.min.js"></script>
These two lines of codes are not working. And except this everything is ok. I have tested. Any how make these plugins work.
这两行代码不起作用。除此之外一切都还可以。我测试过了。任何如何使这些插件工作。
#2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form id='registerForm' name='registerForm' method='post' action="" >
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" />
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" />
</div>
<div>
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
<script type="text/javascript" src="js/jq213.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
<script>
$("#registerForm").validate({
rules:{
name:{
required:true,
minlength:2
},
email:{
required:true,
email:true
},
},
messages:{
name:{
required:"pls enter your name",
minlength:"minimum 2 char for name"
},
email:{
required:"required",
email:"Enter a valid email"
}
}
});
</script>
</body>
</html>
#3
Try this.
<form id='registerForm' name='registerForm' method='post' action="" >
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" required minlength="2" />
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" required />
</div>
<div>
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
<script type="text/javascript" src="jq213.js"></script>
<script type="text/javascript" src="validate.min.js"></script>
<script>
$(function() {
$("#registerForm").validate({
rules:{
name:{
required:true,
minlength:2
},
email:{
required:true,
email:true
},
},
messages:{
name:{
required:"pls enter your name",
minlength:"minimum 2 char for name"
},
email:{
required:"required",
email:"Enter a valid email"
}
}
});
});
</script>
#4
You code works fine and the problem is that you are missing your jQuery file. Add those 2 line of code and it should work.
您的代码工作正常,问题是您缺少jQuery文件。添加这两行代码,它应该工作。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
AND ALSO IMPORTANT
而且也很重要
Use $().ready(function ()
~~
使用$()。ready(function()~~
$().ready(function () {
$("#registerForm").validate({
rules: {
name: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
}
},
messages: {
name: {
required: "pls enter your name",
minlength: "minimum 2 char for name"
},
email: {
required: "required",
email: "Enter a valid email"
}
}
});
});
#5
You could try 3 things:
你可以试试3件事:
- Add type="text/javascript" in your third
"<script></script>"
- Start with $(document).ready(function(){}) before using jQuery functions;
- View page source and try clicking to your src link to found whether they are working or not! If not, maybe the link has changed to wrong when you transfer to PHP.
在第三个“
在使用jQuery函数之前,先从$(document).ready(function(){})开始;
查看页面源代码并尝试单击您的src链接以查看它们是否正常工作!如果没有,转移到PHP时,链接可能已更改为错误。
Hope it will help!
希望它会有所帮助!
#1
<script type="text/javascript" src="jq213.js"></script>
<script type="text/javascript" src="validate.min.js"></script>
These two lines of codes are not working. And except this everything is ok. I have tested. Any how make these plugins work.
这两行代码不起作用。除此之外一切都还可以。我测试过了。任何如何使这些插件工作。
#2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form id='registerForm' name='registerForm' method='post' action="" >
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" />
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" />
</div>
<div>
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
<script type="text/javascript" src="js/jq213.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
<script>
$("#registerForm").validate({
rules:{
name:{
required:true,
minlength:2
},
email:{
required:true,
email:true
},
},
messages:{
name:{
required:"pls enter your name",
minlength:"minimum 2 char for name"
},
email:{
required:"required",
email:"Enter a valid email"
}
}
});
</script>
</body>
</html>
#3
Try this.
<form id='registerForm' name='registerForm' method='post' action="" >
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" required minlength="2" />
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" required />
</div>
<div>
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
<script type="text/javascript" src="jq213.js"></script>
<script type="text/javascript" src="validate.min.js"></script>
<script>
$(function() {
$("#registerForm").validate({
rules:{
name:{
required:true,
minlength:2
},
email:{
required:true,
email:true
},
},
messages:{
name:{
required:"pls enter your name",
minlength:"minimum 2 char for name"
},
email:{
required:"required",
email:"Enter a valid email"
}
}
});
});
</script>
#4
You code works fine and the problem is that you are missing your jQuery file. Add those 2 line of code and it should work.
您的代码工作正常,问题是您缺少jQuery文件。添加这两行代码,它应该工作。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
AND ALSO IMPORTANT
而且也很重要
Use $().ready(function ()
~~
使用$()。ready(function()~~
$().ready(function () {
$("#registerForm").validate({
rules: {
name: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
}
},
messages: {
name: {
required: "pls enter your name",
minlength: "minimum 2 char for name"
},
email: {
required: "required",
email: "Enter a valid email"
}
}
});
});
#5
You could try 3 things:
你可以试试3件事:
- Add type="text/javascript" in your third
"<script></script>"
- Start with $(document).ready(function(){}) before using jQuery functions;
- View page source and try clicking to your src link to found whether they are working or not! If not, maybe the link has changed to wrong when you transfer to PHP.
在第三个“
在使用jQuery函数之前,先从$(document).ready(function(){})开始;
查看页面源代码并尝试单击您的src链接以查看它们是否正常工作!如果没有,转移到PHP时,链接可能已更改为错误。
Hope it will help!
希望它会有所帮助!