Reputation: 25
i am totally lost, I used the AJAX below to post data to PHP and echo "1". However, the code couldnt get into the "if (result==1)" code block. It always go into the ELSE block I have attempted to alert(result). It shows 1 without any problem. Apologize for my bad explanation. Any help is deeply appreciated.
$.ajax({
url: $form.attr('action'),
type: 'POST',
data: $form.serialize(),
success: function(result) {
// ... Process the result ...
//alert(result);
if (result=="1")
{
swal({
type: "success",
title: "Congratulation!",
text: "Please check your email inbox",
animation: "slide-from-top",
showConfirmButton: true
}, function(){
var username = $("#username").val();
var password = $("#password").val();
});
}
else
{
//alert(result);
swal({
type: "error",
title: "",
text: result,
animation: "slide-from-top",
showConfirmButton: true
});
}
}
});
My PHP Code is as below:
if($dum=="TRUE")
{
$password2 = $_POST['password2'];
$fullname = $_POST['fullname'];
$country = $_POST['id_country'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$agent = $_POST['agent'];
$term = $_POST['term'];
$sql = "INSERT INTO usercabinet (username, password, password2, fullname, country, mobile, email, agent, term, emailconfirm, identityconfirm, feeds)
VALUES ('$username', '$password', '$password2', '$fullname', '$country', '$mobile', '$email', '$agent', '$term', '0', '0', 'Welcome to Our New Cabinet')";
if ($conn->query($sql) === TRUE) {
// "New record created successfully, Success!!<br>";
$_SESSION['username'] = $username;
$_SESSION['fullname'] = $fullname;
$_SESSION['country'] = $country;
$_SESSION['mobile'] = $mobile;
$_SESSION['email'] = $country;
$_SESSION['term'] = $term;
$_SESSION['emailconfirm'] = 0;
$_SESSION['identityconfirm'] = 0;
$_SESSION['feeds'] = "Welcome to Cabinet";
echo "1";
}
What could be the possible reason of fail?
Upvotes: 2
Views: 41
Reputation: 9396
Try the following:
result = trim(result);
if(result == 1){
This will remove any trailing spaces from the string. Or you can make sure there is no space after or before <?php ?>
tags. OR better yet, you can submit json
response from PHP Like:
$result = ['status' => 'success'];
echo json_encode($result);
And in your js something like:
$.ajax({
url: $form.attr('action'),
type: 'POST',
data: $form.serialize(),
dataType: 'json',
success: function(result)
{
if (result.status=="success")
}
});
Upvotes: 1