dongmei5168 2013-04-21 12:10
浏览 16
已采纳

jQuery AJAX帖子不适用于所有计算机

I have a simple form that sends data using jQuery/Ajax/PHP. The PHP code validates the input before it sends it to the database and returns an error message to the response div if the input is invalid.

It works great on my computer and on my own server. But when I upload it to the client's server it doesn't work as expected. I noticed the following when I access the page from the client's server:

  • The validation result is being sent to the response div only if ALL the input fields have values. If any of the fields is empty, then nothing happens and no validation message is returned.

  • It doesn't seem to be a machine issue because I'm using the same computer to access the 3 copies, the one on my localhost, the one on my server, and the one on the client's server.

Here is the code; the 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;
    });
});

The PHP page:

<?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>';
    }
}
?>
  • 写回答

1条回答 默认 最新

  • douzao9845 2013-04-21 12:36
    关注

    You are not posting data to the server since you are setting type: "GET".

    This means that an HTTP GET request is sent, not HTTP POST. GET requests are typically cached by the client and therefore you may experience that no request is sent at all (when some combinations of field values are used) because the response of that request is already in the client's cache.

    You should change your code (both javascript and php) to use HTTP POST instead. The reason for this is twofold:

    1. POST responses are not cached, so a new request will be sent each time you submit.
    2. GET should not be used for requests that may have side effects.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图