PHP返回AJAX调用但代码语句无法识别

时间:2017-10-15 14:41:00

标签: php jquery ajax

我完全迷失了,我使用下面的AJAX将数据发布到PHP并回显“1”。但是,代码无法进入“if(result == 1)”代码块。它总是进入我试图提醒(结果)的ELSE块。它显示1没有任何问题。为我糟糕的解释道歉。非常感谢任何帮助。

 $.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
                });     



                }




            }

        });

我的PHP代码如下:

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";

} 

失败的可能原因是什么?

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

set_node_env_variables () {        
    docker-machine ssh remote-machine \
      bash ./set-env-variables.sh \
           "$ENV_VARIABLE_ONE" \
           "$ENV_VARIABLE_TWO" \
           "$ENV_VARIABLE_THREE" \
           "$ENV_VARIABLE_FOUR"        
}

这将从字符串中删除任何尾随空格。或者,您可以确保result = trim(result); if(result == 1){ 标记之后或之前没有空格。或者更好的是,您可以从PHP提交<?php ?>响应,如:

json

在你的js中有类似的东西:

$result = ['status' => 'success'];
echo json_encode($result);