PHP - тип кнопки submit ничего не делает при нажатии - PullRequest
0 голосов
/ 12 февраля 2020

Я работаю в форме с двумя разделенными элементами, и когда я нажимаю кнопку в нижней части формы, она должна перенаправить меня на новую страницу, но на данный момент она ничего не делает.

Я уже пробовал несколько вещей, которые могли бы это исправить, например:

  1. Изменение типа кнопки на тип ввода
  2. Исправление маршрута для формы
  3. Изменение свойства action тега form в имени функции из контроллера

Вот мой текущий прогресс (изображение веб-страницы, на которой размещена форма):

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, которая отвечает за маршрутизацию / перенаправление на новую страницу при нажатии кнопки Submit:

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

Наконец, источник фрагмента код функции внутри моего контроллера (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
    }

** Вопрос: **

Почему форма ничего не делает, когда я щелкнул это? и как я могу это исправить, чтобы перенаправить пользователя на страницу регистрации при нажатии кнопки отправки?

Ответы [ 2 ]

0 голосов
/ 13 февраля 2020

Как подсказал @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
0 голосов
/ 12 февраля 2020

Здесь - аналогичный пост. Здесь обсуждаются формы с методами и действиями Get, которые могут быть вашей проблемой. Другое решение состоит в том, чтобы изменить это на JavaScript метод, добавив в форму такую ​​функцию, как this . Однако учтите, что этот пост предназначен для отправки форм , а не для сброса страницы

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...