PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 16, 2022

[FIXED] How to pass radio button value as route parameter in Laravel?

 November 16, 2022     html, javascript, jquery, laravel, laravel-6     No comments   

Issue

I want to pass the selected radio button value as a parameter with Laravel route.

My Route is:

Route::resource('/datas','DataController');

From this route, I am aiming to call localhost:8000/datas/{data}

My data.blade.php is:

<form id="dataform" action="{{ route('datas.show')}}" method="GET">
   <table class="table">
      <tr>
         <th>Select bullet</th>
         <th>SL NO</th>
         <th>Name</th>
         <th>Age</th>
      </tr>
      @foreach ($datas $key => $data)
         <tr>
            <td><input type="radio" id="data{{$data->id}}" name="data" value={{$data->id}}></option></td>
            <td>{{$key}}</td>
            <td>{{$data->name}}</td>
            <td>{{$data->age}}</td>
         </tr>
      @endforeach
   </table>
   <button type="submit" id="edit_submit" class="btn btn-default">Show</button>                    </form>

My show function will be...


public function show($id)
   {
      //Code to showing data and redirect to show page
   }

I want to get value of this radio button(given below).

<td><input type="radio" id="data{{$data->id}}" name="data" value={{$data->id}}>

and include it as a parameter with the Form action below

<form id="dataform" action="{{ route('datas.show')}}" method="GET">

Solution

I have made some changes to this code. Changed button to anchor tag. So my blade.php is:-

   <table class="table">
      <tr>
         <th>Select bullet</th>
         <th>SL NO</th>
         <th>Name</th>
         <th>Age</th>
      </tr>
      @foreach ($datas $key => $data)
         <tr>
            <td><input type="radio" id="data{{$data->id}}" name="data" value={{$data->id}}></option></td>
            <td>{{$key}}</td>
            <td>{{$data->name}}</td>
            <td>{{$data->age}}</td>
         </tr>
      @endforeach
   </table>
   <a  href="javascript:showfunction()" id="showlink" class="btn btn-default">Show</a>

and I added a javascript function too.

<script>
    function showfunction(){
       var id = document.querySelector('input[name = "data"]:checked').value;
       var url = '{{route("admin.questions.show",":id")}}';
       url=url.replace(':id',id);
       document.location.href=url;
    }
</script>


Answered By - Hisham U
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing