提交表单数据后,页面未在 laravel 中正确加载

Posted

技术标签:

【中文标题】提交表单数据后,页面未在 laravel 中正确加载【英文标题】:after submitting form data, page not being properly loaded in laravel 【发布时间】:2019-11-14 19:53:00 【问题描述】:

我有一个网站,其中有一个表单,用户提交后的表单数据发送到我的数据库和电子邮件。

下面是我的控制器和路由

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Register;
use App\Mail\SendEmail;
use Session;
use Mail;
use Storage;
use Illuminate\Support\Facades\File;

class RegController extends Controller


    public function index()
    
        //
    


    public function create()
    
        //
    

    public function store(Request $request)
    

       // return $request;


// return $request->Fee;
            $data = new Register;
            $data->firstname = $request->firstname;
            $data->lastname = $request->lastname;
            $data->mobilenumber = $request->mobilenumber;
            $data->experience = $request->experience;
            $data->designation = $request->designation;
            $data->confirmation = $request->confirmation;
            $data->company = $request->company;
            $data->address = $request->address;
            $data->addressline2 = $request->addressline2;
            $data->city = $request->city;
            $data->state = $request->state;
            $data->pin = $request->pin;
            $data->country = $request->country;
            $data->Whatsapp = $request->Whatsapp;
            $data->alternatenumber = $request->alternatenumber;
            $data->Email = $request->Email;
            $data->CompanyRegNumber = $request->CompanyRegNumber;
            $data->Date_Incorporation_orBusi_Stp = $request->Date_Incorporation_orBusi_Stp;
            $data->GSTIN = $request->GSTIN;
            $data->Services_Offered = $request->Services_Offered;
            $data->Annual_Turnover = $request->Annual_Turnover;
            $data->Declaration = $request->Declaration;

            $data->termsandconditions = $request->termsandconditions;
            // $data->Fee = $request->Fee;
            $data->Mode_of_Payment = $request->Mode_of_Payment;

            $data->Recommended_by_A = $request->Recommended_by_A;
            $data->Recommended_by_B = $request->Recommended_by_B;

              $fee = $request->Fee[0];
              $data->Fee = $fee;

              if(!empty($request->Fee[1]))
              
               $data->Fee1 = $request->Fee[1];
              
              if(!empty($request->Fee[2]))
              
               $data->Fee2 = $request->Fee[2];
              
            if(!empty($request->Fee[3]))
              
               $data->Fee3 = $request->Fee[3];
              


 if($request->hasFile('Photo')) 
      $image = $request->file('Photo');
      $filename = time().$image->getClientOriginalName();
      $destinationPath = public_path('uploads/Photo');

     Storage::disk('public')->put($filename,  File::get($image));
      $data->Photo=$filename;
    
 if($request->hasFile('Receipt')) 
      $image = $request->file('Receipt');
      $filename = time().$image->getClientOriginalName();
      $destinationPath = public_path('uploads/Receipt');
    Storage::disk('public')->put($filename,  File::get($image));
      $data->Receipt=$filename;
    
 if($request->hasFile('Signature')) 
      $image = $request->file('Signature');
      $filename = time().$image->getClientOriginalName();
      $destinationPath = public_path('uploads/Signature');
      Storage::disk('public')->put($filename,  File::get($image));
      $data->Signature=$filename;
    

            $data->save();
            Session::flash('registered','reg');


        $name = $request->firstname." ".$request->lastname;
        $subject = "TEIA";
        $message =  $data->id;

         $photo = $data->Photo;
         $sign = $data->Signature;
         $recipt= $data->Receipt;
    $email = 'zubairking@gmail.com';


        // ->cc(['name1@domain.com','name2@domain.com'])
      Mail::to($email)->send( new SendEmail($subject,$message , $photo ,$sign,$recipt));




            return back();
    


    public function show($id)
    
        //
    


    public function edit($id)
    
        //
    


    public function update(Request $request, $id)
    
        //
    


    public function destroy($id)
    
        //
    

Route::get('/', function () 
	// Session::flush();
    return view('welcome');
);
Route::resource('register','RegController');

用户提交注册按钮后,数据被提交,然后用户被带到顶部带有成功消息的表单。问题是当用户第一次提交数据时,页面卡在https://booktheparty.in/TEIA/register,页面空白。然后当用户返回并再次提交时,页面被定向到表单页面并显示成功消息。但仍然将数据添加到数据库中 2 次。我想知道为什么我的页面第一次就卡住了。谁能告诉我

【问题讨论】:

