Doubt with checkbox value true and checked

Asked

Viewed 12,434 times

0

I have a form and to update I have this code snippet. I can’t understand what he means. Like, if the student is not active in the system he puts value="true"? Otherwise it also puts value="true" but checked="checked". What does that mean?

    <%if(aluno.getAtivo() == false){ %>
        <input type="checkbox" id="ativo" name="ativo" value="true"/><br>
    <%}else { %>
        <input type="checkbox" id="ativo" name="ativo" value="true" checked="checked"/><br>
    <%} %>      
    <input type="submit" value="Atualizar"> 

Follow full jsp page:

<%@page import="DAO.AlunoDAO"%>
<%@page import="MODEL.Aluno"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Atualizando aluno</title>
</head>
<body>
<% Aluno aluno = new AlunoDAO().find(Integer.parseInt(request.getParameter("id")));%>

<form action="alunoCONTROLLER" method="get">
    <input type="hidden" name="operacao" value="atualizar">
    <input type="hidden" name="id" value="<%=aluno.getId()%>">

    <label for="name">Nome:</label>
    <input type="text" id="nome" name="nome" value="<%=aluno.getNome()%>"/><br/>

    <label for="telefone">Telefone:</label>
    <input type="text" id="telefone" name="telefone" value="<%=aluno.getTelefone() %>"/><br/>

    <label for="email">E-mail:</label>
    <input type="text" id="email" name="email" value="<%=aluno.getEmail()%>" /><br>

    <label for="endereco">Endereço:</label>
    <input type="text" id="endereco" name="endereco" value="<%=aluno.getEndereco() %>"/><br>

    <label for="matricula">Matricula</label>
    <input type="text" id="matricula" name="matricula" value="<%=aluno.getMatricula() %>"/><br>

    <label for="idade">Idade</label>
    <input type="text" id="idade" name="idade" value="<%=aluno.getIdade() %>"/><br>

    <label for="ativo">Ativo:</label>

    <%if(aluno.getAtivo() == false){ %>
        <input type="checkbox" id="ativo" name="ativo" value="true"/><br>
    <%}else { %>
        <input type="checkbox" id="ativo" name="ativo" value="true" checked="checked"/><br>
    <%} %>      
    <input type="submit" value="Atualizar">     
</form>

1 answer

5


Your doubt is related to HTML and not to JSP.

Do not confuse value=true with checked.

Although it may be confusing, the attribute value is not defining whether the checkbox is checked or not in the user interface. This attribute is used to define what will be received on the server when an HTTP POST request is made (obviously the server only receives if the checkbox is checked).

The attribute that controls the checkbox state in the user interface is checked. There are two ways to use it:

<input type="checkbox" id="ativo" name="ativo" value="true" checked="checked"/>

or

<input type="checkbox" id="ativo" name="ativo" value="true" checked/>

Therefore, the code you posted is correct. When aluno.getAtivo() == true, you render a checkbox with the attribute checked and value=true. When aluno.getAtivo() == false, you render the checkbox without the checked attribute, but with value equally true. This is done, because if the user clicks the checkbox and sends, it will be received true on the server, which is consistent with your problem.

See another example below to better understand.

<form action="form_demonstracao" method="get">
  <input type="checkbox" name="veiculo" value="Bicicleta"> Eu tenho uma bicicleta<br>
  <input type="checkbox" name="veiculo" value="Carro" checked> Eu tenho um carro<br>
  <input type="submit" value="Submit">
</form>

If the above form is submitted as is, the server will receive the following:

veiculo=Carro

If the bicycle checkbox is checked then the following will be received:

veiculo=Carro&veiculo=Bicicleta
  • But the page is called refresh.jsp I don’t understand why I can’t chaamr de jsp...

  • I didn’t understand this: vehicle=Car=Bicycle

  • It wouldn’t be: vehicle=Bicycle?

  • 1

    Hello @Alinegonzaga. I don’t understand. I just said that your question is purely HTML, has no relation to JSP. From what I read of your question, you are doubtful about the meaning of the attributes of an input checkbox.

  • vehicle=Bicycle is what was received on the server side. It is only received if the checkbox is checked. Note that in the first example, only the checkbox car is marked, so get only vehicle=Car. After I comment that the checkbox Bike has been marked, then it is received from the server side vehicle=Car & vehicle=Bike, because the two checkboxes are marked. When I speak on the server side, it can be PHP, JAVA, C#, any WEB language.

  • This Bike is an attribute of some class in java?

  • @Alinegonzaga, so better not mark the answer as correct. I put the HTML tag and will certainly find someone who will give you another explanation. This is important in Sopt. Often knowledge is in several responses.

  • 1

    @Alinegonzaga, my main goal was to show you that the value of the value attribute is unrelated to the checkbox state (marked or unchecked). Value can be whatever you want it to be. It can even be the value of an attribute of a Java object. In your case, as you are talking about Active Student, the true is the most correct. In the example I posted, true no longer serves, because we are talking about car.

  • No, I’ll study what you wrote after right. Thanks to Julie.

  • In the case of your example there then the two would be marked?

  • In the example, only one is marked, the Car, because it has the attribute checked. Note that this way, the server receives only vehicle=Car. Soon after I mentioned that I clicked on the Bike. As now the two are marked, then the server gets vehicle=Vehicle=Bike.

  • 1

    AHHHHHHHHHHHHHHHHH Now I understand. Finally...

Show 7 more comments

Browser other questions tagged

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