【发布时间】:2020-02-01 07:11:39
【问题描述】:
我遇到了一个问题,在我正在处理表单的网站中,按下按钮时不会运行代码,这几乎可以正常工作,我不知道是什么改变破坏了它。
<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']);
}
我刚刚用我对代码所做的更改更新了这篇文章
【问题讨论】:
-
你能解释一下发生了什么“不工作”吗?页面没有重新加载,您是否收到错误...?你在表单提交上使用 JavaScript 吗?它有没有干扰(你的 JS 控制台中的任何东西)?
-
我点击提交按钮,没有任何反应,没有重新加载,没有错误,什么都没有
-
在表单中只使用一个 type=button,如果您需要创建另一个按钮,请使用 。对于提交按钮,将其放置在您的表单中,如果您想从表单外部放置它,您可以从 javascript 中使用它