简体   繁体   中英

Laravel insert dynamic input values with radio button

I am a beginner at Laravel, I want to insert a more dynamic address field to the database and I want to select one primary address using the radio button. I can able to add more address fields, but, I am unable to select one primary address because it's selecting all radio button values. I have given below using my code

This is My Blade File

    <div>
          <form action="{{ route('register.post') }}" enctype="multipart/form-data" accept-charset="utf-8" method="POST" >
               @csrf
              <div class="form-group row">
                  <label for="address" class="col-md-4 col-form-label text-md-left">Address</label>
                   <div>
                     <table id="dynamicAddRemove">
                       <tr>
                         <td><button type="button" name="add" id="add-btn" class="btn btn-success">Add More Details</button></td> 
                        </tr>
                        <tr>
                          <td>
                              
                          </td>
                          <tr>
                      </table>
                      </div>
                  </div>    
                  <div class="col-md-6 offset-md-4">
                    <button type="submit" class="btn btn-primary">
                            Save
                     </button>
                 </div>
                </form>
                <div>
                <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
                <script type="text/javascript">
                    var i = 0;
                    $("#add-btn").click(function(){
                        //alert("I am an alert box!");
                    ++i;
                    $("#dynamicAddRemove").append('<tr><td><input type="text" name="moreFields['+i+'][house_no]" placeholder="House No" class="form-control" /></td><td><input type="text" name="moreFields['+i+'][street_name]" placeholder="Street Name" class="form-control" /></td><td><input type="text" name="moreFields['+i+'][area]" placeholder="Area" class="form-control" /></td><td><input type="text" name="moreFields['+i+'][pincode]" placeholder="Pincode" class="form-control" /></td><td style="padding-left:50px;"><input type="radio" class="form-check-input" name="moreFields['+i+'][primary_address]" value="'+i+'"></td><td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>');
                    });
                    $(document).on('click', '.remove-tr', function(){  
                    $(this).parents('tr').remove();
                    });  
                    </script>

This is My Controller File

 public function postRegistration(Request $request)
{  
    $id = DB::getPdo()->lastInsertId();
    foreach ($request->moreFields as $key => $value) {
      
        Address::create([
            'user_id'=>$id,
            'house_no'=>$value['house_no'],
            'street_name'=>$value['street_name'],
            'area'=>$value['area'],
            'pincode'=>$value['pincode'],
            'primary_address'=>$value['primary_address'],
            
        ]);
    }

You dont need to make different name radio buttons use same name for all radio buttons so that user can not select other radio button after selecting one

<input type="radio" class="form-check-input" name="moreFields" value="'+i+'">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM