duanche4578 2015-11-25 05:34
浏览 4
已采纳

ajax结果显示错误

I have a simple problem but I couldn't find out the solution. the below code is my POST ajax code.

once Its submitted I got the result from the PHP file. once this alert is work alert(addcenterresult);

if OK its alerted 1 if not 2 but the result if every time showing alert ("SomeThing Wrong");

I checked my PHP everything is good. I assumed the error in my ajax code but I found nothing !!!

Please can you help

$('#add-jeddah').click(function () {
    var centername = $('#JeddahCenterNAme').val();
    var dataString = 'centername=' + centername;

    if (centername == '') {
        alert("Please write the center name");


    } else {
        // AJAX Code To Submit Form.
        $.ajax({
            type: "POST",
            url: "func/addjeddahCenter.php",
            data: dataString,
            cache: false,
            success: function (addcenterresult) {
                alert(addcenterresult);
                if (addcenterresult == "1") {
                    alert("Well Done");

                } else {
                    alert("SomeThing wrong");
                }
            }
        });


    }

    //  END add-Jeddah function
});

My PHP

$nameCenter = $_POST['centername'];
$addresult = "INSERT INTO centers (id,centerName) VALUES ('','$nameCenter')";

if (mysql_query($addresult)) {
    echo "1";
        } else {
             echo "2";
    }
  • 写回答

1条回答 默认 最新

  • duanhe2027 2015-11-25 06:00
    关注

    Try using the jquery trim when you match the condition equal to 1 or 2 ,sometimes whitespace is returned with the string

    give a try to following lines

    if ($.trim(addcenterresult) == "1") {
        alert("Well Done");
    } else if ($.trim(addcenterresult) == "2"){
        alert("SomeThing wrong");
    }
    

    This would also verify that the response you receive is either between 1 or 2 only.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码