-1

I'm new to the Laravel universe and I'm putting the finishing touches on an ecommerce app. The final frontier now is getting the dynamic shipping rate, and adding it to my overall order total, but I'm at a loss as to how to do this. The rates are coming through a foreach loop like this

 <?php $i = 0; ?>
   @foreach ($rates as $rate)

   <tr>
       <td>
           <img src="{{ $rate->provider_image_75 }}" alt="">
           {{ $rate->provider }} ({{ $rate->duration_terms }})
       </td>
       <td width="20%">
           <input type="radio" class="pull-right" name="rate_{{$i}}" value="{{ $rate->object_id }}">
           ${{ $rate->amount }}
       </td>
   </tr>
   <?php $i++; ?>

But all the inputs are selectd when I click on them. Because I have the code directly in the laravel blade, I'm not using the controller. I'm unsure on how to get the selected rate, and only the selected rate to be added to the $cartTotal variable. I need a little more clarification on this, because I'm new to php development. Thanks.

D.T
  • 15
  • 5
  • What is the problem you're having? There's nothing wrong with the code you're showing. – miken32 Nov 19 '21 at 00:01
  • The problem is, all the rates I click get selected, as opposed to only one. The second problem is, I need only that selected rate to be added to my $cartTotal variable so I pass the shipping costs to the customer. That's the goal. Sorry for being sloe, but I'm new to laravel and php development. I'm learning on the fly. – D.T Nov 19 '21 at 00:03
  • 1
    Radio buttons with the same name only allow one to be selected though. You absolutely don’t want to give them all different names. That’s not how radio buttons work. – miken32 Nov 19 '21 at 05:40

1 Answers1

-1

My apologies for misleading you on my last answer.. As our friend miken32 said, radio buttons should have same name. I thought you are using checkboxes.. again, sorry! Remove my edits from your code.

I'm not sure how exactly you are trying to get the value. Do you want to add the rate without submitting a form? As far as i know that's not the best way to do it. You can create the radio inputs with the same name and different values. Put them in a form, create a route and set the route as form action attribute and get the value of selected radio button using $request->rate and add it to your $cartTotal variable.

Tinxuanna
  • 93
  • 2
  • 14
  • That's what I'm tyring to do, but the shipping rate api is totally dynamic, meaning that I can't use static values. I've been told with a foreach loop that I have to perform a $loop index function to get the $request->rate. That's the hold up. – D.T Nov 19 '21 at 16:19