英文:
Laravel Submit Button Form does nothing
问题
当我点击提交按钮后,什么都没有发生,只是地址发生了变化,我应该检查什么?请帮忙。
view.blade.php
@if(Auth::user()->id !== $data->id)
<form action="/send/{{$data->id}}" method="POST">
<div class="card-body bg-light">
<div class="form-group">
<textarea name="message" id="" cols="30" rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">发送消息</button>
</div>
</form>
@endif
web.php
Route::post('/send/{id}', 'UserController@send');
UserController.php
public function send(Request $request, $profile_id){
$this->validate($request,[
'message' =>'required',
]);
$current_user = Auth::user();
$newMsg = new inbox();
$newMsg->senderID = $current_user->id;
$newMsg->receiveID = $profile_id;
$newMsg->message = $request->message;
$newMsg->save();
return redirect("/profile-user/{$profile_id}")->with("response", "消息发送成功");
}
英文:
when I click my submit button and nothing happens, just the address changed, what should I check? please help?
view.blade.php
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
@if(Auth::user()->id !== $data->id)
<form action="/send/{{$data->id}}" method="POST">
<div class="card-body bg-light">
<div class="form-group">
<textarea name="message" id="" cols="30" rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</form>
@endif
<!-- end snippet -->
web.php
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
Route::post('/send/{id}', 'UserController@send');
<!-- end snippet -->
UserController.php
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
public function send(Request $request, $profile_id){
$this->validate($request,[
'message' =>'required',
]);
$current_user = Auth::user();
$newMsg = new inbox();
$newMsg->senderID = $current_user->id;
$newMsg->receiveID = $profile_id;
$newMsg->message = $request->message;
$newMsg->save();
return redirect("/profile-user/{$profile_id}")->with("response", "Message sent Successfully");
}
<!-- end snippet -->
答案1
得分: 1
更改您的表单网址并检查您的网址是否存在。
@if(Auth::user()->id !== $data->id)
<form action="{{ url('/send/' . $data->id) }}" method="POST">
<div class="card-body bg-light">
<div class="form-group">
<textarea name="message" id="" cols="30" rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">发送消息</button>
</div>
</form>
@endif
英文:
Change Your Form Url and also check you url is exist
@if(Auth::user()->id !== $data->id)
<form action="{{url('/send/'.$data->id}}" method="POST">
<div class="card-body bg-light">
<div class="form-group">
<textarea name="message" id="" cols="30" rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</form>
@endif
答案2
得分: 0
尝试这个
@if(Auth::user()->id != $data->id)
<form action="{{url('/send/'.$data->id)}}" method="POST">
<div class="card-body bg-light">
<div class="form-group">
<textarea name="message" id="" cols="30" rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">发送消息</button>
</div>
</form>
@endif
使用 !=
而不是 !==
也许 $data->id
包含字符串数据类型,所以不要使用 !==
只有当你确保两者类型相同时才使用 !==
参考链接 https://www.w3schools.com/php/php_operators.asp
并且你的 /send/{{$data->id}}
也是错误的,使用 {{url('/send/'.$data->id)}}
。
英文:
Try this
@if(Auth::user()->id != $data->id)
<form action="{{url('/send/'.$data->id}}" method="POST">
<div class="card-body bg-light">
<div class="form-group">
<textarea name="message" id="" cols="30" rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</form>
@endif
Use !=
not !==
may be $data->id
it contain string datatype so don't use !==
only use !==
when you are sure both are same type
ref link https://www.w3schools.com/php/php_operators.asp
and you /send/{{$data->id}}
also wrong use {{url('/send/'.$data->id}}
答案3
得分: 0
你需要验证表单中缺少的 CSRF 令牌。尝试确保它会工作。
@if(Auth::user()->id !== $data->id)
<form action="/send/{{$data->id}}" method="POST">@csrf
<div class="card-body bg-light">
<div class="form-group">
<textarea name="message" id="" cols="30" rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">发送消息</button>
</div>
</form>
@endif
英文:
You have to validate the csrf token which one is missing in
your form.. Try it sure it will work
@if(Auth::user()->id !== $data->id)
<form action="/send/{{$data->id}}"
method="POST">@csrf
<div class="card-body bg-light">
<div class="form-group">
<textarea name="message" id="" cols="30"
rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-
primary">Send Message</button>
</div>
</form>
@endif
答案4
得分: -1
你的文本区域必须引用它所属的表单:
<form id="my_form" ... >
<textarea form="my_form" ... ></textarea>
</form>
英文:
Your textarea must refer the form it belongs to
<form id="my_form" ... >
<textarea form="my_form" ... ></textarea>
</form>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论