2011-05-09 85 views
0

好了,所以我有这个功能PHP函数无法读取jquery POST数据?

<?php 

/** 
* @author Mitchell A. Murphy 
* @copyright 2011 
*/ 
include ('func_lib.php'); 
connect(); 
echo (check($_POST['input']) ? 'true' : 'false'); 
function check($args) 
{ 
    $args = strtolower($args); 
    $checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i"; 
    if (preg_match($checkemail, $args)) 
    { 
     //logic for email argument 
     $sql = "SELECT * FROM `users` WHERE `email`='" . $args . "'"; 
     $res = mysql_query($sql) or die(mysql_error()); 
     echo "type=email:"; 
     if (mysql_num_rows($res) > 0) 
     { 
      return true; 
     } else 
     { 
      return false; 
     } 
    } else 
    { 
     //logic for username argument 
     $sql = "SELECT * FROM `users` WHERE `username`='" . $args . "'"; 
     $res = mysql_query($sql) or die(mysql_error()); 
     echo "type=username:"; 
     if (mysql_num_rows($res) > 0) 
     { 
      return true; 
     } else 
     { 
      return false; 
     } 
    } 

} 

?> 

功能应该由这个jQuery脚本访问:

$('form.register .submit').click(validateRegister); 

function validateRegister() { 

    //Variables 
    var emailExists = false; 
    var userExists = false; 
    var $error = ""; 

    //Executes functions 
    email(); 


    function email() { 
     var $error = $('#email .error'); 
     var input = $('#email input').val(); 
     var emailRE = /^.*@.+\..{2,5}$/; 
     if (input.match(emailRE)) { 
      $error 
       .html('<div>Proper Email Format: <span>[email protected]</span></div>') 
       .animate({ 
       'left': '-130px', 
       'opacity': '0' 
      }); 

      //Checks for Existing Email 

      function checkExisting_email() { 
       $.ajax({ 
        type: 'POST', 
        url: 'includes/checkExist.php', 
        data: input, 
        statusCode: { 
         404: function() { 
          alert('page not found'); 
         } 
        }, 

        success: function (data) { 
         alert(data); 
        }, 
        error: function() { 
         alert("error bro"); 
        } 
       }); 

      } 
      emailExists = checkExisting_email(); 

      //If it exists 
      if (emailExists) { 
       alert("This email already exists!"); 
      } else if (emailExists == false) { 
       alert("Email doesnt exist!"); 
      } 

     } else { 
      //Email doesn't match 
      $error 
       .html('<div>Proper Email Format: <span>[email protected]</span></div>') 
       .animate({ 
       'left': '-150px', 
       'opacity': '1' 
      }); 
     } 
    } 
    return false; 
} 

但由于某些原因脚本(JS)不发送任何数据?如果是这样,我该如何引用它。我是后端开发人员,但做了JavaScript的设计师让我解决了这个问题。我知道PHP的作品,因为我做了一个测试的形式与这个HTML标记发送数据:

<form action="includes/checkExist.php" method="post"> 
<input type="text" name="input" /> 
<input type="submit" name="submit" /> 
</form> 

这工作...那么,为什么是jQuery的返回为NULL输入?

回答

1

看到checkExisting_email()不返回任何东西,所以emailExists = checkExisting_email();不会设置emailExists。此数据仅在回调函数中提供,该函数仅在alert()上显示结果。

为了方便起见,请使用jQuery ajax验证字段remote。检查documentation and sample

1

您需要为“数据”传递键/值对,而不仅仅是值。

原样,你的表格是要与查询字符串看起来像这样贴:

[email protected] 

它应该是:

数据:{输入:输入},

这将将查询字符串设置为:

[email protected] 

另外,由于您正在将值一个按ID的元素,你不需要指定输入标签。

var input = $('#email').val(); 
+0

谢谢:)我有一点之前,但这会帮助我,我没有:P – 2011-05-09 21:21:48