-1

Like a lot of people here, I'm having an issue. I'm trying to delete something in an sql base with an html button (which is not in a form (should I?)). To achieve this I'm using Ajax and PHP. My Ajax code reach success and everything should work. But PHP is looking for an GET request so POST stay null.

Here is my Ajax:

    function deleteImg(arg){
      console.log("I'm now in the function");
      var url = window.location.href;
      $.ajax({
           type: "POST",
           url:url,
           data:{action:'delete', id:'arg'},
           beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")},
           success:function(html) {
                console.log("Success :) ");
           }

      });
}

And here is my php:

<?php
    
    session_start();
    
    $dir    = '../uploads/';
    $files1 = scandir($dir);
    
    function remove($id){
        $file = $dir . $files1[$id];
        unlink($file);
    }
    
    
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
     echo "<script>alert( \" request method is get \" )</script>";
     }
    
    
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      echo "<script>alert( \" La request method is post \" )</script>";
      }
    
    if(isset($_POST['action'])){
    
      if( $_POST['action'] == 'delete'){
            header('Location: projets.php');
            remove($id);
      }
    
    }
?>

This is my first question and English is not my primary language, so I'm sorry if something is missing.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Elouan
  • 11
  • 6

2 Answers2

0

Your remove function attempts to use variables from the global context but are unknown within the function itself. To allow access to these variables within the function either add them as parameters to the function or declare as global within.

<?php
<?php
    error_reporting( E_ALL );
    session_start();
    $message=false;

    $dir    = '../uploads/';
    $dir=__DIR__ . '/upload';   #for MY environment
    $files = scandir( $dir );


    function remove( $id ){
        global $dir;
        global $files;
        if( array_key_exists( $id, $files ) ){
            $file = realpath( $dir . '/' . $files[ $id ] );
            return unlink( $file );
        }
        return false;
    }




    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        ob_clean();

        $args=array(
            'action'    =>  FILTER_SANITIZE_STRING,
            'id'        =>  FILTER_SANITIZE_STRING
        );
        $_POST=filter_input_array( INPUT_POST, $args );
        extract( $_POST );


        if( !empty( $action ) && !empty( $id ) ){
            $status=remove( $id );
            $message=$status ? 'File deleted' : 'Error: Unable to find file';
        }

        exit( $message );
    }
?>



<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>Delete...</title>
        <script src='//code.jquery.com/jquery-latest.js'></script>
        <script>
            function deleteImg( event, arg ){
                var url = window.location.href;
                $.ajax({
                    type:"POST",
                    url:url,
                    data:{
                        action:'delete',
                        id:arg
                    },
                    success:function( r ) {
                        console.log( r );
                        event.target.parentNode.removeChild( event.target );
                    }
                });
            }
        </script>
    </head>
    <body>
        <?php
            foreach( $files as $index => $file ){
                if( $file!='.' && $file!='..' && !is_dir( $file ) ) {
                    printf('<a href="#" onclick="deleteImg( event, \'%d\' )">Delete %s</a><br /><br />', $index, $file );
                }
            }
        ?>
    </body>
</html>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Yeah thank you :) , but still, the remove function isn't the issue here, I'm trying to have a $POST value wich is set... – Elouan Feb 26 '20 at 10:25
  • The code I posted works perfectly well and deletes the image ~ initially I used strings for the `deleteImg` arg then modified it to use integers as per your comment... works fine – Professor Abronsius Feb 26 '20 at 10:43
-1

So, I'm still trying to fix this. I discover if I replace $_POST by $_GET, the value is neither set.

if(isset($_GET['action']))

Will still be false. Even if the server is listening on $_GET....

Elouan
  • 11
  • 6