Kitsakorn P Kitsakorn P - 23 days ago 12
AngularJS Question

How to use if with data callback from $

I'm trying to get my app run into my if statement but it went down to else statement which is not what I want. callback data is "Pass" which is correct for my if condition but it still ran down to else. Please take a look at my code.

js file

$, postdata
).success(function (data, status, headers, config) {
//if (data) {

// $scope.PostDataResponse = data;
if (data === "Pass"){

setCookie("Username", $scope.formdata.Username);
setCookie("cafe_id", $scope.formdata.cafe_id);

alert("ลงทำเบียนสำเร็จ !");

// $scope.reset();
// $scope.getData();

}else{ <--- ran down to else statement
alert(data); <--- callback value is "Pass" which is match with my if condition.

PHP file

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods : GET,POST,PUT,DELETE,OPTIONS");
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With, X-YOUR-CUSTOM-HEADER');
header("Content-Type : application/json");
header("Accept : application/json");

$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "middlework";

$conn = new mysqli($serverName,$userName,$userPassword,$dbName);

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$strSQL = "INSERT INTO users ";
$strSQL .="(Username,Password,Firstname,Lastname,Email,Tel,AccountStat,VerifyCode,Verifystat) ";
$strSQL .="VALUES ";
$strSQL .="('".$request->Username."','".$request->Password."','".$request->Firstname."' ";
$strSQL .=",'".$request->Lastname."','".$request->Email."','".$request->Tel."','User','".session_id()."','None' ) ";

mysqli_query($conn,$strSQL) or die(mysqli_error($conn));

$insertcafe = "INSERT INTO cafe (cafe_id,Username,CafeName) VALUES ('1' , '".$request->Username."', '".$request->CafeName."')";

$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = ""; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = ""; // GMAIL username
$mail->Password = "1100501068349"; // GMAIL password
$mail->From = ""; // "";
$mail->FromName = "ThaiCoffeeShopOnline"; // set from Name
$mail->Subject = "ยืนยันการสมัครสมาชิก ThaiCoffeeShopOnline";
$mail->Body = "ขอขอบคุณที่สมัครเป็นสมาชิกกับเรา กรุณาคลิก Url ด้านล่างเพื่อทำการ Activate บัญชีของคุณ</br>

$mail->AddAddress($request->Email); // to Address

if (!$mail->send()) {
//echo "ไม่สามารถส่ง email: " . $mail->ErrorInfo;
echo "EmailFail";
} else {
echo "Pass ";
//echo "Save Cafe Done.[".$insertcafe."]";

echo "";
//echo mysqli_error($conn);
//echo "ไม่สามาถบันทึกข้อมูล[".$insertcafe."]";


value is Pass


Your PHP file has echo "Pass "; which writes "Pass" followed by a space which you are comparing against the string "Pass" without a space.

In addition, you might also have a blank line at the end of your PHP file which will also be included in the output. This can be worked around by removing the ?> tag from the end.