$(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;
?>
任何想法并感谢您的帮助!
答案 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;
}
试试这个。