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

bellaratmelia's avatar

How to disable button from submitting form?

Hi Guys, I have a form with two buttons.. I want to use one to submit the form and the other for another purpose. But right now both buttons are submitting the form... Here is my code for a better picture.

This is the button that i am planning to stop submitting the form with.

input id="btnCombine" class="btn btn-primary" type="submit" value="Combine" style="margin-bottom:10px;

This is the method i am using to disable submit. $("btnCombine")on("click",(function(event){ alert("w"); e.preventDeafult(); })

I am not sure if laravel have a way to prevent the button from submitting the form. If there is please do let me know.

Thanks in advanced!

0 likes
4 replies
RachidLaasri's avatar
Level 41

change the type from submit to button, so you don't need to preventDefault.

type="submit"

to

type="button"
4 likes
bashy's avatar

Not directly related but you can also manage two submit buttons by using different name attributes on them. Then you can pick them up in PHP

if (Input::has('submit1'))
{
    // This was used - <button type="submit" name="submit1">Submit1</button>
}

if (Input::has('submit2'))
{
    // This was used - <button type="submit" name="submit2">Submit2</button>
}
aixguru's avatar

As a side note, this might just be a typo from where you copied in your javascript, but you have

$("btnCombine")on("click",(function(event){ alert("w"); e.preventDeafult(); })

You use event as the argument in the callback, but then e.preventDefault(). One of those needs to be changed to match the other.

aurawindsurfing's avatar

Just on a side note here as well. I faced the same issue on an input where I was using vuejs. Turns out in vue it is so simple it hurts... have a look:

my code that submits on enter:

<input
        ref="autocomplete"
        name="location"
        type="text"
        required="required"
        :class="classname"
        :id="id"
        :placeholder="placeholder"
        v-model="autocompleteText"
        @focus="onFocus()"
        @blur="onBlur()"
        @change="onChange"
        @keypress="onKeyPress"
     >

all I needed to do is change that line:

@keypress.enter.prevent="onKeyPress"

much better then that:

$("btnCombine")on("click",(function(event){ alert("w"); e.preventDeafult(); })

You can find reference here: https://vuejs.org/v2/guide/events.html

Please or to participate in this conversation.