我正在尝试使用php和ajax验证我的注册表单。但我没有得到任何回报。这是我的 ajax 代码。
<script type="text/JavaScript">
function frmValidation(str)
{
if (str=="")
{
document.getElementById("txtError").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtError").innerHTML=xmlhttp.responseText;
}
xmlhttp.open("GET","validation.php?value="+str,true);
xmlhttp.send();
}
}
我的php文件名为验证.php是
<?php
$value= $_REQUEST["value"];
$pass= $_REQUEST["value"];
$error="";
if($value=="")
{
echo "This is a mandatory field";
}
elseif($value=="fname")
{
if(!preg_match("/^[a-zA-Z ]*$/", $value))
{
echo "Please enter the correct First Name";
}
}
elseif($value=="lname")
{
if(!preg_match("/^[a-zA-Z ]*$/", $value))
{
echo "Please enter the correct Last Name";
}
}
elseif($value=="designation")
{
if(!preg_match("/^[a-zA-Z ]*$/", $value))
{
echo "Please enter the correct designation";
}
}
elseif($value=="suburb")
{
if(!preg_match("/^[a-zA-Z ]*$/", $value))
{
echo "Please enter the correct suburb";
}
}
elseif($value=="city")
{
if(!preg_match("/^[a-zA-Z ]*$/", $value))
{
echo "Please enter the correct city name";
}
}
elseif($value=="state")
{
if(!preg_match("/^[a-zA-Z ]*$/", $value))
{
echo "Please enter the correct state";
}
}
elseif($value=="country")
{
if(!preg_match("/^[a-zA-Z ]*$/", $value))
{
echo "Please enter the correct country";
}
}
elseif($value=="pincode")
{
if(!preg_match("/^[a-zA-Z0-9 ]*$/", $value))
{
echo "Please enter the correct designation";
}
}
elseif($value=="mobile")
{
if(!preg_match("/^['+0-9'-'(')'s]*$/", $value))
{
echo "Please enter the correct designation";
}
}
elseif($value=="email")
{
if(!preg_match("/^'w+(['.-]?'w+)*@'w+(['.-]?'w+)*('.'w{2,3})+$/", $value))
{
echo "Please enter the correct email format";
}
}
elseif($value=="cpass")
{
if($pass==$value)
{
echo "Confirm password is wrong";
}
}
?>
HTML表单是这样的
<div class="col-lg-12">
<div class="container">
<h1>Sign Up form</h1>
<form role="form" action="signup.php" method="post">
<div class="panel-group">
<div class="panel panel-primary">
<div class="panel-heading"> General Information </div>
<div class="panel-body">
<div class="col-sm-6">
<div class="form-group">
<label for="First Name">First Name</label>
<input type="text" class="form-control" onblur="frmValidation(this.value)" tabindex="1" id="First Name" placeholder="First Name" name="fname"><span id="txtError" style="color: red"></span>
</div>
<div class="form-group">
<label for="Company Name">Company Name</label>
<input type="text" tabindex="3" class="form-control" id="Company Name" placeholder="Company Name" name="cname">
</div>
<div class="form-group">
<label for="Address">Address</label>
<input type="text" tabindex="5" class="form-control" id="address" placeholder="Address" name="address">
</div>
<div class="form-group">
<label for="city">City</label>
<input type="text" tabindex="7" class="form-control" id="City" placeholder="City" name="city">
</div>
<div class="form-group">
<label for="country">Country</label>
<input type="text" tabindex="9" class="form-control" id="country" placeholder="Country" name="country">
</div>
<div class="form-group">
<label for="mobile">Mobile No.</label>
<input type="text" tabindex="11" class="form-control" id="suburb" placeholder="Mobile No." name="mobile">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="Last Name">Last Name</label>
<input type="text" tabindex="2" class="form-control" id="Last Name" placeholder="Last Name" name="lname">
</div>
<div class="form-group">
<label for="Designation">Designation</label>
<input type="text" tabindex="4" class="form-control" id="Designation" placeholder="Designation" name="designation">
</div>
<div class="form-group">
<label for="suburb">Suburb</label>
<input type="text" tabindex="6" class="form-control" id="suburb" placeholder="Suburb" name="suburb">
</div>
<div class="form-group">
<label for="state">State</label>
<input type="text" tabindex="8" class="form-control" id="state" placeholder="State" name="state">
</div>
<div class="form-group">
<label for="pincode">Pincode</label>
<input type="text" tabindex="10" class="form-control" id="pincode" placeholder="Pincode" name="pincode">
</div>
<div class="form-group">
<label for="dob">Date Of Birth</label>
<input type="text" tabindex="12" class="form-control" id="dob" placeholder="Date of Birth" name="dob">
</div>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading"> Login Details </div>
<div class="panel-body">
<div class="col-sm-6">
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" tabindex="13" class="form-control" id="email" placeholder="Email Address" name="email">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="password">Password</label>
<input type="password" tabindex="14" class="form-control" id="password" placeholder="Password" name="pass">
</div>
<div class="form-group">
<label for="Confirm password">Confirm Password</label>
<input type="password" tabindex="14" class="form-control" id="confirm password" placeholder="Confirm Password" name="cpass">
</div>
</div>
</div>
<div class="form-group">
<input type="submit" tabindex="15" class="form-control btn btn-primary" value="Submit">
</div>
</div>
</div>
</form>
</div>
</div>
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Script to Activate the Carousel -->
<script>
$('.carousel').carousel({
interval: 5000 //changes the speed
})
</script>
</div>
在 html 形式中,我试图至少为第一个字段获取值,只是为了检查天气是否正常工作。但我没有得到任何回报。请让我知道问题出在哪里,因为我是使用 ajax 的新手。请帮忙!谢谢!
您的格式不正确,这隐藏了代码中的错误:您告诉计算机"何时收到 AJAX 响应,然后发送 AJAX 请求"。当然,除非您先发送请求,否则不会有任何响应做出反应。
这是您的代码,已更正:
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtError").innerHTML=xmlhttp.responseText;
}
// we're still in the "onreadystatechange" function here!
// xmlhttp.open("GET","validation.php?value="+str,true);
// xmlhttp.send();
}
// send the request from *here*
xmlhttp.open("GET","validation.php?value="+str,true);
xmlhttp.send();