0

I'm new to programming and i'm not good at all with Ajax.
I want to get a value back from a php script in Ajax.
I send a javascript variable to a php script like that :

 $('#deleteSelectedButton').on('click', function () {
    if (confirm('Do you want to suppress the messages ?')) {
        $.ajax({
            type: 'POST',
            url: 'suppression-message',
            data: {
                'checkboxIdArray': checkboxIdArray.toString(),
            }
        });
        return false;
    }
});  

This is sent to the following php script which is deleting messages according to the id contained in the checkboxIdArray:

if (isset($_POST['checkboxIdArray'])) {

    $checkboxIdArray = $_POST['checkboxIdArray'];
    $str = json_encode($checkboxIdArray);
    $tab = explode(",", $str);
    $deleteSuccess = true;

    foreach($tab as $id)
    {
        $id = filter_var($id, FILTER_SANITIZE_NUMBER_INT);
        if (!$messageModelDb->delete($id)) {
            $deleteSuccess = false;
            die();
        }
    }
    if ($deleteSuccess === true) {
        $message = 'Success';;
    } else {
        $message= "Error";
    }
}

I want to get the $message variable back to my javascript in order to display a message according to the result of the script.

I would really appreciate some help ...
Thank you.

Simondebn
  • 23
  • 7

5 Answers5

0

You have to use success function and actually include the message in the response

 $.ajax({
            type: 'POST',
            url: 'suppression-message',
            data: {
                'checkboxIdArray': checkboxIdArray.toString(),
            },
            success : function(response){
              // your code or logic
              alert(response);
            }
        });

PHP

if ($deleteSuccess === true) {
    $message = 'Success';
} else {
    $message= "Error";
}
echo $message;
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
0
 $('#deleteSelectedButton').on('click', function () {
    if (confirm('Do you want to suppress the messages ?')) {
        $.ajax({
            type: 'POST',
            url: 'suppression-message',
            data: {
                'checkboxIdArray': checkboxIdArray.toString(),
            }, 
            success: function(response){
              alert(response);
            }
        });
        return false;
    }
});  
Nicholas Nur
  • 228
  • 2
  • 12
0

There is nothing special about an HTTP request made with JavaScript.

You output data in the response to it in from PHP in the same way as any other HTTP response.

echo $message;

In JavaScript, you process it as described in the documentation for jQuery.ajax.

Write a function that accepts a the response content as the first argument.

Then call done on the jqXHR object that .ajax returns and pass it that function.

    function handleResponse(data) {
        alert(data);
    }

    var jqXHR = $.ajax({
        type: 'POST',
        url: 'suppression-message',
        data: {
            'checkboxIdArray': checkboxIdArray.toString(),
        }
    });

    jqXHR.done(handleResponse);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Try out the code to get the value in ajax

<script>
 $('#deleteSelectedButton').on('click', function () {
    if (confirm('Do you want to suppress the messages ?')) {
        $.ajax({
            type: 'POST',
            url: 'suppression-message',
            data: {
                'checkboxIdArray': checkboxIdArray.toString(),
            }
        }).done(function(result)
        {
            alert(result);
        });
        return false;
    }
});  

</script>

Here is the php code

<?php
if (isset($_POST['checkboxIdArray'])) {

    $checkboxIdArray = $_POST['checkboxIdArray'];
    $str = json_encode($checkboxIdArray);
    $tab = explode(",", $str);
    $deleteSuccess = true;

    foreach($tab as $id)
    {
        $id = filter_var($id, FILTER_SANITIZE_NUMBER_INT);
        if (!$messageModelDb->delete($id)) {
            $deleteSuccess = false;
            die();
        }
    }
    if ($deleteSuccess === true) {
        $message = 'Success';;
    } else {
        $message= "Error";
    }
    echo $message;
}
?>
Subhash Shipu
  • 343
  • 1
  • 4
  • 15
0

Since jQuery implemented deferreds, .done is the preferred way to implement a success callback. You should also implement a .fail method with a failure response code.

darthaditya
  • 124
  • 3