0

I'm new to laravel, I'm trying to achieve a functionality when selecting a year for a specific ID or number that year will be removed from the dropdown list or it will not be listed. Below I added screenshots and my code so far. I'm actually struggling to figure this out. :(

Please let me know if you need to check my controller.php and others.

Here's my code: JS:

$(document).ready(function () {
    // input fields
    $("#tax-dec-form").on('submit', function (e) {
        e.preventDefault()
        $('#tax-dec-form').find('span.error').remove() //resets error messages

        let _url = null;
        let data = $('#tax-dec-form').serialize();
        if ($('#tax-dec-form').hasClass('new')) {
            _url = 'add-tax-info'
            data = $('#tax-dec-form').serialize() + "&pin_id=" + pin_id
        } else {
            _url = 'update-tax-info'
        }

        $.ajax({
            url: _url,
            type: "POST",
            data: data,
            success: function (response) {
                if (response.code == 200) {
                    //console.log(response)
                    $('#tax-dec-form').removeClass('new');
                    swal({ title: "Success!", text: response.message, type: "success", buttonsStyling: false, confirmButtonClass: "btn btn-success" })
                }
            },
            error: function (response) {
                console.warn(response.responseJSON.errors)
                $.each(response.responseJSON.errors, function (field_name, error) {
                    if ($(document).find('#tax-dec-form [name=' + field_name + ']').next().is('span')) {
                        $(document).find('#tax-dec-form [name=' + field_name + ']').next('span').remove()
                    }
                    $(document).find('#tax-dec-form [name=' + field_name + ']').after('<span class="error text-danger">' + error + '</span>')

                })
            }
        })
    })
    $('[data-toggle="tooltip"]').tooltip()
})

//for dynamic year list
$(document).ready(function () {
    var d = new Date();
    for (var i = 0; i <= 40; i++) {
        var option = "<option value=" + parseInt(d.getFullYear() + i) + ">" + parseInt(d.getFullYear() + i) + "</option>"
        $('[id*=DropDownList1]').append(option);
    }
});

Blade.php:

<div class="content menu-css">
    <div class="container-fluid">
        {{-- upper form here --}}
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header card-header-primary">
                        <h4 class="card-title">Tax Information</h4>
                        <p class="card-category">Please complete all fields</p>
                    </div>
                    <div class="form-group">&nbsp</div>
                    <div class="card-body">
                        @if (session('status'))
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="alert alert-success">
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                        <i class="material-icons">close</i>
                                    </button>
                                    <span>{{ session('status') }}</span>
                                </div>
                            </div>
                        </div>
                        @endif
                        <div class="form_container">
                            <form action="/action_page.php" id="tax-dec-form" {{$taxesInfo !== null ? '' : 'class=new'}}>
                                @csrf
                                <div class="form-group">
                                    <label for="taxdeclarationnumber">Tax Declaration No:</label>
                                    <input value="{{ $taxesInfo->tax_declaration_number ?? ''}}" type="text" class="form-control" name="tax_declaration_number" placeholder="Input Tax Declaration No..">
                                </div>
                                <div class="form-group">&nbsp</div>
                                <div class="form-group">
                                    <label for="current">Current RPT: </label>
                                    <input type="text" value="{{ $taxesInfo->current_rpt ?? ''}}" class="form-control" name="current_rpt" placeholder="Input Current RPT..">
                                </div>

                                <div class="form-group">
                                    <label for="years" class="bmd-label-static">Select Tax Declaration Year</label>
                                    <select id="DropDownList1" class="custom-select mr-sm-2" data-style="btn btn-secondary" name="year">

                                    </select>
                                </div>
                            </form>
                        </div>
                        <div class="clearfix">&nbsp;</div>
                        <div id="form-footer">
                            <button type="button" class="btn btn-primary" data-dismiss="modal" id="btn-save1">Save</button>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
Jin
  • 59
  • 1
  • 8
  • Welcome to SO. The simpler you can express your problem, the easier it is for others to help. There's quite a lot of HTML here but if I am understanding correctly there are really only relevant 2 lines, ``, is that correct? Sorry if I am misunderstanding. Are there actually multiple selects? Likewise for the JS - only the `//for dynamic year list` lines at the end seem relevant to the `select`? Is there a reason to dynamically add the select options with JS, instead of using PHP/Laravel? – Don't Panic Apr 17 '21 at 13:04
  • I think part of your question is about removing selected options once selected - [here's an example](https://stackoverflow.com/questions/1518216/jquery-remove-options-from-select), is that what you're after? – Don't Panic Apr 17 '21 at 13:07
  • Thanks for the example. This works for me. Yes, I want to remove selected options once selected. – Jin May 27 '21 at 10:40

0 Answers0