<form method =“ post”>在此不起作用

时间:2019-04-26 06:49:57

标签: php laravel laravel-blade

此表类中的Post表单在提交时不响应

<table class="table table-hover">
    <tbody>
        <tr>
            <th>NO.</th>
            <th>NAME.</th>
            <th>Telephone</th>
            <th>email</th>
            <th>date</th>
            <th></th>
            <th>action</th>
        </tr>
    @foreach($users as $value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->firstname}} {{$value->lastname}}</td>
            <td>{{$value->phonenumber}}</td>
            <td>{{$value->email}}</td>
            <td>{{$value->created_at}}</td>
                <form method="POST" action="{{ route('admin') }}">
                    @csrf
                    <div class="form-group">
                        {{-- <input type="hidden" name="value" value="{{$value}}"> --}}
                        @if($value->status == 'Waiting')
                            <td><button type="submit" name="action" value="Waiting" class="label label-primary">Waiting</span></td>
                        @else
                            <td><button type="submit" name="action" value="Approved" class="label label-success">Approved</span></td>
                        @endif
                    </div>
                </form>
        </tr>
    @endforeach
    </tbody>
</table>

它在表下方的外部起作用,我不知道为什么它不起作用,任何人都有想法?我还尝试使用链接按钮,但是那也不起作用。

4 个答案:

答案 0 :(得分:1)

尝试将表单移至<td>标记中。 tr> form> td是无效的HTML。

<tr>
    <td>{{$value->id}}</td>
    <td>{{$value->firstname}} {{$value->lastname}}</td>
    <td>{{$value->phonenumber}}</td>
    <td>{{$value->email}}</td>
    <td>{{$value->created_at}}</td>
    <td>
        <form method="POST" action="{{ route('admin') }}">
            @csrf
            <div class="form-group">
                {{-- <input type="hidden" name="value" value="{{$value}}"> --}}
                @if($value->status == 'Waiting')
                    <button type="submit" name="action" value="Waiting" class="label label-primary">Waiting</button>
                @else
                    <button type="submit" name="action" value="Approved" class="label label-success">Approved</button>
                @endif
            </div>
        </form>
    </td>
</tr>

并且您的按钮已用<span>标签关闭。解决该问题。

答案 1 :(得分:0)

您错误地用button关闭了span元素

                        @if($value->status == 'Waiting')
                            <td><button type="submit" name="action" value="Waiting" class="label label-primary">Waiting</button></td>
                        @else
                            <td><button type="submit" name="action" value="Approved" class="label label-success">Approved</button></td>
                        @endif

谢谢。

答案 2 :(得分:0)

确保在{{ route('admin') }}上使用的路由方法

我在终端上创建一个控制器所测试的相同代码

Route::resource('test', 'TestController');

刀片进入

<table class="table table-hover">
    <tbody><tr>
        <th>NO.</th>
        <th>NAME.</th>
        <th>Telephone</th>
        <th>email</th>
        <th>date</th>
        <th></th>
        <th>action</th>
    </tr>
    @foreach($adverts as $value)
        <tr>
            <form method="POST" action="{{ route('test.store') }}">
                @csrf
                <div class="form-group">
                    <input type="hidden" name="id" value="{{$value->id}}">
                    <td><button type="submit" name="action" value="Approved" class="label label-success">Approved</span></td>
                </div>
            </form>
        </tr>
    @endforeach

    </tbody></table>

我的控制器

class TestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        dd($request->all());
    }

注意:我将表单操作指向action="{{ route('test.store') }}"

我认为您使用的表单操作是错误的

答案 3 :(得分:0)

我认为这段代码对您有所帮助。

<table class="table table-hover">
    <tbody>
        <tr>
            <th>NO.</th>
            <th>NAME.</th>
            <th>Telephone</th>
            <th>email</th>
            <th>date</th>
            <th></th>
            <th>action</th>
        </tr>
    @foreach($users as $value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->firstname}} {{$value->lastname}}</td>
            <td>{{$value->phonenumber}}</td>
            <td>{{$value->email}}</td>
            <td>{{$value->created_at}}</td>
            <td>
                <form method="POST" action="{{ route('admin') }}">
                    <input type="hidden" value="{{csrf_token()}}" name="_token">
                    <div class="form-group">
                        {{-- <input type="hidden" name="value" value="{{$value}}"> --}}
                        @if($value->status == 'Waiting')
                            <td><button type="submit" name="action" value="Waiting" class="label label-primary">Waiting</button></td>
                        @else
                            <td><button type="submit" name="action" value="Approved" class="label label-success">Approved</button></td>
                        @endif
                    </div>
                </form>
            </td>
        </tr>
    @endforeach
    </tbody>
</table>