Javascript 当ajax调用PHP进行验证时,PHP没有响应

Javascript 当ajax调用PHP进行验证时,PHP没有响应,javascript,php,ajax,Javascript,Php,Ajax,我正在尝试使用php和ajax验证我的注册表单。但我没有得到任何回报。这是我的ajax代码 <script type="text/JavaScript"> function frmValidation(str) { if (str=="") { document.getElementById("txtError").innerHTML=""; return; }

我正在尝试使用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();
    }  
    }

功能FRM验证(str)
{
如果(str==“”)
{
document.getElementById(“txtError”).innerHTML=“”;
返回;
} 
if(window.XMLHttpRequest)
{
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
} 
其他的
{  
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtError”).innerHTML=xmlhttp.responseText;
}
open(“GET”、“validation.php?value=“+str,true”);
xmlhttp.send();
}  
}

我的php文件validation.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>

报名表
一般资料
名字
公司名称
地址
城市
国家
流动电话号码。
姓
任命
郊区
陈述
枕形
出生日期
登录详细信息
电子邮件地址
密码
确认密码
$('.carousel')。carousel({
间隔:5000//改变速度
})
在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();

您的格式不正确,这在代码中隐藏了一个错误:您告诉计算机“当收到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();

AJAX请求时请检查您的浏览器控制台,可能有一些提示。您的
xmlhttp.open()
xmlhttp.send()调用在您的
onreadystatechange
回调中。。。它们应该在它之外。当AJAX请求时,请检查您的浏览器控制台,可能有一些提示。您的
xmlhttp.open()
xmlhttp.send()调用在您的
onreadystatechange
回调中。。。他们应该在外面,谢谢你的帮助。我在代码方面也遇到了一些其他问题。我发送文本输入字段的值,在php中,我试图获得文本输入字段的名称。现在另一个prblm是我想要输入字段的名称。你能帮我吗?如何使用ajax将输入字段的名称发送到php你可以把它命名为任何东西,更详细的解释:如果你有不同的问题,发布一个新的问题-在评论中四处询问不是我们在这里做的方式。谢谢你的帮助。我在代码方面也遇到了一些其他问题。我发送文本输入字段的值,在php中,我试图获得文本输入字段的名称。现在另一个prblm是我想要输入字段的名称。你能帮我吗?如何使用ajax将输入字段的名称发送到php你可以把它命名为任何你喜欢的东西,更详细的解释:如果你有不同的问题,发布一个新的问题-在评论中四处询问不是我们在这里做的方式。