douni1396 2016-10-05 17:31
浏览 57
已采纳

Laravel:如何在刀片中将变量从一个页面发送到另一个页面

I'm building a laravel application where from The first page I want to send a variable or say value to second page in a form ..How do I do it? Let's say In my first page I have several buttons so when a user click on any one button, he will be redirected to another page where he has to submit a form. In first page while he select a button, he automatically select a course_id which will also submit inside the second page form. But How do i send the course_id from the first page button?

<li> <a  href="{{route('registration')}}">A</a> </li>
 <li> <a href="{{route('registration')}}" >B</a> </li> 

When a user click on the button second page will appear ..Where I'm gonna submit a form where course_id will come from the first page means the <a> tag

Here is my form demo:

{!! Form::open(array('route' => 'postRegistration','class'=>'form-horizontal','method'=>'POST'))  !!}
                {!! Form::token(); !!}
              {!!   csrf_field() ; !!} 

    <div class="form-group">
              <label class="sr-only" for="form-first-name">Name</label>
                      <input type="text" name="name" placeholder="Name..." class="form-first-name form-control" id="form-first-name">
   </div>
     <div class="form-group">
                <label class="sr-only" for="form-email">Email</label>
                       <input type="text" name="email" placeholder="Email..." class="form-email form-control" id="form-email">
       </div>

      <div class="form-group">
           <label class="sr-only" for="form-last-name">Address</label>
              <textarea rows="4" style="height:100px;" name="address" placeholder="Address..." class="form-last-name form-control" id="form-last-name"></textarea>
        </div>
<button type="submit" class="btn">Submit</button>
             {!! Form::close() !!}

In my database I have to submit the above field as well as the course_id from the database. How do I do it in Laravel?

Any suggestion or solution please?

  • 写回答

3条回答 默认 最新

  • dongwei1921 2016-10-05 20:00
    关注

    You can send the variable as route parameter. To do this change your <a> tags like this.

    <li> <a href="{{route('registration', ['course_id' => 'A'])}}">A</a> </li>
    <li> <a href="{{route('registration', ['course_id' => 'B'])}}">B</a> </li> 
    

    Then you need to allow your registration route to take parameter. Suppose your route is like this

    Route::get('/registration', [
        'as' => 'registration', 'uses' => 'SomeController@method'
    ]);
    

    Change it to

    Route::get('/registration/{course_id}', [
        'as' => 'registration', 'uses' => 'SomeController@method'
    ]);
    

    Now you have to add this parameter into your SomeController@method

    public method($course_id){ ... }
    

    Now pass this $course_id to your form view. To submit this variable with your other fields you can add this as hidden input field.

    <div class="form-group">
        <input type="hidden" name="course_id" value="{{ $course_id }}">
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