我有一个使用 jQuery/Ajax/PHP 发送数据的简单表单。PHP 代码在将输入发送到数据库之前验证输入,如果输入无效,则将错误消息返回到响应 div。
它在我的计算机和我自己的服务器上运行良好。但是当我将它上传到客户端的服务器时,它并没有按预期工作。当我从客户端的服务器访问页面时,我注意到以下内容:
- 仅当所有输入字段都有值时,验证结果才会发送到响应 div。如果任何字段为空,则不会发生任何事情,也不会返回验证消息。 
- 这似乎不是机器问题,因为我使用同一台计算机访问 3 个副本,一个在我的本地主机上,一个在我的服务器上,一个在客户端服务器上。 
这是代码;jQuery:
$(document).ready(function() {
    $('#signup').click(function() {
        var queryString = 'ajax=true';
        var txtName = encodeURIComponent($('#txtName').val());  
        if(txtName.length > 0){
            txtName = txtName.replace(/\%/g, '-');
        }
        var txtEmail = escape($('#txtEmail').val());
        var txtPhone = encodeURIComponent($('#txtPhone').val());
        if(txtPhone.length > 0){
            txtPhone = txtPhone.replace(/\%/g, '-');
        }
        var txtPhoneCode = encodeURIComponent($('#txtPhoneCode').val());        
        if(txtPhoneCode.length > 0){
            txtPhoneCode = txtPhoneCode.replace(/\%/g, '-');
        }
        queryString = queryString + '&txtEmail=' + txtEmail;
        queryString = queryString + '&txtName=' + txtName;
        queryString = queryString + '&txtPhone=' + txtPhone;
        queryString = queryString + '&txtPhoneCode=' + txtPhoneCode;
        $.ajax({
            type: "GET",
            url: 'send.php',
            data: queryString ,
            success: function(msg) {
                $('#response').html(msg);
            }
        });
        return false;
    });
});
PHP页面:
<?php
if(isset($_GET['ajax']) && ($_GET['ajax'] == 'true')){
    $name = trim($_GET['txtName']); // coming from input text
    $email = trim($_GET['txtEmail']); // coming from input text
    $phone = trim($_GET['txtPhone']); // coming from input text
    $phonecode = trim($_GET['txtPhoneCode']); // coming from a select
    if(strlen($name) == 0){
        echo 'Please enter your name';
    }
    elseif(strlen($email) == 0){
        echo 'Please enter your email';
    }
    elseif(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $email)){
        echo 'Please enter a valid email';
    }
    elseif(strlen($phonecode) == 0){
        echo 'Please select phone code';
    }
    elseif(strlen($phone) == 0){
        echo 'Please enter your phone';
    }
    elseif(!preg_match("/^[0-9]*$/i", $phone)){
        echo 'Please enter a valid phone';
    }
    else{
        require('config.php');
        // send to mysql db
        $email = stripslashes($email);
        $name = urldecode(str_replace('-', '%', $name));
        $phone = urldecode(str_replace('-', '%', $phone));
        $phonecode = urldecode(str_replace('-', '%', $phonecode));
        $dt = gmdate("Y-m-d H:i:s");
        $sql = "insert into subscribers(datecreated, name, email, phone, phonecode) values('$dt', '$name', '$email', '$phone', '$phonecode')";
        $result = mysql_query($sql) or die('Error: Failed to save subscription!');
        // redirect
        echo '<script>setTimeout(function(){ window.location = "thankyou.html#ty"; }, 0);</script>';
    }
}
?>