如果已添加,则会一直返回相同的警报消息。

时间:2018-04-12 20:09:03

标签: javascript ajax

我正在尝试向用户提供反馈。如果他们已经添加了一些内容,他们会收到一条消息,说明此景点已添加到您的itinerar 。如果用户在他们的行程中没有吸引力,他们将获得成功消息。虽然它总是说已经添加了吸引力,即使它没有。没有错误,有谁知道为什么这种情况不断发生。以下是该功能的代码。

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

2 个答案:

答案 0 :(得分:0)

您应该使用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?

答案 1 :(得分:0)

请务必使用if(data == 0)代替if(data = 0)。一个=将值赋给变量,而两个比较它们