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

MyirLik's avatar

Send if is checked

How can I write a function in javascript so that when I checked the checkbox the form will send in case an error does not occur and is not sent?



      <div class="col-md-4 col-xs-6" style="width: 100%;"><input 

        style="background-color: #FF0000 !important;
    padding: 5px 15px !important;  margin-top: 20px;
    float: right; 
    border-radius: 10px;
    border: none;
    font-size: 15px !Important;
    box-shadow: 2px 2px 2px #acacac;
    color: #fff !important;" 
    name="send" id="forms-send" type="submit" value="<?=$this->lang->line('form.send'); ?>" ></div>
    </div>
    <input type="checkbox" id="myCheck" onclick="isChecked()">
0 likes
3 replies
Tray2's avatar

Something like this should work.

html

<input id="submit" type="checkbox" onclick="submit()">

js

function submit() {
	el =	document.querySelector('#submit');
	if (el.checked) {
  	document.getElementById("myForm").submit(); 
  }
}

You need to give the form you want to submit an id.

MyirLik's avatar

This will automatically send the form when I use submit, I would like it to show me an error when I click submit and it is not checked

Tray2's avatar

Something like this should work

<button onclick="submit()">Submit</button>
<span id="message"></span>
function submit() {
	el =	document.querySelector('#submit');
	if (el.checked) {
  		document.getElementById("myForm").submit(); 
	} else {
		message = document.querySelector('#message').
		message.innerHTML = 'Dude you need to check the checkbox';
	}
}

Please or to participate in this conversation.