Otis
Otis

Reputation: 411

How pass input field value using route parameter to a controller in laravel

How can i pass an input field value using route parameter to my controller. i want to pass the value of the input field below to my controller after clicking the anchor tag below, How can i achieve that?

My code below

 <input type="text"  name="sender_address" value="Alberta" class="form-control margin-bottom-20 hide" style="border:1px solid  brown;" placeholder=" Enter & Save address">

 <a href="/sender_address/{sender_address}">Save Address</a>

Upvotes: 0

Views: 2164

Answers (2)

Ashikul
Ashikul

Reputation: 51

You can use the below 3 option to pass an input value

1)you can use Form

<form class="form-horizontal" action="path_to_action" method="POST">
      @csrf
    <input type="text"  name="sender_address" value="Alberta" class="form-control margin-bottom-20 hide" style="border:1px solid  brown;" placeholder=" Enter & Save address">
<button type="submit" class="btn btn-add btn-lg">Save Address</button>    
</form>

2)You can use jQuery / AjAX to do that.

<input type="text" id="sender_address" name="sender_address" value="Alberta" class="form-control margin-bottom-20 hide" style="border:1px solid  brown;" placeholder=" Enter & Save address">
 <a class="save_address">Save Address</a>
$('#save_address').on('click', function(e){
   e.preventDefault();
   var address = $('#sender_address').val();
   
   $.ajax({
        type: 'POST',
        url: '/sender_address',
        data: {
            sender_address : address
        },
        async: false,
        dataType: 'json',
        success: function (response) {
            console.log(response);
        }
    });
});

3)You can use the <a> tag

<a href="{{ route('sender_address', $sender->id) }}"></a>

If you use <a href="{{ route('sender_address', $sender->id) }}"></a> you need to retrieve data from database using that ID

Upvotes: 1

Abdul Hakeem
Abdul Hakeem

Reputation: 405

it's not recommended way to achieve that requirement, as an alternative you can pass the values to controller via a AJAX POST method with the help of jquery.

 <input type="text" id="sender_address" name="sender_address" value="Alberta" class="form-control margin-bottom-20 hide" style="border:1px solid  brown;" placeholder=" Enter & Save address">
 <a class="save_address">Save Address</a>

I've added class to your anchor tag. and let's continue

$('.save_address').on('click', function(e){
   e.preventDefault();
   var address = $('#sender_address').val();
   
   $.ajax({
        type: 'POST',
        url: '/sender_address',
        data: {
            sender_address : address
        },
        async: false,
        dataType: 'json',
        success: function (response) {
            console.log(response);
        }
    });
});

Upvotes: 3

Related Questions