【问题标题】:keeps returning same alert message despite if it has been added or not.无论是否已添加,都会不断返回相同的警报消息。
【发布时间】:2018-04-12 20:09:02
【问题描述】:

我正在尝试向用户提供反馈。如果他们已经添加了一些东西,他们会收到一条消息,说这个景点已经添加到您的行程中。如果用户的行程中没有景点,他们将收到成功消息。虽然它总是说这个景点已经被添加了,即使它还没有。没有错误,有谁知道为什么会这样。下面是该函数的代码。

function addAttractionItinerary(ev) {
 var id=$(ev.target).parents('div.Attraction')[0].$data.attraction_id;
$.post('php/validation.php', {"CHECKRECORD" :1 ,"id" : id}, function(data){
if (data = 1) {
alert("This attraction has already been added to your itinerary")
handleModalClose();
} else if  (data = 0){ 
         $.ajax({
    url: 'php/itinerary.php',
    type: 'POST', //will send a POST request to the PHP back-end with the "attractionId" the user has clicked on
    dataType: 'json',
    data: {
        action: 'CREATE',
        attractionId: $(ev.target).parents('div.Attraction')[0].$data.attraction_id //The $(ev.target).parents('div.attraction')[0].$data.attraction_id expression get's the "attractionId" of the clicked element
    },
    //It first get's the parent div with the class "attraction" reads the first one in the list [0] and then reads the $data property of this element
    success: function(data) {
 alert("The attraction is successfully added");
},
    error: console.log
}); 
    }
});
}

【问题讨论】:

  • “php/validation.php”中发生了什么?我猜这个函数总是返回 1。这是第一个看的地方
  • `"...没有错误..."*.你在自相矛盾。
  • 确保使用if(data == 0) 而不是if(data = 0)。一个= 将值分配给变量,而两个比较它们。

标签: javascript ajax


【解决方案1】:

您应该使用if (data == 1) {}if (data == 0) {} 而不是if (data = 1) {},因为这会将数据变量设置为1。

http://php.net/manual/en/control-structures.if.php http://php.net/manual/en/language.operators.comparison.php

Can I define a variable in a PHP if condition?

【讨论】:

  • 查看我添加到上述答案的粘贴箱。我收到错误无效用户
  • 我认为你有空$_SESSION['userId']
【解决方案2】:

确保使用if(data == 0) 而不是if(data = 0)。一个= 将值赋给变量,而两个比较它们

【讨论】:

  • 好的,谢谢,但是当我添加这个时,它会返回一个错误,提示用户无效。我已在此粘贴箱中添加了我的 php 文件。 (它们太大了,无法添加到问题中。)pastebin.com/GHNJVJAX 在 itinerary.php 上它有即将出现的错误,但不确定它有什么问题。我顺便登录了
  • 这与上一个问题无关。这是说无效用户,因为会话中没有设置 userId 变量。所以你需要弄清楚为什么没有设置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 2015-09-28
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多