Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rajan001's avatar

How to disable submit button after submitting one time

Hi i want to make a function when user submits a form, that button should remain disabled after refreshing the page. I have tried this method but it only disable for the page when i refresh it can be accessible.

<script>
    $('.reverse-form').one('submit', function() {
    $(this).find('input[type="submit"]').attr('disabled', 'disabled');
    });
</script>
0 likes
12 replies
rajan001's avatar

Could you please tell me how to do that?

Sinnbeck's avatar

@rajan001 don't know what should trigger it. But say it's a variable named $disable

<button {{$disable ? 'disabled' : ''}} >Submit </button>
rajan001's avatar

i applied that method but i got variable not defined error. Here is my code please review it.

Controller

        public function reverse(Request $request)
        {
                $biniyojan = new Biniyojan();
                $biniyojan->date =  $request->date;
                $biniyojan->ab =  $request->ab;
                $biniyojan->school =  $request->school;
                $biniyojan->behora =  $request->behora;
                $biniyojan->save();
    
            if (!empty($request->source)) {
                for ($i = 0; $i < count((array)$request->source); $i++) {
                $biniyojan_details = new BiniyojanDetails();
                 
                $biniyojan_details['biniyojan_id'] = $biniyojan->id;
                $biniyojan_details->school = $request->school;
                $biniyojan_details->source = $request->source[$i];
                $biniyojan_details->kriyakalap = $request->kriyakalap[$i];
                $biniyojan_details->debit_credit = $request->debit_credit[$i];
                $biniyojan_details->debit_credit_type = $request->debit_credit_type[$i];
                $biniyojan_details->cash = $request->cash[$i];
                $biniyojan_details->save();
    
                    //Reducing Biniyojan balance from Budgets table
                    if($getBudgetValRev = bajet::where(['kriyakalap' => $biniyojan_details->kriyakalap])->first()){
                        $newBudgetRev = $getBudgetValRev->bujet + $biniyojan_details->cash;
                        bajet::where('kriyakalap',$biniyojan_details->kriyakalap)->update(['bujet'=>$newBudgetRev]);  
                    }else{
                        $newBudgetRev = 0;
                    }
                    
                    //Reducing Biniyojan balance from Budgets table
              
                }
            }
    
         return redirect()->back()->with('status', 'बिनियोजन उल्टियो !!!');
        }

Blade form


<form method="POST" action="{{ route('reverse') }}" class="reverse-form" style="width: 100%;">
                        @csrf
                        <div class="container-hide">
                            <div class="form-row col-x1-3">
                                <div class="form-group     col-md-2">
                                    <input type="date" placeholder="मिति" value="{{ $bini_details->date }}"
                                        name="date" class="form-control" id="date">
                                </div>
                                <div class="form-group col-md-2">
                                    <select id="inputState" placeholder="" name="ab"
                                        value="{{ $bini_details->ab }}" class="form-control">
                                        <option>२०७९-०८०</option>
                                        <option>२०८०-०८१</option>
                                    </select>
                                </div>



                                <div class="form-group     col-md-2">
                                    <select id="inputState" name="school" class="form-control">
                                        <option selected>{{ $bini_details->school }} </option>
                                        @foreach ($school_array as $sch)
                                            <option value="{{ $sch->name }}">{{ $sch->name }}</option>
                                        @endforeach
                                    </select>
                                </div>

                            </div>
                            @foreach ($bini_details->biniyojan_details as $details)
                                <div id="form-field">
                                    <div class="form-row col-x1-3">
                                        <div class="form-group col-md-2">
                                            <select id="source" name="source[]" class="form-control">
                                                <option selected>{{ $details->source }}</option>
                                                <option>केन्द्र</option>
                                                <option>प्रदेश</option>
                                                <option>स्थानीय</option>
                                                <option>अन्य</option>
                                            </select>
                                        </div>
                                        <div class="form-group col-md-2">
                                            <select class="kriyakalap form-control" onclick="display(event);"
                                                name="kriyakalap[]" class="form-control">
                                                <option selected>{{ $details->kriyakalap }}</option>

                                            </select>
                                        </div>

                                        <div class="form-group col-md-2">
                                            <select name="debit_credit[]" id="debit_credit" class="form-control">
                                                <option selected>{{ $details->debit_credit }}</option>
                                                <option>डेबिट</option>
                                                <option>क्रेडिट</option>
                                            </select>
                                        </div>
                                        <div class="form-group col-md-2">
                                            <select name="debit_credit_type[]" id="debit_credit_type"
                                                class="form-control">
                                                <option selected>{{ $details->debit_credit_type }}</option>
                                                <option>बजेट खर्च</option>
                                                <option>बैंक</option>
                                                <option>दायित्व</option>
                                                <option>बिबिध</option>
                                                <option>पेश्की खर्च</option>
                                                <option>पेश्की फर्छ्यौट</option>
                                                <option>समायोजन खाता</option>
                                            </select>
                                        </div>

                                        <div class="form-group col-md-2">
                                            <input type="text" placeholder="रकम" value="{{ $details->cash }}"
                                                name="cash[]" class="form-control" id="price">
                                        </div>



                                    </div>
                                </div>
                            @endforeach
                            <div class="form-row col-x1-3" style="justify-content:left ;">
                                <div class="form-group     col-md-2">
                                    <input type="text" placeholder="ब्यहोरा" value="{{ $bini_details->behora }}"
                                        name="behora" class="form-control" id="behora">
                                </div>
                            </div>
                        </div>

                        <div class="form-group col-md-2">
                            <input type="submit" {{$disable ? 'disabled' : ''}} id="submit" name="submit" value="विवरण उल्टानुहोस"
                                class="btn btn-primary btn-md">
                        </div>

                        @if (session('status'))
                            <div class="alert alert-success">
                                {{ session('status') }}
                            </div>
                        @endif
                    </form>


