繁体   English   中英

无法将 HTML 请求数据发布到 controller

[英]Can not post HTML request data to controller

我遇到了一个问题,在我正在处理表单的网站中,按下按钮时不会运行代码,这几乎可以正常工作,我不知道是什么改变了它。

<form action="{{action('Admin\AdminResponsibleController@assign')}}"  method="post" id="assignParty">
    @csrf
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    email: <input type="text" name="email"><br>
    @if ($message = Session::get('success'))
        <div class="alert alert-success alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button> 

        </div>
    @endif
    @if ($message = Session::get('message'))
        <div class="alert alert-warning alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button> 
                <strong>{{ $message }}</strong>
        </div>
    @endif
    <input type="radio" name="party_type" value="responsible" checked>Responsible Party<br>  
    <input type="radio" name="party_type" value="responsibleTwo"> Second Responsible Party<br> 
    <input type="radio" name="party_type" value="witness"> Witness <br>
    <input type="checkbox" name="remove" value="remove"> Remove Selected Assignment <br>
    <input type="hidden" id="userId" name="userId" value="<?php echo $user->id; ?>">
</form>
<button type="submit" form="assignParty" value="Submit">Submit</button>

路线

    Route::post('admin/viewPatient/assign', 'Admin\AdminResponsibleController@assign');

我试图运行的代码,dd 用于测试,它甚至从未到达那里

 public function assign(Request $request)
{     
    dd('hit');
    if($request->input('userId') != null){
        $patient = intval($request->input('userId'));
        $patient = User::where('id', $patient)->first();
    }  /**/

    $party = User::where('email', $request->input('email'))
                // We want to be sure that Admins and Patients can't be responsible parts, if they need to be we can create another account for them
                // Having patients be able to be repsonsible parties would confuse the patient and could lead to buggy code
                ->first();

    if($party != null){
        if($party->user_type == 'Admin' || $party->user_type == 'Patient'){
            return redirect()->back()->with(['message', 'Can not assign this user']);
        }
    }
    // setup remove user variable as false
    $removeUser = false;
    // if the email is null, remove user is true
    if($request->input('remove') != null){
        $removeUser = true;
    } else if($request->input('email') == null){
        return redirect()->back()->with(['message', 'Please include an email']);
    }
    // switch case to switch to different statements based on the input of party_type
    switch ($request->input('party_type')) {
        case 'responsible':
            $this->check($request, $party, 'responsible_party', 'Responsible Party', $removeUser, $patient);
            break;
        case 'responsibleTwo':
            $this->check($request, $party, 'responsible_party_two', 'Second Responsible Party', $removeUser, $patient);
            break;
        case 'financial':
            $this->check($request, $party, 'financial', 'Financially Responsible Party', $removeUser, $patient);
            break;
        case 'legal':
            $this->check($request, $party, 'legal', 'Legal Rep', $removeUser, $patient);
            break;
        case 'witness':
            $this->check($request, $party, 'witness', 'Witness', $removeUser, $patient);
            break;
        default:
            // in future versions please include a link to dispatch a support request
            // this really shouldn't happen, but a default case should be included in case something somehow goes wrong
            throw new \Exception('You must provide a party type.');
            break;

    }
    // return to the view with a message that the party has been assigned
    return redirect()->back()->with(['success', 'Party Updated sucessfully']);
}

我刚刚用我对代码所做的更改更新了这篇文章

您是否尝试将操作更改为路由?:

 Route::post('admin/viewPatient/assign', 'Admin\AdminResponsibleController@assign')->name('assign.post');

<form action="{{route('assign.post')}}"  method="post" enctype="multipart/form-data" id="assignParty">

https://laravel.com/docs/6.x/helpers#method-route

  • 你也应该像这样关闭输入: />
  • 您可以尝试在终端中运行php artisan cache:clear
  • 您可以尝试在终端中运行php artisan route:cache

等等,你的按钮在你的表单之外!!

<form>
   inputs

   <button type="submit" form="assignParty" value="Submit">Submit</button>
</form>

您需要使用输入标签而不是按钮标签。

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

已解决,页面其他地方有一个未关闭的表单标签

<form action="{{action('Patient\UploadController@adminUpload')}}" method="post" enctype="multipart/form-data">
                        @csrf
                        <input type="file" name="file" id="file">
                        <input id="user" type="hidden" class="form-control" name="user" value="{{$user->id}}" required autofocus>
                        <input type="submit" value="Upload File" name="submit">
                    <span class="text-danger">{{$errors->first('file')}}</span>`enter code here`

变成

<form action="{{action('Patient\UploadController@adminUpload')}}" method="post" enctype="multipart/form-data">
                        @csrf
                        <input type="file" name="file" id="file">
                        <input id="user" type="hidden" class="form-control" name="user" value="{{$user->id}}" required autofocus>
                        <input type="submit" value="Upload File" name="submit">
                    </form>
                    <span class="text-danger">{{$errors->first('file')}}</span>

结束表单 提交表单结束

<form action="{{action('Admin\AdminResponsibleController@assign')}}"  method="post" id="assignParty">
    @csrf
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    email: <input type="text" name="email"><br>
    @if ($message = Session::get('success'))
        <div class="alert alert-success alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button> 

        </div>
    @endif
    @if ($message = Session::get('message'))
        <div class="alert alert-warning alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button> 
                <strong>{{ $message }}</strong>
        </div>
    @endif
    <input type="radio" name="party_type" value="responsible" checked>Responsible Party<br>  
    <input type="radio" name="party_type" value="responsibleTwo"> Second Responsible Party<br> 
    <input type="radio" name="party_type" value="witness"> Witness <br>
    <input type="checkbox" name="remove" value="remove"> Remove Selected Assignment <br>
    <input type="hidden" id="userId" name="userId" value="<?php echo $user->id; ?>">

<button type="submit" form="assignParty" value="Submit">Submit</button>
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM