【问题标题】:PHP - Button type submit does nothing when clickedPHP - 单击时按钮类型提交什么也不做
【发布时间】:2020-05-28 04:56:53
【问题描述】:

我正在使用具有两个分隔的 div 的表单,当我单击表单底部的按钮时,它应该将我重定向到一个新页面,但到目前为止它什么也没做。

我已经尝试了一些可以解决它的方法,例如:

  1. 将按钮类型更改为输入类型
  2. 修复表单路径
  3. 从控制器将表单标签的action属性改成函数名

这是我目前的进度(放置表单的网页图片):

Div 1
Div 2

这是我的视图源代码 (SelectFlight.blade.php):

@extends('layouts.app')

@section('title', 'Select Flight')
@section('content')
<form method="GET" action="{{route('PassengerRegister')}}" autocomplete = "off">
    @csrf
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-13">
                <div class="card">
                    <div class="card-header bg-primary">
                        <i class="fas fa-plane-departure"></i><b>
                        {{ __('Select your Departing Flight') }}

                            @foreach($flights_depart as $flight)
                            {{ "(".$flight->date_departure.")" }}
                            @break
                            @endforeach

                        </b></div>
                        <div class="card-body">
                                @csrf

                                <table class="table">
                                <thead>
                                    <tr> 
                                    <p style = "text-align: center">
                                    <b>
                                        List of flights from 
                                            {{$depart_source_country}} to 
                                        @foreach($depart_destination_country as $destination)
                                            {{$destination->country_name}}
                                        @endforeach
                                    </b>
                                    </p>
                                    <th scope="col">FLIGHT NO.</th>
                                    <th scope="col">DEPARTURE TIME / ARRIVAL TIME</th>
                                    <th scope="col">DEPARTURE LOCATION </th>
                                    <th scope="col">ARRIVAL LOCATION </th>
                                    <th scope="col">FLIGHT DURATION </th>
                                    <th scope="col">FLIGHT TYPE </th>
                                    <th scope="col">RESERVE TICKET </th>
                                    </tr>
                                </thead>
                                <tbody>

                                    @php 
                                        $counter1 = 0;
                                        $duration_1 = ''; 
                                        $flight_type_name = '';
                                        $flight_type_price;
                                        $total_fare;
                                    @endphp

                                    @foreach($flights_depart as $flight)
                                    <tr>
                                        <td>{{ $flight->flight_no }}</td>
                                        <td>{{ $flight->time_departure }} - {{ $flight->time_arrival }}</td>
                                        <td>{{ $depart_source_country }}</td>

                                        @foreach($depart_destination_country as $destination)
                                            <td>{{ $destination->country_name }}</td>
                                        @endforeach

                                        @for($y=$counter1; $y<@count($first_durations); $y++)
                                            @php $duration_1 = $first_durations[$y]; @endphp
                                            @break;
                                        @endfor

                                        <td> {{ $duration_1 }} Hours</td>

                                        @for($df=$counter1; $df<@count($departing_flight_types); $df++)
                                            @php $flight_type_name = $departing_flight_types{$df}; @endphp
                                            @break;
                                        @endfor

                                        <td>{{$flight_type_name[0]}}</td>

                                        @for($df=$counter1; $df<@count($departing_flight_prices); $df++)
                                            @php $flight_type_price = $departing_flight_prices{$df}; @endphp
                                            @break;
                                        @endfor

                                        @foreach($depart_destination_country as $destination)
                                        <td>

                                            <input type="radio" id = "flight_id" name="flight_id" 
                                                    value="{{$flight->flight_id}}" required/>

                                                    @php  
                                                        $total_fare = intval($book_details['seat_qty'] * ($flight_type_price[0] + $destination->country_price));
                                                        echo "PHP ". number_format($total_fare, 2, '.', ',');
                                                    @endphp

                                            <input type="hidden" name="total_fare" value="{{$total_fare}}"/>

                                        </td>
                                        @endforeach
                                    </tr>
                                    @php 
                                        $counter1++;
                                    @endphp

                                    @endforeach



                                </tbody>
                                </table>
                            </form>
                        </div>
                </div>
            </div>
        </div>
    </div>

    <hr>
    @if($flights_return ?? '' != null)
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-13">
                <div class="card">
                    <div class="card-header bg-primary">
                        <i class="fas fa-plane-arrival"></i><b>
                        {{ __('Select your Returning Flight') }}

                        @foreach($flights_return ?? '' as $flight)
                            {{ "(".$flight->date_departure.")" }}
                        @break
                        @endforeach

                        </b></div>
                        <div class="card-body">
                            <form method="GET" autocomplete = "off">
                                @csrf

                                <table class="table">
                                <thead>
                                    <tr> 
                                    <p style = "text-align: center">
                                    <b>
                                        List of flights from 
                                        {{$return_source_country}} to 
                                        @foreach($return_destination_country as $destination)
                                            {{$destination->country_name}}
                                        @endforeach
                                    </b>
                                    </p>
                                    <th scope="col">FLIGHT NO.</th>
                                    <th scope="col">DEPARTURE TIME / ARRIVAL TIME</th>
                                    <th scope="col">DEPARTURE LOCATION </th>
                                    <th scope="col">ARRIVAL LOCATION </th>
                                    <th scope="col">FLIGHT DURATION </th>
                                    <th scope="col">FLIGHT TYPE </th>
                                    <th scope="col">RESERVE TICKET </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php 
                                        $counter2 = 0; 
                                        $flight_type_name = '';
                                    @endphp
                                    @foreach($flights_return ?? '' as $flight)
                                    <tr>
                                        <td>{{ $flight->flight_no }}</td>
                                        <td>{{ $flight->time_departure }} - {{ $flight->time_arrival }}</td>
                                        <td>{{ $return_source_country }}</td>

                                        @foreach($return_destination_country as $destination)
                                            <td>{{ $destination->country_name }}</td>
                                        @endforeach

                                        @for($x=$counter2; $x<@count($second_durations); $x++)
                                            @php $duration_2 = $second_durations[$x]; @endphp
                                            @break;
                                        @endfor
                                        <td> {{ $duration_2 }} Hours </td>

                                        @for($df=$counter2; $df<@count($returning_flight_types); $df++)
                                            @php $flight_type_name = $returning_flight_types{$df}; @endphp
                                            @break;
                                        @endfor

                                        <td>{{$flight_type_name[0]}}</td>

                                        @for($df=$counter2; $df<@count($returning_flight_prices); $df++)
                                            @php $flight_type_price = $returning_flight_prices{$df}; @endphp
                                            @break;
                                        @endfor

                                        @foreach($return_destination_country as $destination)
                                            <td>
                                                <input type="radio" id = "flight_id" name="flight_id" 
                                                        value="{{$flight->flight_id}}" required/>

                                                        @php  
                                                            $total_fare = intval($book_details['seat_qty'] * ($flight_type_price[0] + $destination->country_price));
                                                            echo "PHP ". number_format($total_fare, 2, '.', ',');
                                                        @endphp

                                                <input type="hidden" name="total_fare" value="{{$total_fare}}"/>
                                            </td>
                                        @endforeach
                                    </tr>
                                    @php $counter2++; @endphp
                                    @endforeach

                                </tbody>
                                </table>
                            </form>
                        </div>
                </div>
            </div>
        </div>
    </div>
    @endif

    <br><br>
    <div class="form-group row mb-0">
        <div class="col-md-6 offset-md-10">
            <button type="submit" class="btn btn-primary">
                {{ __('Continue') }}
            </button>
        </div>
    </div>
</form>
@endsection

这是我在 web.php 上的代码行,它负责在单击提交按钮时路由/重定向到新页面:

Route::get('PassengerRegister', 'BookController@PassengerRegister')->name('PassengerRegister');

最后是我的控制器里面一个函数的sn-p源码(BookController.php)

public function PassengerRegister(Request $request){
        return view("register");  //As of this moment it will just redirect to register view when submit button is clicked
    }

**问题:**

为什么当我点击表单时它什么也不做?以及如何解决它以便在单击提交按钮时将用户重定向到注册页面?

【问题讨论】:

  • 表单标签中有表单标签,所以它不起作用
  • 甚至打开和关闭form标签的数量都不匹配,你有两个前者,三个后者。 “为什么当我点击它时表单什么也不做?” - 因为你的按钮一开始就不被认为是表单的一部分(正确的表单),因为你把 HTML 结构搞砸了。
  • 是的,我发现
    的结束标签太多了,甚至在主表单中也有表单。现在我修复了 html 代码并删除了不必要的过多标签,这些标签不匹配并且现在正在工作:)

标签: php html laravel


【解决方案1】:

Here 是一个类似的帖子。它讨论了带有 Get 方法和操作的表单,这可能是您的问题。另一种解决方案是通过向您的表单添加一个函数(如this)将其更改为 JavaScript 方法。 但是,请记住,这篇文章是为了提交表单而不是重置页面

