2

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?

4
  • console.log(result); and see what you get Commented Oct 15, 2017 at 14:46
  • @mega6382. Thanks for your reply.. Could you please guide me how to check trailing space?? I attempted with modifying echo"1" to $result =1; echo $result; but it stil fails.. I am new to this.. Commented Oct 15, 2017 at 14:48
  • Please check the data type of "result" by following alert(typeof result); Commented Oct 15, 2017 at 14:48
  • If the provided solution was helpful. Please be sure to accept it. Commented Nov 23, 2017 at 6:51

1 Answer 1

1

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")
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.