raqulka raqulka - 4 months ago 24
PHP Question

Two or more ajax calls to one PHP file

I know this has been asked and I looked the other topics, but couldn not find an answer.

I have two ajax calls, that point to the same page. One deletes language selection and other one deletes education experience. Weird thing is that the last query on PHP page works (at the moment language).

EDIT:
Why is the first query not working? (when trying to delete education exp)




PHP

/*------------------------------------------
[NANY EDUCATION EXP DELETE]
-------------------------------------------*/
if(isset($_POST['education_id']) && !empty($_POST['education_id'])){
$education_id = $_POST['education_id'];
$stmt = $user_home->runQuery("DELETE FROM user_education WHERE education_id=:education_id");
$stmt->execute(array(':education_id' => $education_id));
$response_array['status'] = 'success';
}else{
$response_array['status'] = 'error';

}

/*------------------------------------------
[NANY LANGUAGE EXP DELETE]
-------------------------------------------*/
if(isset($_POST['language_id']) && !empty($_POST['language_id'])){
$language_id = $_POST['language_id'];
$stmt = $user_home->runQuery("DELETE FROM user_language WHERE language_id=:language_id");
$stmt->execute(array(':language_id' => $language_id));
$response_array['status'] = 'success';
}else{
$response_array['status'] = 'error';

}


JS




/*------------------------------------------
[NANY EDUCATION EXP DELETE]
-------------------------------------------*/
$(document).on("click", ".deleteEducationEXP", function (e) {
e.preventDefault();
var education_id = $(this).attr('id');
$(this).parent().parent().parent().fadeOut(300, function() { $(this).remove(); });

$.ajax({
type: "POST",
url:'PHP/deleteData.php',
data:"education_id=" + education_id,
success:function(data){
if(data.status == 'success'){
console.log("success");
}else if(data.status == 'error'){
console.log("error");
}
},
error: function(jqXHR, textStatus, errorThrown, data){
console.log(jqXHR, textStatus, errorThrown, data);
}
});
});

/*------------------------------------------
[NANY LANGUAGE EXP DELETE]
-------------------------------------------*/
$(document).on("click", ".deleteLangugage", function (e) {
e.preventDefault();
var language_id = $(this).attr('id');
$(this).parent().parent().fadeOut(300, function() { $(this).remove(); });

$.ajax({
type: "POST",
url:'PHP/deleteData.php',
data:"language_id=" + language_id,
success:function(data){
if(data.status == 'success'){
console.log("success");
}else if(data.status == 'error'){
console.log("error");
}
},
error: function(jqXHR, textStatus, errorThrown, data){
console.log(jqXHR, textStatus, errorThrown, data);
}
});
});

Answer

The problem is that both else clauses set $response_array['status'] = "error";.

When they delete education, the first if block succeeds, deletes from the education table and sets $response_array['status'] = "success";. Then it goes to the next if block, which fails and overwrites $reponse_array['status'].

Use elseif to combine them.

if(!empty($_POST['education_id'])){
    /*------------------------------------------
    [NANY EDUCATION EXP DELETE]
    -------------------------------------------*/ 
    $education_id = $_POST['education_id'];
    $stmt = $user_home->runQuery("DELETE FROM user_education WHERE education_id=:education_id");
    $stmt->execute(array(':education_id' => $education_id));
    $response_array['status'] = 'success';
}elseif(!empty($_POST['language_id'])){
    /*------------------------------------------
    [NANY LANGUAGE EXP DELETE]
    -------------------------------------------*/ 
    $language_id = $_POST['language_id'];
    $stmt = $user_home->runQuery("DELETE FROM user_language WHERE language_id=:language_id");
    $stmt->execute(array(':language_id' => $language_id));
    $response_array['status'] = 'success';
}else{
    $response_array['status'] = 'error';
}

BTW, you don't need to use both isset() and !empty(), since empty() checks whether it's set first.

Comments