【讨论】:

    【解决方案2】:

    正如@Curstis 建议的那样。您有几个表单标签散布在此页面周围。现在,我已经为您删除了它们,只留下了第一个和最后一个表单标签。所以试试下面的代码,表单应该现在提交。始终确保您的提交按钮是这样绑定的:

    ...
    。让它工作。
     @extends('layouts.app')
    
     @section('title', 'Select Flight')
     @section('content')
     <form method="GET" action="{{route('PassengerRegister')}}" autocomplete="off">
    @csrf
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-13">
                <div class="card">
                    <div class="card-header bg-primary">
                        <i class="fas fa-plane-departure"></i><b>
                            {{ __('Select your Departing Flight') }}
    
                            @foreach($flights_depart as $flight)
                            {{ "(".$flight->date_departure.")" }}
                            @break
                            @endforeach
    
                        </b></div>
                    <div class="card-body">
                        @csrf
    
                        <table class="table">
                            <thead>
                                <tr>
                                    <p style="text-align: center">
                                        <b>
                                            List of flights from
                                            {{$depart_source_country}} to
                                            @foreach($depart_destination_country as $destination)
                                            {{$destination->country_name}}
                                            @endforeach
                                        </b>
                                    </p>
                                    <th scope="col">FLIGHT NO.</th>
                                    <th scope="col">DEPARTURE TIME / ARRIVAL TIME</th>
                                    <th scope="col">DEPARTURE LOCATION </th>
                                    <th scope="col">ARRIVAL LOCATION </th>
                                    <th scope="col">FLIGHT DURATION </th>
                                    <th scope="col">FLIGHT TYPE </th>
                                    <th scope="col">RESERVE TICKET </th>
                                </tr>
                            </thead>
                            <tbody>
    
                                @php
                                $counter1 = 0;
                                $duration_1 = '';
                                $flight_type_name = '';
                                $flight_type_price;
                                $total_fare;
                                @endphp
    
                                @foreach($flights_depart as $flight)
                                <tr>
                                    <td>{{ $flight->flight_no }}</td>
                                    <td>{{ $flight->time_departure }} - {{ $flight->time_arrival }}</td>
                                    <td>{{ $depart_source_country }}</td>
    
                                    @foreach($depart_destination_country as $destination)
                                    <td>{{ $destination->country_name }}</td>
                                    @endforeach
    
                                    @for($y=$counter1; $y<@count($first_durations); $y++) @php $duration_1=$first_durations[$y]; @endphp @break; @endfor <td> {{ $duration_1 }} Hours</td>
    
                                        @for($df=$counter1; $df<@count($departing_flight_types); $df++) @php $flight_type_name=$departing_flight_types{$df}; @endphp @break; @endfor <td>{{$flight_type_name[0]}}</td>
    
                                            @for($df=$counter1; $df<@count($departing_flight_prices); $df++) @php $flight_type_price=$departing_flight_prices{$df}; @endphp @break; @endfor @foreach($depart_destination_country as $destination) <td>
    
                                                <input type="radio" id="flight_id" name="flight_id" value="{{$flight->flight_id}}" required />
    
                                                @php
                                                $total_fare = intval($book_details['seat_qty'] * ($flight_type_price[0] + $destination->country_price));
                                                echo "PHP ". number_format($total_fare, 2, '.', ',');
                                                @endphp
    
                                                <input type="hidden" name="total_fare" value="{{$total_fare}}" />
    
                                                </td>
                                                @endforeach
                                </tr>
                                @php
                                $counter1++;
                                @endphp
    
                                @endforeach
    
    
    
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <hr>
    @if($flights_return ?? '' != null)
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-13">
                <div class="card">
                    <div class="card-header bg-primary">
                        <i class="fas fa-plane-arrival"></i><b>
                            {{ __('Select your Returning Flight') }}
    
                            @foreach($flights_return ?? '' as $flight)
                            {{ "(".$flight->date_departure.")" }}
                            @break
                            @endforeach
    
                        </b></div>
                    <div class="card-body">
    
                        <table class="table">
                            <thead>
                                <tr>
                                    <p style="text-align: center">
                                        <b>
                                            List of flights from
                                            {{$return_source_country}} to
                                            @foreach($return_destination_country as $destination)
                                            {{$destination->country_name}}
                                            @endforeach
                                        </b>
                                    </p>
                                    <th scope="col">FLIGHT NO.</th>
                                    <th scope="col">DEPARTURE TIME / ARRIVAL TIME</th>
                                    <th scope="col">DEPARTURE LOCATION </th>
                                    <th scope="col">ARRIVAL LOCATION </th>
                                    <th scope="col">FLIGHT DURATION </th>
                                    <th scope="col">FLIGHT TYPE </th>
                                    <th scope="col">RESERVE TICKET </th>
                                </tr>
                            </thead>
                            <tbody>
                                @php
                                $counter2 = 0;
                                $flight_type_name = '';
                                @endphp
                                @foreach($flights_return ?? '' as $flight)
                                <tr>
                                    <td>{{ $flight->flight_no }}</td>
                                    <td>{{ $flight->time_departure }} - {{ $flight->time_arrival }}</td>
                                    <td>{{ $return_source_country }}</td>
    
                                    @foreach($return_destination_country as $destination)
                                    <td>{{ $destination->country_name }}</td>
                                    @endforeach
    
                                    @for($x=$counter2; $x<@count($second_durations); $x++) @php $duration_2=$second_durations[$x]; @endphp @break; @endfor <td> {{ $duration_2 }} Hours </td>
    
                                        @for($df=$counter2; $df<@count($returning_flight_types); $df++) @php $flight_type_name=$returning_flight_types{$df}; @endphp @break; @endfor <td>{{$flight_type_name[0]}}</td>
    
                                            @for($df=$counter2; $df<@count($returning_flight_prices); $df++) @php $flight_type_price=$returning_flight_prices{$df}; @endphp @break; @endfor @foreach($return_destination_country as $destination) <td>
                                                <input type="radio" id="flight_id" name="flight_id" value="{{$flight->flight_id}}" required />
    
                                                @php
                                                $total_fare = intval($book_details['seat_qty'] * ($flight_type_price[0] + $destination->country_price));
                                                echo "PHP ". number_format($total_fare, 2, '.', ',');
                                                @endphp
    
                                                <input type="hidden" name="total_fare" value="{{$total_fare}}" />
                                                </td>
                                                @endforeach
                                </tr>
                                @php $counter2++; @endphp
                                @endforeach
    
                            </tbody>
                        </table>
    
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endif
    
    <br><br>
    <div class="form-group row mb-0">
        <div class="col-md-6 offset-md-10">
            <button type="submit" class="btn btn-primary">
                {{ __('Continue') }}
            </button>
        </div>
    </div>
     </form>
     @endsection
    

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 2021-10-12
      • 1970-01-01
      • 2017-08-15
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多