Send checkbox value not selected by form

Asked

Viewed 557 times

0

I have an html form, and in it I have an input checkbox, I need to send the value of that checkbox independent if it is selected or not, if it has selected send 1 if it has not sent 0. So when it is not selected it does not send anything, No argument, just some argument. the url when I mark the checkbox. Someone knows how to send the value of the checkbox not marked by the url?

<form method="get" action="paginadestino">
  <table class="tabelas" id="tabelaUsuarios2" bordercolor=green align="center" border="1px solid #ddd">
        <tr id="linhaParaClone" style="display:none">
            <td>
                <input type="text" id="usuario" name="usuario" style="width:120px" maxlength="20" required>
            </td>
            <td>
                <input type="text" name="senha" style="width:95px" maxlength="20" required>
            </td>
            <td>
                <input type="text" name="tag" style="width:80px" maxlength="8" required>
            </td>
            <td style="display:none">
                <input type="text" name="permissoes" style="width:80px" value="0-0-0-0-0" required>
            </td>
            <td>
                <input type="time" name="hrentrada" style="width:80px" maxlength="4" required>
            </td>
            <td>
                <input type="time" name="hrsaida" style="width:80px" maxlength="4" required>
            </td>
            <td>
                <input type="checkbox" name="adm">
            </td>
            <td>
                <button type="button" name="mais" onclick="clonarLinha()">+</button>
                <button type="button" name="menos" onclick="removeLinha(this)">-</button>
            </td>
            <td></td>
       </tr>
  </table>
</form>
  • 1

    Could you explain it better? Is the value of the checkbox always 1? If it is always the same value, it makes no sense to send it in Ubmit, just put the value 1 for the variable that represents the checkbox. Or even more, it doesn’t even make sense to have checkbox on the form.

1 answer

1


What is the need to send a variable without data, if value does not arrive you already know that was not clicked. It’s just in your back end to check if any answers are coming. PHP example:

<?php    
if(isset($_GET['adm'])){
    $admin = "Sim";
}else{
    $admin = "Não";
}
?>

EDIT 1: Example Addition but that doesn’t have much logic to exist.

$("#enviar").click(function(){
		if($("#adm").is(':checked')){
			$("#valor_checkbox").val("Checado");
			$("#checkbox_show").val("Checado");
		}else{
			$("#valor_checkbox").val("DesChecado");
			$("#checkbox_show").val("DesChecado");			
		}
	});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="adm" name="adm">
<input type="hidden" id="valor_checkbox" name="valor_checkbox">
<button id="enviar">Enviar</button></br></br>
<label>Input Demo</label>
<input type="text" id="checkbox_show" name="checkbox_show">

Explanation:

You can use JavaScript to check if the checkbox is checked when you submit the form. And use the if() to fill in a hidden field(<input type='hidden'>), to save a string that represents when the checkbox is checked or not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.