Laravel提交按钮表单不起作用

huangapple go评论60阅读模式
英文:

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()-&gt;id !== $data-&gt;id)
        &lt;form action=&quot;/send/{{$data-&gt;id}}&quot; method=&quot;POST&quot;&gt;
            &lt;div class=&quot;card-body bg-light&quot;&gt;

                &lt;div class=&quot;form-group&quot;&gt;
                    &lt;textarea name=&quot;message&quot; id=&quot;&quot; cols=&quot;30&quot; rows=&quot;10&quot; class=&quot;form-control&quot;&gt;&lt;/textarea&gt;
                &lt;/div&gt;
                &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Send Message&lt;/button&gt;

            &lt;/div&gt;
        &lt;/form&gt;
        @endif

<!-- end snippet -->

web.php

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Route::post(&#39;/send/{id}&#39;, &#39;UserController@send&#39;);

<!-- end snippet -->

UserController.php

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    public function send(Request $request, $profile_id){
        $this-&gt;validate($request,[
            &#39;message&#39; =&gt;&#39;required&#39;, 
        ]);

        $current_user = Auth::user();
        $newMsg = new inbox();
        $newMsg-&gt;senderID = $current_user-&gt;id;
        $newMsg-&gt;receiveID = $profile_id;
        $newMsg-&gt;message = $request-&gt;message;

        $newMsg-&gt;save();

        return redirect(&quot;/profile-user/{$profile_id}&quot;)-&gt;with(&quot;response&quot;, &quot;Message sent Successfully&quot;);
        
    }

<!-- 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()-&gt;id !== $data-&gt;id)
        &lt;form action=&quot;{{url(&#39;/send/&#39;.$data-&gt;id}}&quot; method=&quot;POST&quot;&gt;
            &lt;div class=&quot;card-body bg-light&quot;&gt;

                &lt;div class=&quot;form-group&quot;&gt;
                    &lt;textarea name=&quot;message&quot; id=&quot;&quot; cols=&quot;30&quot; rows=&quot;10&quot; class=&quot;form-control&quot;&gt;&lt;/textarea&gt;
                &lt;/div&gt;
                &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Send Message&lt;/button&gt;

            &lt;/div&gt;
        &lt;/form&gt;
        @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()-&gt;id != $data-&gt;id)
        &lt;form action=&quot;{{url(&#39;/send/&#39;.$data-&gt;id}}&quot; method=&quot;POST&quot;&gt;
            &lt;div class=&quot;card-body bg-light&quot;&gt;

                &lt;div class=&quot;form-group&quot;&gt;
                    &lt;textarea name=&quot;message&quot; id=&quot;&quot; cols=&quot;30&quot; rows=&quot;10&quot; class=&quot;form-control&quot;&gt;&lt;/textarea&gt;
                &lt;/div&gt;
                &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Send Message&lt;/button&gt;

            &lt;/div&gt;
        &lt;/form&gt;
        @endif

Use != not !==

may be $data-&gt;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-&gt;id}} also wrong use {{url(&#39;/send/&#39;.$data-&gt;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()-&gt;id !== $data-&gt;id)
       &lt;form action=&quot;/send/{{$data-&gt;id}}&quot; 
   method=&quot;POST&quot;&gt;@csrf
           &lt;div class=&quot;card-body bg-light&quot;&gt;

                &lt;div class=&quot;form-group&quot;&gt;
                    &lt;textarea name=&quot;message&quot; id=&quot;&quot; cols=&quot;30&quot; 
  rows=&quot;10&quot; class=&quot;form-control&quot;&gt;&lt;/textarea&gt;
               &lt;/div&gt;
               &lt;button type=&quot;submit&quot; class=&quot;btn btn- 

primary">Send Message</button>

           &lt;/div&gt;
       &lt;/form&gt;
       @endif

答案4

得分: -1

你的文本区域必须引用它所属的表单:

<form id="my_form" ... >
    <textarea form="my_form" ... ></textarea>
</form>
英文:

Your textarea must refer the form it belongs to

&lt;form id=&quot;my_form&quot; ... &gt;
    &lt;textarea form=&quot;my_form&quot; ... &gt;&lt;/textarea&gt;
&lt;/form&gt;

huangapple
  • 本文由 发表于 2020年1月3日 20:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定