当使用表单而不是http客户端时,对PHP的POST请求不会检索任何数据

当使用表单而不是http客户端时,对PHP的POST请求不会检索任何数据,php,angularjs,Php,Angularjs,我使用AngularHTTP客户端与数据库交互,一切正常,但当我试图使用表单将数据发布到同一链接时,我发现数据未定义 我试图对值进行编码和解码,因为我知道在发出任何POST请求和发送数据之前,我需要执行angular.toJSON方法,但这不起作用 这是我的index.php,在这里我收到来自表单的POST请求 if (empty($action)) { if ((($_SERVER['REQUEST_METHOD'] == 'POST')) &&

我使用AngularHTTP客户端与数据库交互,一切正常,但当我试图使用表单将数据发布到同一链接时,我发现数据未定义

我试图对值进行编码和解码,因为我知道在发出任何POST请求和发送数据之前,我需要执行angular.toJSON方法,但这不起作用

这是我的index.php,在这里我收到来自表单的POST请求

if (empty($action)) {
    if ((($_SERVER['REQUEST_METHOD'] == 'POST')) && 
             (strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false)) {

        $input = json_decode(file_get_contents('php://input'), true);

        $action = isset($input['action']) ? $input['action'] : null;
        $subject = isset($input['subject']) ? $input['subject'] : null;
        $data = isset($input['data']) ? $input['data'] : null;
    }

case 'createNote':
        // if I die() here, it prints the die()
        if(!empty($data)) {
            // if I die() here, $data is undefined.
            $data = json_decode($data);

            $user = $data[0];
            $comment = $data[1];
            $film_id = $data[2];
            $lastupdated = date("Y-m-d H:i:s");


            $sql = "INSERT INTO nfc_note (user, film_id, comment, lastupdated) 
                    VALUES (:user, :film_id, :comment, :lastupdated)";
        }
        break;
我用来发送邮寄请求的表格

<form action="index.php" method="POST">
    <input type="hidden" name="action" value="create">
    <input type="hidden" name="subject" value="note">
    <input type="hidden" name="data" value="<?php echo "['username','content', 1]"; ?>">

    <input type="submit" value="Submit">
</form>

使用表单时不起作用。有什么我不知道的建议或错误吗?

您的PHP代码需要Json格式的数据,但它没有得到。这是因为HTML表单以
application/x-www-Form-urlencoded
的形式发送POST数据

为了支持这两种数据格式,您需要在PHP代码上构建一个逻辑来检测这两种格式。HTTP头中提到了数据格式,您可以在其中进行检查。查找
内容类型
。对于HTML表单的POST数据,它是
application/x-www-form-urlencoded
,对于Json,它应该是
application/Json

您可以使用
$\u POST[]
读取PHP中的表单值。在您的情况下,
$\u POST['data']
。为了简化HTML表单,您还可以将
数据
数组拆分为表单中自己的输入

if (empty($action)) {
    if ((($_SERVER['REQUEST_METHOD'] == 'POST')) && 
             (strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false)) {

        $input = json_decode(file_get_contents('php://input'), true);

        $action = isset($input['action']) ? $input['action'] : null;
        $subject = isset($input['subject']) ? $input['subject'] : null;
        $data = isset($input['data']) ? $input['data'] : null;
    }

case 'createNote':
        // if I die() here, it prints the die()
        if(!empty($data)) {
            // if I die() here, $data is undefined.
            $data = json_decode($data);

            $user = $data[0];
            $comment = $data[1];
            $film_id = $data[2];
            $lastupdated = date("Y-m-d H:i:s");


            $sql = "INSERT INTO nfc_note (user, film_id, comment, lastupdated) 
                    VALUES (:user, :film_id, :comment, :lastupdated)";
        }
        break;

有关更多信息,请参见此:

我遇到了类似的问题 “使用表单而不是http客户端时,对PHP的POST请求不会检索任何数据”。 通常使用流php来检测从角度到php的数据

$data = file_get_contents('php://input');
但是当用fortify扫描代码时,它会检测到xss漏洞,我必须更改方法

用这种方法解决了这个问题,希望能帮助到别人。:-)

角度:

$http({
method: 'POST',
url: '<?php echo base_url(); ?>admin/saveTemplateEmail',
headers: {'Content-Type': 'application/json'},
data: {templateEmail:$scope.selectedTipology}}).then(function (response) {.....}
$http({
method: 'POST',
url: '<?php echo base_url(); ?>admin/saveTemplateEmail',
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
data: jQuery.param({templateEmail:$scope.selectedTipology})}).then(function (response) {.....}
我把它改成了

角度:

$http({
method: 'POST',
url: '<?php echo base_url(); ?>admin/saveTemplateEmail',
headers: {'Content-Type': 'application/json'},
data: {templateEmail:$scope.selectedTipology}}).then(function (response) {.....}
$http({
method: 'POST',
url: '<?php echo base_url(); ?>admin/saveTemplateEmail',
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
data: jQuery.param({templateEmail:$scope.selectedTipology})}).then(function (response) {.....}
避免这种情况。此外,
.success
方法也已成功。