2013-04-21 96 views
2

我有一个简单的表单,使用jQuery/Ajax/PHP发送数据。 PHP代码在将输入发送到数据库之前验证输入,并在输入无效时将错误消息返回给响应div。jQuery AJAX后并不适用于所有电脑

它在我的电脑和我自己的服务器上效果很好。但是,当我将其上传到客户端的服务器时,它无法按预期工作。我注意到以下,当我从客户端的服务器访问这些页面:

  • 验证结果被发送到响应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>'; 
    } 
} 
?> 
+1

你有没有正确导入jQuery库 – 2013-04-21 12:13:29

+1

在你的浏览器的控制台中的任何错误? – nnnnnn 2013-04-21 12:15:32

+0

@穆罕默德·阿迪尔:是的。问题是仅当所有表单域都有值时才进行验证。 – 2013-04-21 12:28:32

回答

3

您还没有发布数据到服务器后要设置type: "GET"

这意味着发送HTTP GET请求,而不是HTTP POST。 GET请求通常由客户端缓存,因此您可能会遇到根本没有请求发送(使用某些字段值组合)的原因,因为该请求的响应已经存在于客户端的缓存中。

你应该改变你的代码(javascript和php)来使用HTTP POST。造成这种情况的原因有两个:

  1. POST缓存没有被缓存,所以每次提交时都会发送一个新的请求。
  2. GET不应该用于可能有副作用的请求。
+0

你是一个真正的古鲁花花公子。我非常感谢你的帮助。我坚持了近一个星期。欢呼:) – 2013-04-21 12:41:18

相关问题