无法使用enctype =“ multipart / form-data”

时间:2018-11-08 10:48:23

标签: php laravel forms

我正在Laravel上使用Forms,但我仍在学习。我以为我已经把所有的事情都弄清楚了,但是我已经被一个问题困扰了:

当我有enctype =“ multipart / form-data”时,我无法获得输入值。我上载的文件仍会上载到磁盘,但其余值不会打印。如果删除enctype =“ multipart / form-data”,我会得到这些值。

表格:

<form id="forms" method="POST" action="alteracaocomissao" enctype="multipart/form-data">

  {{ csrf_field() }}

  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="nomeentidade">Nome:</label>
      <input type="text" class="form-control" id="nome1" name="nome1" placeholder="Nome entidade" required>

    </div>
    <div class="form-group col-md-6">
      <label for="numentidade">Nº:</label>
      <input type="text" class="form-control" id="num1" name="num1" placeholder="Número" required>
    </div>
  </div>

  <div class="form-row">
    <div class="form-group col-md-6 mb-3">
<label for="conta">Conta:</label>
      <input type="text" class="form-control" id="conta" name="conta" placeholder="Conta" required>
    </div>

    <div class="form-group col-md-3 mb-3">
        <label for="balcao">Local:</label>
            <select class="form-control" id="local1" name="local1" required>
    <option value="">Escolher...</option>
    <option value="1">Local</option>
    <option value="2">Local1</option>
            </select>
    </div>

    <div class="form-group col-md-3 mb-3">
      <label for="atleracao">Tipo de alteração:</label>
        <select class="form-control" id="alteracao" name="alteracao" required>
    <option value="">Escolher...</option>
    <option value="1">Alterar1</option>
    <option value="2">Alterar2</option>
        </select>
    </div>
  </div>

    <hr>

  <div class="form-row" id="buildyourform">
    <div class="form-group col-md-4">
      <label for="comissao">TEST:</label>
      <select class="form-control" id="TEST1" name="TEST1" required>
        <option value="">Escolher...</option>
        <option value="1">TEST</option>
        <option value="2">TEST1</option>
            </select>
    </div>

    <div class="form-group col-md-2">
      <label for="desconto">Desconto solicitado:</label>
      <div class="input-group">
      <input type="text" class="form-control" id="desconto" name="desconto" placeholder="Número" required>
      <span class=input-group-addon>%</span>
    </div>
    </div>

        <div class="form-group col-md-2">
    <label for="add"> &nbsp </label>
    <input type="button" value="Adicionar campos" class="form-control btn btn-light" id="add" />
    </div>

    <div class="form-group col-md-1" id="2field">
      <label for="remove"> &nbsp </label>
      <input type="button" value="Remover" class="form-control btn btn-light" id="remove" />
    </div>

    <div class="form-group col-md-2">
    </div>
  </div>

  <hr>

  <div class="form-row">

    <div class="form-group col-md-12">
      <label for="fundamentacao">Fundamentação:</label>
      <textarea type="text" class="form-control" id="fundamentacao" name="fundamentacao" placeholder="Fundamentação do pedido" required></textarea>
    </div>

  </div>

  <div class="form-row">
    <div class="form-group col-md-2">
        <label for="file2">Anexo:</label>
            <input type="file" name="file2" id="file2" required>

    </div>
  </div>

  <hr>

  <button type="submit" class="btn btn-primary">Enviar</button>
</form>

控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{

    public function AltComiss(Request $request)
    {
        session_start();

        $array1 = $request -> all();
        $path = $request->file('file2')->store('altComiss');

        $arrayRm1 = array_shift($array1);

        $_SESSION["testPostSection1"] = $array1;   

        return redirect('alteracaocomissao');

    }
}

路线:

Route::post('alteracaocomissao', 'PostController@AltComiss');

测试代码:

@php

session_start();

  if (isset($_SESSION["testPostSection1"])) {

    echo '<pre>'; print_r($_SESSION["testPostSection1"]); echo '</pre>';

  }

@endphp

我确实有SESSION来测试是否保存了值,因为我仍然没有设置数据库,直到我这样做之前,我都在使用SESSION进行测试。显然,一旦我建立了数据库,我就会从SESSION切换到将值插入数据库。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这会为您提供帮助

use Illuminate\Http\Request;

//use these For Start your session and Store file 
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;


class PostController extends Controller{
   public function AltComiss(Request $request){
     $array1 = $request->all();

     if ($request->hasFile('file2')) {
        $file = $request->file('file2');
        $destinationPath = 'altComiss';
        $file->move($destinationPath,$file->getClientOriginalName());
     }

     $arrayRm1 = array_shift($array1);

     Session::flash('allInput',$arrayRm1);
     return redirect('alteracaocomissao');
  }

}

在用于获取会话数据的查看文件上,只需使用

@if (Session::has('allInput'))
    <?php 
      echo '<pre>';
          print_r(Session::get('allInput'));
      echo '</pre>';
    ?>
@endif