1

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+"&nbsp;&nbsp;|&nbsp;&nbsp;<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

asked May 24, 2012 at 5:54
9
  • 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? Commented 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). Thanks Commented May 24, 2012 at 6:22
  • Can please print the value of data in success function and provide the output here ? Commented 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. Commented 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 spaces Commented May 24, 2012 at 6:43

1 Answer 1

1

Use this to compare if($.trim(data)!="error")

And don't recheck for if($.trim(data)=="error")

use

if($.trim(data)!="error")
{
 //
}
else{
//
}
answered May 24, 2012 at 6:55
Sign up to request clarification or add additional context in comments.

4 Comments

Worked!!!!!Gem subirkumar.Thanks to you too Codemaster Gabriel .Also can't I eliminate the white spaces on server side.I tried adding this 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
stripslashes doesnot remove the whitespaces on the server side. trim(), ltrim(), rtrim() are the functions that are available to remove the whitespaces even on the server side.
I guess you have a empty space after the php close tag ?><space> Please check.
@subirkumarsao No there was no space after close tag as you mentioned(cross checked it). Codemaster Gabriel ohhh...I have been away from php for some while and I think i need to revisit the functions section on php.net!! :P Thanks both for the inputs!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.