【问题标题】:posted value is not displaying when use enctype as multipart/form-data in cakephp在 cakephp 中使用 enctype 作为 multipart/form-data 时未显示发布的值
【发布时间】:2016-04-22 11:33:06
【问题描述】:

我在 cakephp 工作。我创建了一个表单,其中包括文件上传和文本框和文本区域。

这里,我的 html 代码如下所示:

<form action="" id="frmReg" method="post" enctype= "multipart/form-data">
   <div style="position:relative; margin-bottom:30px;" class="fildtpart4">
      <label><?php __d('statictext', 'Question', false); ?>:</label>
      <span>
          <textarea class="validate[required]" name="question1['question']" cols="" rows=""></textarea>
          <div style="color: red; position:absolute ; bottom:-25px ;" id="charNum"></div>
     </span>  
     <div class="clear"></div>
     <div class="fildtpart3">
        <label><?php __d('statictext', 'Currect Answer', false); ?></label>
        <span>
           <input name="question1['currect_ans']" type="text" class="validate[required]" id="" value="">
           <input type="file" class="validate[required]" onchange="showMyImage1(this)" name="question1['sponsor_image']" id="" />
           <input type="file" class="validate[required]" onchange="showMyImage2(this)" name="question1['question_image2']" id="" />
        </span>
        <input type="submit" value="submit" name="submit">
        <div class="clear10"></div>  
     </div>
   </div>
<form>

当我提交此表单时,它只会显示图像而不是所有数据。

在我的控制器中,我写了:

 function add_polls()
 { 
    print_r($this->params['form']);exit;
 }

然后它给出如下输出:

array(
    [question1]=>array(
                    [name] => Array
                              (
                                 ['sponsor_image'] => contact.jpg
                                 ['question_image2'] => contact.jpg
                              )
                    [type] => Array
                              (
                                 ['sponsor_image'] => image/jpeg
                                 ['question_image2'] => image/jpeg
                              )
                    [tmp_name] => Array
                                   (
                                     ['sponsor_image'] => /tmp/phpUK7Vcj
                                     ['question_image2'] => /tmp/php3SCWGZ
                                   )
                    [error] => Array
                               (
                                 ['sponsor_image'] => 0
                                 ['question_image2'] => 0
                               )
                    [size] => Array
                              (
                                ['sponsor_image'] => 2305
                                ['question_image2'] => 2305
                              )
                      )
         )

这里,它没有打印 question1['currect_ans']question1['question']。当我从表单中删除 enctype 时,它​​将显示所有值。那么我该如何解决这个问题呢?

注意:CakePHP 版本为 1.3.13。

【问题讨论】:

  • 您能否尝试将 enctype 设置为 multipart/mixed。
  • @DiomedesAndreou 如果我使用 enctype 作为 multipart/mixed 可以上传图片吗?
  • 请务必提及您使用的确切 CakePHP 版本!
  • @ndm CakePHP 版本是 CakePHP(tm) v 0.2.9
  • 没有这样的 CakePHP 版本(不再),您很可能正在查看 @since 标记。正确的版本号可以在 CakePHP 核心代码文件夹的 VERSION.txt 文件中找到。也请在您的问题中添加此类信息,并相应地标记它,不要只是将其放在评论中。

标签: php forms cakephp


【解决方案1】:

这是混合了 PHP 处理文件的方式,而您没有遵循 CakePHP 约定,导致 CakePHP “覆盖”您的其他表单数据。

你应该使用表单助手,这样你就可以首先避免这个问题,除非你当然会摆弄字段名称并再次使用这种冲突的名称。

question1 中添加所有内容,包括文件,将导致文件在$_FILES 中被分组到question1 下,正如您的问题所示,即

Array(
    [question1] => Array(
        [name] => Array(
            ['sponsor_image'] => contact.jpg
            ['question_image2'] => contact.jpg
        )
        // ...
    )
)

$_POST中的其余表单数据使用相同的键,即

Array(
    [question1] => Array(
        ['question'] => 'foo',
        ['currect_ans'] => 'bar'
    )
)

你应该已经闻到了即将到来的冲突。

CakePHP 会先用$_POST 填充$params['form'],然后遍历$_FILES 并使用keys 设置$params['form'] 中的文件数据,这将导致之前设置的表单数据,即文本输入,将被覆盖,因为两者都使用密钥 question1

tl;dr 遵循命名约定,或使用表单助手

遵循 CakePHP 约定并使用 data 键(以及可选的模型名称,不需要存在)对输入进行分组,这样所有数据将被单独正确处理并通过 Controller::$data 提供

name="data[question]"
name="data[currect_ans]"
name="data[sponsor_image]"
name="data[question_image2]"

顺便说一句,嵌套键不需要使用引号。

如前所述,更好的是使用表单助手,它会自动创建正确的字段名。

另见

【讨论】:

    【解决方案2】:

    所以我复制了您的问题并报告以下内容。

    在我的 cakephp 3.x 上,通过使用“常规”HTTP 帖子提交,您的表单效果很好。 (我在您的表单中添加了&lt;input type="submit"&gt;)。

    [
        'question1' => [
            'question' => 'da',
            'currect_ans' => 'da',
            'sponsor_image' => [
                'name' => 'Mobile version.png',
                'type' => 'image/png',
                'tmp_name' => '/tmp/php3A021O',
                'error' => (int) 0,
                'size' => (int) 149447
            ],
            'question_image2' => [
                'name' => 'Mobile version.png',
                'type' => 'image/png',
                'tmp_name' => '/tmp/phpi6VWxl',
                'error' => (int) 0,
                'size' => (int) 149447
            ]
        ]
    ]
    

    那么您是如何提交数据的呢?是通过ajax吗?你使用的是什么版本的 cakephp?尝试回答这些问题,以便我进一步帮助您。

    【讨论】:

    • 不,我使用了简单的 post 方法来发送我的数据。而且我使用的是 CakePHP 1.3.13 版本的 cakephp。
    【解决方案3】:

    您是否尝试过使用

    $postedData = $this->request->data;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 2018-04-07
      • 2010-11-05
      相关资源
      最近更新 更多