[英]Javascript doesn't work on WAMP but works fine without it
I just started learning PHP and WAMP. 我刚刚开始学习PHP和WAMP。 Before that I created a set of webpages using HTML,CSS, and JS.
在此之前,我使用HTML,CSS和JS创建了一组网页。 The JS is for validating the form, and if everything goes well it will jump to a php page that insert data into sql database.
JS用于验证表单,如果一切顺利,它将跳转到将数据插入sql数据库的php页面。
The simplified form.html and validation.js are: 简化的form.html和validation.js是:
function validation() {
var emailCheck = email.search(/^[a-zA-z0-9_]+\@[a-zA-z0-9_]+\.[a-zA-z]{2,3}$/);
if (emailCheck==-1) alert("Please enter a valid email address");
else document.getElementById("signup").action = "success.php";
}
The Form code 表格代码
<form id="signup" class = "signup" method="POST">
.....
<input type="text" name="email" id="email" required/>
....
<button class = "button3" onclick = "validation()">Submit</button>
</form>
The success.php is simply to insert data to the mySQL database. success.php只是将数据插入到mySQL数据库中。
Now I run the form.html without using the localhost, the javascript works perfectly. 现在,我在不使用localhost的情况下运行form.html,javascript可以完美运行。 The alert window shows up if I enter wrong stuff.
如果我输入错误的内容,将显示警报窗口。
If I run localhost/form.html with WAMP, when I enter wrong stuffs, the javascript doesn't work. 如果我使用WAMP运行localhost / form.html,则当我输入错误的内容时,javascript无法正常工作。 The alert window doesn't show up.
警报窗口不显示。 BUT when I pass the validation, it successfully jumps to the success.php.
但是,当我通过验证时,它会成功跳转到success.php。
Therefore, I consider the Javascript is working but somehow part of it doesn't since the javascript is the only connection between the form.html and success.php. 因此,我认为Javascript可以正常工作,但由于JavaScript是form.html和success.php之间的唯一连接,因此部分原因无法正常工作。
Any idea? 任何想法?
variable "email" is undefined, so you can't using search function on a undefined variable. 变量“电子邮件”是未定义的,因此您不能对未定义的变量使用搜索功能。 You need get the email value first.
您需要首先获取电子邮件值。
function validation() {
var emailCheck = document.getElementById("email").value.search(/^[a-zA-z0-9_]+\@[a-zA-z0-9_]+\.[a-zA-z]{2,3}$/);
if (emailCheck==-1) alert("Please enter a valid email address");
else document.getElementById("signup").action = "success.php";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.