您是否尝试使用 header() 或 redirect() 来测试您的代码是否返回?还会触发一些错误吗?你的 laravel.log 显示了什么? 您是否尝试更改return back();到return redirect('/register') ? @Jovs 寄存器是我的 php 代码所在的位置,我要重定向到的页面是“welcomeblade”.. 那是主页,我将如何做到这一点 大约第二次“工作”实际上您看到的消息是上一个请求的会话闪存。但正如我和@Jovs 告诉你的那样,你是否尝试将return back(); 更改为return redirect("/register");,因为测试建议你也可以使用 header("Location: register"),但是会话闪存它不会工作。 @FabioWilliamConceição 我是 laravel 的新手,“注册”是我的控制器,我的表单在索引刀片中。您能否建议使用您所说的格式来回答如何做到这一点 【参考方案1】:

请将此代码替换为您的商店功能

 public function store(Request $request)
    

       // return $request;


// return $request->Fee;
            $data = new Register;
            $data->firstname = $request->firstname;
            $data->lastname = $request->lastname;
            $data->mobilenumber = $request->mobilenumber;
            $data->experience = $request->experience;
            $data->designation = $request->designation;
            $data->confirmation = $request->confirmation;
            $data->company = $request->company;
            $data->address = $request->address;
            $data->addressline2 = $request->addressline2;
            $data->city = $request->city;
            $data->state = $request->state;
            $data->pin = $request->pin;
            $data->country = $request->country;
            $data->Whatsapp = $request->Whatsapp;
            $data->alternatenumber = $request->alternatenumber;
            $data->Email = $request->Email;
            $data->CompanyRegNumber = $request->CompanyRegNumber;
            $data->Date_Incorporation_orBusi_Stp = $request->Date_Incorporation_orBusi_Stp;
            $data->GSTIN = $request->GSTIN;
            $data->Services_Offered = $request->Services_Offered;
            $data->Annual_Turnover = $request->Annual_Turnover;
            $data->Declaration = $request->Declaration;

            $data->termsandconditions = $request->termsandconditions;
            // $data->Fee = $request->Fee;
            $data->Mode_of_Payment = $request->Mode_of_Payment;

            $data->Recommended_by_A = $request->Recommended_by_A;
            $data->Recommended_by_B = $request->Recommended_by_B;

              $fee = $request->Fee[0];
              $data->Fee = $fee;

              if(!empty($request->Fee[1]))
              
               $data->Fee1 = $request->Fee[1];
              
              if(!empty($request->Fee[2]))
              
               $data->Fee2 = $request->Fee[2];
              
            if(!empty($request->Fee[3]))
              
               $data->Fee3 = $request->Fee[3];
              


 if($request->hasFile('Photo')) 
      $image = $request->file('Photo');
      $filename = time().$image->getClientOriginalName();
      $destinationPath = public_path('uploads/Photo');

     Storage::disk('public')->put($filename,  File::get($image));
      $data->Photo=$filename;
    
 if($request->hasFile('Receipt')) 
      $image = $request->file('Receipt');
      $filename = time().$image->getClientOriginalName();
      $destinationPath = public_path('uploads/Receipt');
    Storage::disk('public')->put($filename,  File::get($image));
      $data->Receipt=$filename;
    
 if($request->hasFile('Signature')) 
      $image = $request->file('Signature');
      $filename = time().$image->getClientOriginalName();
      $destinationPath = public_path('uploads/Signature');
      Storage::disk('public')->put($filename,  File::get($image));
      $data->Signature=$filename;
    

            $data->save();
            Session::flash('registered','reg');


        $name = $request->firstname." ".$request->lastname;
        $subject = "TEIA";
        $message =  $data->id;

         $photo = $data->Photo;
         $sign = $data->Signature;
         $recipt= $data->Receipt;
    $email = 'zubairking@gmail.com';


        // ->cc(['name1@domain.com','name2@domain.com'])
      Mail::to($email)->send( new SendEmail($subject,$message , $photo ,$sign,$recipt));




            return redirect()->back();
    

希望对你有用

【讨论】:

【参考方案2】:

尝试添加此代码:

return redirect('write the route you want to be redirected ');

在控制器中保存数据后。

【讨论】:

【参考方案3】:

return redirect()-&gt;back();return redirect('')

【讨论】:

以上是关于提交表单数据后,页面未在 laravel 中正确加载的主要内容,如果未能解决你的问题,请参考以下文章

使用 laravel 框架提交数据后清除表单输入

我的表单在 Laravel 中未正确提交

在 Laravel 中提交表单后如何重定向确认页面?

表单提交后,在 laravel 中向用户显示提交的数据

http地址中如何加参数and传递form表单中的参数

PHP笔记-laravel框架记录session中flash()的一次应用