I have a issue with my ajax form submission.I am dynamically submitting a form and using php at the server side to process it.This is the ajax success function.
$.ajax({
type: "POST",
url: "register.php",
data: "uname="+uname+"&eid="+eid+"&pwd="+pass+"&cpwd="+cpass+"&country="+coun+"&contact="+contact,
dataType: "html",
success: function(data){
if(data!="error")
{
//alert(data);
$("#user_status", window.parent.document).html("Welcome "+data+" | <a href='forum/logout.php'>Logout</a>");
if(window.parent.document.getElementById('post_user_name'))
$("#post_user_name", window.parent.document).html(msg);
parent.$.fancybox.close();
}
if(data=="error")
{
//alert(data);
$("#status").html("<span><center><font class='formright err_msg' style='width:176px;'>The user is already register with us.</font><center></span>");
return false;
}
Now if the user is valid he is logged in and f not there has to be an error like "Already exists".The valid part works fine but for invalid I return an error from the php file but still my error message doesn't show up and just error is printed on the screen.I am using fancybox for my forms(jquery fancybox) PHP code is
if($_POST['pwd']==$_POST['cpwd'])
{
$username = $_POST['uname'];
$email = $_POST['eid'];
$password = md5($_POST['pwd']);
$cpassword = $_POST['cpwd'];
$contact_no = $_POST['contact'];
$country = $_POST['country'];
$cnt = $checkUser['cnt'];
if($cnt!=0)
{
echo "error";
//exit;
/*$_SESSION['error_msg'] = 'Email Address already exists';
redirect_to_link("index.html");*/
}
else
{
//echo "entered here";
$userArray = array();
//$user = return_post_value($_POST['uname']);
$userArray['uname'] = return_post_value($_POST['uname']);
$userArray['email'] = return_post_value($_POST['eid']);
$userArray['password'] = md5(return_post_value($_POST['pwd']));
$userArray['contact_no'] = return_post_value($_POST['contact']);
$userArray['country'] = return_post_value($_POST['country']);
//print_r($userArray);
//exit;
$userObj->addUserValue($userArray);
$_SESSION['username']= $userArray['uname'];
echo $userArray['uname'];
// return $user;
}
The echo $userArray['uname']; part works but echo "error" doesn't.Checked in Firebug response header,i can see the error word returned.
Can anyone throw some light on it?
Thanks
-
Can you do an alert on data on Javascript after you get the response. And why are you doing a return false; on the if(data=='error') condition?Subir Kumar Sao– Subir Kumar Sao2012年05月24日 05:59:38 +00:00Commented May 24, 2012 at 5:59
-
yes tried that but it does not reach there it still goes into the (data!="error") loop!!I am not getting why when its not equal!! :P I am trying to do something similar here proptiger.com in the header Register link (form). ThanksKillABug– KillABug2012年05月24日 06:22:32 +00:00Commented May 24, 2012 at 6:22
-
Can please print the value of data in success function and provide the output here ?Akhilesh Sharma– Akhilesh Sharma2012年05月24日 06:34:57 +00:00Commented May 24, 2012 at 6:34
-
@subirkumarsao The return false is bcoz after the error i don't want the form to submit and the user already exits message to be displayed.Its a sign up form not a login one.KillABug– KillABug2012年05月24日 06:37:43 +00:00Commented May 24, 2012 at 6:37
-
We need to see whats in that data. As you are sayings you can see "error" in the firebug response then it can be white spaces before and after the "error". Do a alert before the if(data!="error") and post the output. Something like alert('A'+data+'A');. Makes easy to understand if there are white spacesSubir Kumar Sao– Subir Kumar Sao2012年05月24日 06:43:18 +00:00Commented May 24, 2012 at 6:43
1 Answer 1
Use this to compare if($.trim(data)!="error")
And don't recheck for if($.trim(data)=="error")
use
if($.trim(data)!="error")
{
//
}
else{
//
}
4 Comments
stripslashes to remove any whitespaces,but did not work!! And also the part from where I reffered this example doesn't have it!! Then what's the reason?can you please comment on this and elaborate a bit. Thanks