gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:37:43 25 4
gpt4 key购买 nike

我完全迷失了,我使用下面的AJAX将数据发送到PHP并回显“1”。但是,代码无法进入“if (result==1)”代码块。它总是进入我试图警告(结果)的 ELSE block 。显示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";

}

失败的可能原因是什么?

最佳答案

尝试以下操作:

result = trim(result);
if(result == 1){

这将从字符串中删除所有尾随空格。或者您可以确保 <?php ?> 前后没有空格标签。或者更好的是,您可以提交 json PHP 的响应如下:

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

在你的js中,类似:

 $.ajax({
url: $form.attr('action'),
type: 'POST',
data: $form.serialize(),
dataType: 'json',
success: function(result)
{
if (result.status=="success")
}
});

关于PHP返回AJAX调用但代码语句无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46756253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com