Php 如何强制服务器使用POST而不是GET

Php 如何强制服务器使用POST而不是GET,php,ajax,post,get,Php,Ajax,Post,Get,和这里的很多人一样,我也有个问题。 我试图用一个html按钮删除sql库中的某些内容(它不是表单中的(我应该吗?)。 为了实现这一点,我使用Ajax和PHP。 我的Ajax代码取得了成功,一切都应该正常。 但是PHP正在寻找GET请求,所以POST保持为null 这里是我的Ajax: function deleteImg(arg){ console.log("I'm now in the function"); var url = window.location.

和这里的很多人一样,我也有个问题。 我试图用一个html按钮删除sql库中的某些内容(它不是表单中的(我应该吗?)。 为了实现这一点,我使用Ajax和PHP。 我的Ajax代码取得了成功,一切都应该正常。 但是PHP正在寻找GET请求,所以POST保持为null

这里是我的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 :) ");
           }

      });
}
这是我的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);
      }

    }
?>


这是我的第一个问题,英语不是我的主要语言,所以如果有什么遗漏,我很抱歉。

所以,我仍在努力解决这个问题。 我发现,如果我将$\u POST替换为$\u GET,则两个值都没有设置

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

仍然是假的。即使服务器正在侦听$_GET…

您的
remove
函数也会尝试使用全局上下文中的变量,但函数本身是未知的。要允许访问函数中的这些变量,请将它们作为参数添加到函数中,或在函数中声明为
global

<?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>

删除。。。
函数deleteImg(事件,参数){
var url=window.location.href;
$.ajax({
类型:“POST”,
url:url,
数据:{
操作:'delete',
id:arg
},
成功:功能(r){
控制台日志(r);
event.target.parentNode.removeChild(event.target);
}
});
}

data:{action:'delete',id:'arg'}应该是
data:{action:'delete',id:arg}
如果您使用的是ajax
data:{action:'delete',id:arg}
没有引号的话,那么就不需要表单了。
arg(用户表单数据,请查看此答案:同时删除
beforeSend
回调。jQuery将自动设置正确的内容类型标题,此处无需以任何方式干扰。是的,谢谢:),但仍然,删除函数不是这里的问题,我正在尝试设置$POST值…我发布的代码运行良好并删除图像~最初我使用字符串作为
deleteImg
arg,然后根据您的注释将其修改为使用整数…工作正常