Dynamically Changing Button Names on Click

When click On button Changing Button Names

In HTML Part :-

 @foreach($filteredData as $item)
<form class="add_to_cart_form" data-item-id="{{ $item->id }}" id="add_to_cart_form" enctype="multipart/form-data" role="form">
                @csrf
                <div class="social">
                    <!-- <span class="border border-success"> -->
                    <div class="buttons" id="my_checkbox">
                        @if (!empty($item->face_price))
                        <input type="checkbox" class="btn" name="face_price" id="" value="{{ $item->face_price }}">
                        &nbsp;<a href="{{$item->facebook}}" target="_blank"> Facebook:</a>
                        &nbsp;${{ $item->face_price }}/post</span><br><br>
                        @else
                        <input type="hidden" value="" name="">
                        @endif
      
                        @else
                        <button type="submit" id="sample_form_pic{{ $item->id }}" class="btn btn-sm py-1 btn-primary me-2">Add To Cart</button>
                        @endif




                    </div>
                   


                </div>
            </form>
@endforeach

In javascript :-

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
  $(document).ready(function() {
    $('.add_to_cart_form').submit(function(e) {
        e.preventDefault(); // Prevent form submission
        if ($(this).find(':checkbox:checked').length === 0) {
            alert('Please click on a checkbox.'); // Display warning message
            return;
        }
        // Get the button ID based on the form's data-item-id attribute
        var itemId = $(this).data('item-id');
        var buttonId = '#sample_form_pic' + itemId;

        // Serialize the form data
        var formData = $(this).serialize();

        // Send the Ajax request
        $.ajax({
            url: 'add_summary', // Replace with the actual route URL
            type: 'POST',
            data: formData,
            success: function(response) {
                // Handle the success response here

                // For example, you can update the UI to show that the item is added
                $(buttonId).text('Added').attr('disabled', true);
            },
            error: function(xhr) {
                // Handle the error response here
            }
        });
    });
});



</script>

Output:-

Related Posts

The Future of Computer Operations: How Smart Systems Fix Problems

Every day, millions of people tap smartphone screens to navigate morning traffic, order groceries to their doorsteps, and transfer money between bank accounts in seconds. Behind every…

Read More

How DataOps Improves Alert Accuracy with AI Tools

Data pipelines run constantly to deliver numbers, reports, and dashboards. When something breaks, monitoring systems send alerts to data engineers. But when these notifications ring every ten…

Read More

Core Engineering Skills Needed to Master Data Pipeline Automation Systems

Introduction Imagine building a giant LEGO castle, but someone keeps swapping out your plastic bricks for blocks of melting ice. That is what working with raw digital…

Read More

Continuous Data Validation in DataOps: The Complete Architecture Guide

Continuous data validation is the systematic practice of asserting data correctness, schema consistency, and distribution integrity across every state boundary of an enterprise data pipeline. In DataOps,…

Read More

Implementing XOps: Key Pillars, Common Challenges, and Real-World Solutions

Introduction Modern engineering teams rarely run on pure application code alone. Enterprise software delivery now relies on distributed microservices, complex telemetry pipelines, machine learning inference engines, high-throughput…

Read More

Navigating Urology Treatment: From Early Symptoms to Advanced Care

Introduction Experiencing changes in urinary habits, persistent pelvic discomfort, or sudden kidney pain can feel unsettling. Many individuals delay seeking help because they are uncertain which doctor…

Read More