表格提交 - 成功

时间:2009-12-16 03:02:11

标签: php jquery forms

好吧,我有一个非常奇怪的表格提交问题,我浪费了几天但仍然无法弄清楚: 1)当我正确填写表格(对于新用户)运作良好 2)如果我尝试使用现有电子邮件注册用户...它只是刷新而不是抛出错误 3)如果我在尝试失败后提交表格,虽然数据已提交到数据库,但我没有收到任何成功标志 这是我的代码:

 $(function() {  
 $("#send").click(function() {  
    // validate and process form here  
//var email = $("#email").val();
//var lname =  $("input#lname").val();
var dataString = $("#pForm").serialize();
//alert (dataString);
        $.ajax({ 
            type: "POST",
            url: "../php/insertUser.php",
            data: dataString,
            success:function(msg, status){
        //alert (dataString);
        var reply = parseInt(msg);
        alert(status+ "" + msg);
        if(reply ==1){
            alert('Email address already exists in our members database.\nPlease try another address and then submit it again!');
        }
        else if(reply ==2){ //do nothing
        //  alert('You have one or more empty fields!\nPlease provide all the information required and then submit it again!');
        }
        else if(reply == 0){
        $('#pForm').hide('fast');
        $('#accForm').show('slow');
        }
                              }
                });      

  });  
});  

我的php看起来像这样:

 <?php 
 include ('databaseCon.php');
 $err = 0;
//echo $data= $_POST['dataString'];
 $accType = $_POST['accType'];
 $fname =$_POST['fname'];
 $lname = $_POST['lname'];
 $email = $_POST['email'];

 $country = $_POST['country'];
 $state = $_POST['state'];
 $city = $_POST['city'];



 // check for empty fields
 if (($accType == "") || ($fname == "") || ($lname == "") || ($email == "") || ($country == "") || ($city == "")){
$empty = true;
}

if ($email != ""){
    $em = mysql_query("Select Email from Users where Email = '$email';") or die(mysql_error());
    $row = mysql_num_rows($em);

    }
//else{echo "unknown error 2 " + $email + "\n";}
    if ($empty == false){
        //echo ""+ $accType +"\n"+ $fname +"\n"+ $lname +"\n"+ $email +"\n"+ $country +"\n"+  $state +"\n"+ $city + "" ;
        if($row == 0){
        echo $users = mysql_query("Insert into Users values('','$fname', '$lname', '$email', '$accType', '$country', '$state', '$city');") or die(mysql_error());   
        }
        else{
            $err = 1;}
        }
    else{
    $err = 2;
    }
 echo $err;

 ?>

任何想法并感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您是否通过在ajax调用上实现错误处理程序进行了测试?

在一个不相关的说明中,您确实需要对查询使用某种形式的数据转义/数据库保护,最好是准备好。

答案 1 :(得分:0)

if(reply ==1){
    alert('Email address already exists in our members database.\nPlease try another address and then submit it again!');
    return false;
}

试试这个。