使用 javascript 到 php 的表单验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6526359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
form validation with javascript to php
提问by Ali Ben Messaoud
I find this code in W3Schools. It's about form validation with Javascript.
我在 W3Schools 中找到了这段代码。这是关于使用 Javascript 进行表单验证。
When the user tape wrong mail format, an alert is appear.
当用户磁带错误的邮件格式时,会出现警报。
When I check the code I see on form onsubmit="return validateForm();
, well the function return false in error case.
当我检查我在 form 上看到的代码时onsubmit="return validateForm();
,该函数在错误情况下返回 false。
So my question is how to get the boolean value from javascript to use it in PHP to write error message instead of use of message box.
所以我的问题是如何从 javascript 获取布尔值以在 PHP 中使用它来编写错误消息而不是使用消息框。
This is my code:
这是我的代码:
<html>
<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["email"].value
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>
</html>
Thanks in advance.
提前致谢。
Ali
阿里
采纳答案by Daniel Bingham
You can't get the boolean value from javascript. What you need to do is have an error div ready in advance. Something like this:
您无法从 javascript 获取布尔值。您需要做的是提前准备好错误 div。像这样的东西:
<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<div id="error"></div>
<input type="submit" value="Submit">
</form>
Add css to it to hide it. Then in your validation form, use javascript to alter the HTML of the error div.
向其中添加 css 以隐藏它。然后在您的验证表单中,使用 javascript 更改错误 div 的 HTML。
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
document.getElementById("error").innerHTML="Not a valid e-mail address";
return false;
}
The "return false;" part is to tell the form not to submit. As mentioned in other answers PHP and Javascript do not interface. PHP is server side, Javascript is client side. They don't talk to each other. They can, however, both modify the HTML. Just remember, Javascript always runs in the browser. So if it needs to send things to PHP you need to use AJAX to call a server side PHP script and send the information that way. Most of the time, however, when you are using Javascript, it isn't PHP that needs the information, it's the user. So using Javascript to modify the HTML does the trick.
“返回假;” 部分是告诉表单不要提交。正如其他答案中提到的,PHP 和 Javascript 没有接口。PHP 是服务器端,Javascript 是客户端。他们不互相交谈。但是,它们都可以修改 HTML。请记住,Javascript 始终在浏览器中运行。因此,如果需要向 PHP 发送内容,则需要使用 AJAX 调用服务器端 PHP 脚本并以这种方式发送信息。然而,大多数情况下,当您使用 Javascript 时,需要信息的不是 PHP,而是用户。所以使用 Javascript 来修改 HTML 就可以了。
回答by MRR0GERS
if (..)
{
document.getElementByID('error').InnerHTML = "Not a valid e-mail address!";
return false;
}
else
{
document.getElementByID('error').InnerHTML = "";
}
and in your markup add:
并在您的标记中添加:
<span id="error"></span>
回答by Joe
I wouldn't use PHP for this.
我不会为此使用 PHP。
<html>
<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["email"].value
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
document.getElementById('errorMsg').style.display = 'block';
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
<div id="errorMsg" style="display: none;">Not valid email</div>
</form>
</body>
</html>
This way you won't have to go back to your server.
这样您就不必返回到您的服务器。
回答by Major Productions
Outside of using AJAX, you can't. Remember that PHP is a server side language, and that JavaScript runs on the client in the browser. By the time PHP renders the page in the browser, it's done processing. What you need is two layers of validation - JavaScript validation AND PHP validation. Why? Because JavaScript can be turned off in the browser. That makes it useless as a security measure. JavaScript validation should only be used as a way to enhance the user's experience.
除了使用 AJAX,你不能。请记住,PHP 是一种服务器端语言,并且 JavaScript 在客户端的浏览器中运行。当 PHP 在浏览器中呈现页面时,它已完成处理。您需要的是两层验证 - JavaScript 验证和 PHP 验证。为什么?因为 JavaScript 可以在浏览器中关闭。这使得它作为一种安全措施毫无用处。JavaScript 验证仅应用作增强用户体验的一种方式。
Finally, don't go to w3schools. A lot of its information is invalid (see the site http://www.w3fools.comfor more info). Instead, use Mozilla's Developer Network: https://developer.mozilla.org/en-US/learn/
最后,不要去w3schools。它的很多信息都是无效的(有关更多信息,请参见站点http://www.w3fools.com)。相反,请使用 Mozilla 的开发者网络:https: //developer.mozilla.org/en-US/learn/
回答by Tim Almond
The easiest thing is to do the display in the function. Replace the alert with something like:-
最简单的就是在函数中做显示。用以下内容替换警报:-
document.getElementById("errormessage").innerHTML="Not a valid e-mail address";
document.getElementById("errormessage").innerHTML="无效的电子邮件地址";
You'll also need a DIV in your HTML with an id of errormessage.
您还需要在 HTML 中包含一个带有 errormessage id 的 DIV。