Sinnbeck's avatar

@rajan001 yeah. You need to define the variable in the controller and pass it to the view

rajan001's avatar

@Sinnbeck please explain in details about, In controller where should i define and how?

Sinnbeck's avatar

@rajan001 what determines of it was submitted already? I assume a record exists in some column? Show the controller method

rajan001's avatar

@Sinnbeck This is the controller


 public function reverse(Request $request)
        {
                $biniyojan = new Biniyojan();
                $biniyojan->date =  $request->date;
                $biniyojan->ab =  $request->ab;
                $biniyojan->school =  $request->school;
                $biniyojan->behora =  $request->behora;
                $biniyojan->save();
    
            if (!empty($request->source)) {
                for ($i = 0; $i < count((array)$request->source); $i++) {
                $biniyojan_details = new BiniyojanDetails();
                 
                $biniyojan_details['biniyojan_id'] = $biniyojan->id;
                $biniyojan_details->school = $request->school;
                $biniyojan_details->source = $request->source[$i];
                $biniyojan_details->kriyakalap = $request->kriyakalap[$i];
                $biniyojan_details->debit_credit = $request->debit_credit[$i];
                $biniyojan_details->debit_credit_type = $request->debit_credit_type[$i];
                $biniyojan_details->cash = $request->cash[$i];
                $biniyojan_details->save();
    
                    //Reducing Biniyojan balance from Budgets table
                    if($getBudgetValRev = bajet::where(['kriyakalap' => $biniyojan_details->kriyakalap])->first()){
                        $newBudgetRev = $getBudgetValRev->bujet + $biniyojan_details->cash;
                        bajet::where('kriyakalap',$biniyojan_details->kriyakalap)->update(['bujet'=>$newBudgetRev]);  
                    }else{
                        $newBudgetRev = 0;
                    }
                    
                    //Reducing Biniyojan balance from Budgets table
              
                }
            }
    
         return redirect()->back()->with('status', 'बिनियोजन उल्टियो !!!');
        }
Snapey's avatar

why even show them the form if they already submitted it?

CODE-AXION's avatar

Simple Hack:

<input type="submit" value="Place Your Order" class="pull-right btn btn-success" name="submitBtn" onclick="this.disabled=true;this.form.submit();" 

If you are not using ajax

<script>
$(document).ready(function() {
    $(document).on('submit', 'form', function() {
        $('button').attr('disabled', 'disabled');
    });
});

if you are using ajax

<button id="load" class="btn btn-primary" type="button" disabled>
							<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
							<span class="visually-hidden">Loading...</span>
							Sign in
</button>

<button type="button" id="register-button"  class="btn btn-primary" onclick="ajax_login();">Sign in</button>


beforeSend: function() {$('#load').show();$('#register-button').hide()},
NoLAstNamE's avatar

You may try this one:

<form class="important-form">
    ...

    <button type="submit" class="btn-prevent">Submit</button>
</form>

<script>
$(document).on("click", ".btn-prevent", function (e) {
    e.preventDefault();
    let importantForm = $('.important-form');

    if (! importantForm[0].checkValidity()) {
        importantForm[0].reportValidity()
    } else {
        $('.btn-prevent').attr('disabled', true);
    }
});
</script>

Please or to participate in this conversation.