Php 当我点击Laravel8中的Submit按钮时,在两个URL处提交表单
你好,我是laravel的新手,问题是我创建了两个函数,一个用于查看编辑表单,另一个用于更新数据,但当我单击submit it concat view并更新两个url时,在表单操作中,我只给了它更新url 这是我的web.php代码Php 当我点击Laravel8中的Submit按钮时,在两个URL处提交表单,php,laravel,Php,Laravel,你好,我是laravel的新手,问题是我创建了两个函数,一个用于查看编辑表单,另一个用于更新数据,但当我单击submit it concat view并更新两个url时,在表单操作中,我只给了它更新url 这是我的web.php代码 Route::post('addStudent',[StudentController::class,'addStudent'])->name('addStudent'); Route::get('/president',[PresidentControlle
Route::post('addStudent',[StudentController::class,'addStudent'])->name('addStudent');
Route::get('/president',[PresidentController::class,'index']);
Route::get('editPresident/{id}',[PresidentController::class,'presidentEditForm']);
Route::post('updatePresident',[PresidentController::class,'updatePresident'])->name('updatePresident');
这是我的控制器代码
// view president edit from
function presidentEditForm($id)
{
$userData=$this->user->getPresidentById($id);
return view('president/edit_president',['president'=>$userData]);
}
// update president from the admin
function updatePresident(Request $data)
{
$validated = $data->validate([
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|max:255',
'mob_no'=>'required',
'city'=>'required',
'join_date'=>'required'
]);
// saving inputs into variables
$firstname=$data->first_name;
$lastname=$data->last_name;
$email=$data->email;
$mob_no=$data->mob_no;
$city=$data->city;
$password=$data->password;
$joiningdate=$data->join_date;
$joiningtill=$data->join_till;
$userid=$data->userId;
// saving into the users object
$this->user->firstname=$firstname;
$this->user->lastname=$lastname;
$this->user->email=$email;
if($password!="")
{
$this->user->password=$password;
}
$this->user->joining_from=$joiningdate;
$this->user->joining_till=$joiningtill;
$this->user->city=$city;
$this->user->mobile_no=$mob_no;
}
这是我的视图代码
@extends('includes.header')
@section('content')
<x-pagetitle data="Edit President"/>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
<button type="button" onclick="back()" class="btn btn-dark btn-xs btn_back my-2"><i class="fas fa-arrow-left"></i> Go Back</button>
</div>
</div>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
<div class="card">
<div class="card-body">
<form action="updatePresident" method="POST">
@csrf
@foreach ($president as $p)
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 ">
<label for="first_name">First name*</label>
<input type="text" class="form-control" id="first_name" placeholder="First name" name="first_name" value="{{$p['firstname']}}">
<input type="hidden" name="userId" value="{{$p['usr_id']}}">
<div class="invalid-feedback">
@error('first_name')
{{ $message }}
@enderror
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 ">
<label for="last_name">Last name*</label>
<input type="text" class="form-control" id="last_name" placeholder="Last name" name="last_name" value="{{$p['lastname']}}">
<div class="invalid-feedback">
@error('last_name')
{{ $message }}
@enderror
</div>
</div>
</div>
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="email">Email*</label>
<input type="text" class="form-control" id="email" placeholder="Email" name="email" value="{{$p['email']}}">
<div class="invalid-feedback">
@error('email')
{{ $message }}
@enderror
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="password">Password*</label>
<input type="text" class="form-control" id="password" placeholder="password" name="password">
<div class="invalid-feedback">
@error('password')
{{ $message }}
@enderror
</div>
</div>
</div>
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="mob_no">Phone No/Mobile No*</label>
<input type="text" class="form-control" id="mob_no" placeholder="Mobile/Phone No" name="mob_no" value="{{$p['mobile_no']}}">
<div class="invalid-feedback">
@error('mob_no')
{{ $message }}
@enderror
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="city">City*</label>
<input type="text" class="form-control" id="city" placeholder="City" name="city" value="{{$p['city']}}">
<div class="invalid-feedback">
@error('city')
{{ $message }}
@enderror
</div>
</div>
</div>
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="join_date">Joining Date*</label>
<div class="input-group date" id="join_date" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#join_date" name="join_date" value="{{$p['joining_from']}}" placeholder="Job Start Date">
<div class="input-group-append" data-target="#join_date" data-toggle="datetimepicker">
<div class="input-group-text"><i class="far fa-calendar-alt"></i></div>
</div>
</div>
<div class="invalid-feedback">
@error('join_date')
{{ $message }}
@enderror
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="join_till">Joining Till</label>
<div class="input-group date" id="join_till" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#join_till" name="join_till" value="{{$p['joining_till']}}" placeholder="Job Start Date">
<div class="input-group-append" data-target="#join_till" data-toggle="datetimepicker">
<div class="input-group-text"><i class="far fa-calendar-alt"></i></div>
</div>
</div>
<div class="invalid-feedback">
@error('join_till')
{{ $message }}
@enderror
</div>
</div>
</div>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12 ">
<button class="btn btn-primary" type="submit">Update</button>
</div>
</div>
@endforeach
</form>
</div>
</div>
</div>
</div>
@endsection
@extends('includes.header'))
@节(“内容”)
回去
@csrf
@foreach(总裁为$p)
名字*
@错误('first_name')
{{$message}}
@恩德罗
姓*
@错误('姓氏')
{{$message}}
@恩德罗
电子邮件*
@错误('电子邮件')
{{$message}}
@恩德罗
密码*
@错误('密码')
{{$message}}
@恩德罗
电话号码/手机号码*
@错误('mob_no')
{{$message}}
@恩德罗
城市*
@错误('城市')
{{$message}}
@恩德罗
加入日期*
@错误('加入日期')
{{$message}}
@恩德罗
连接至
@错误('join_till')
{{$message}}
@恩德罗
更新
@endforeach
@端部
这就是错误所在
您需要在表单中添加@method('PUT'),然后才能更新数据
你应该检查一下路线
php artisan路由:列表
检查一下路由方法,你们可以使用其他类似的方法
@方法('your method name')你需要在你的表单中添加@method('patch')来执行更新操作,你的操作变成action=“{route('updatePresident')}”action=“{{route('updatePresident')}}”这给了我一个错误“route[updatePresident]not defined”,当我将它添加到表单中时,它正在添加我的视图表单url,然后是操作url为什么我不知道