3

I have an ajax function in jquery calling a php file to perform some operation on my database, but the result may vary. I want to output a different message whether it succeeded or not

i have this :

echo '<button id="remove_dir" onclick="removed('.$dir_id.')">remove directory</button>';

<script type="text/javascript">
    function removed(did){
        $.ajax({ 
            type: "POST", 
            url: "rmdir.php", 
            data: {dir_id: did},
            success: function(rmd){ 
                if(rmd==0)
                    alert("deleted");
                else
                    alert("not empty");
                window.location.reload(true);
            } 
        }); 
    }
</script>

and this

   <?php
require('bdd_connect.php');
require('functions/file_operation.php');
if(isset($_POST['dir_id'])){
    $rmd=remove_dir($_POST['dir_id'],$bdd);
}
?>

my question is, how to return $rmd so in the $.ajax, i can alert the correct message ?

thank you for your answers

5 Answers 5

5

PHP

<?php
   require('bdd_connect.php');
   require('functions/file_operation.php');
   if (isset($_POST['dir_id'])){
      $rmd=remove_dir($dir_id,$bdd); 
      echo $rmd;
   }
?>

JS

function removed(did){
    $.ajax({ 
        type: "POST", 
        url: "rmdir.php", 
        data: {dir_id: did}
    }).done(function(rmd) {
         if (rmd===0) {
            alert("deleted");
         }else{
            alert("not empty");
            window.location.reload(true);  
         }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

3

i advice to use json or :

if(isset($_POST['dir_id'])){
    $rmd=remove_dir($dir_id,$bdd);  
    echo $rmd;
}

Comments

1

You need your php file to send something back, then you need the ajax call on the original page to behave based on the response.

php:

if(isset($_POST['dir_id'])){
    $rmd=remove_dir($dir_id,$bdd);  
    echo "{'rmd':$rmd}";
}

which will output one of two things: {"rmd": 0} or {"rmd": 1}

We can simulate this return on jsBin

Then use jquery to get the value and do something based on the response in our callback:

$.ajax({ 
    type: "POST",
    dataType: 'json',
    url: "http://jsbin.com/iwokag/3",
    success: function(data){ 
        alert('rmd = ' + data.rmd)
    } 
});

View the code, then watch it run. Only I didn't send any data here, my example page always returns the same response.

Comments

0

Just try echoing $rmd in your ajax file, and then watching the console (try console.log(rmd) in your ajax response block)

$.ajax({ 
        type: "POST", 
        url: "rmdir.php", 
        data: {dir_id: did},
        success: function(rmd){ 

            console.log(rmd);
        } 
    }); 

You can then act accordingly based on the response

Comments

0

Try echo the $rmd out in the php code, as an return to the ajax.

if(isset($_POST['dir_id'])){
    $rmd=remove_dir($dir_id,$bdd);  
    //if $rmd = 1 alert('directory not empty');
    //if $rmd = 0 alert('directory deleted');
    echo $rmd;
}

Your "rmd" in success: function(rmd) should receive the callabck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.