Recover checkbox as checked with database data?

Asked

Viewed 2,660 times

3

The code below for entering data via checkbox, but I need to get the checkbox recorded in the bank for any update?

<?php   include("includes/config.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<div class="header"><img src="imagens/logo.png"style="width:300px;height:150px;"></div>
<div class="nav"><?php include("menu.php") ?></div>
<body>
<div class="corpo">

<form name="editar" enctype="multipart/form-data" method="post">

<?php
$id = $_GET['id'];
$seleciona = mysql_query("
SELECT orcamento.*, clientes.*, veiculos.* FROM orcamento
INNER JOIN clientes ON clientes.id = orcamento.cliente_id
INNER JOIN veiculos ON veiculos.id = orcamento.cliente_id

WHERE orcamento.id = '$id'" ) or die (mysql_error());


if($seleciona == ''){
    echo 'Erro';

}else{
        while($res_id = $row = mysql_fetch_assoc($seleciona)){

?>

<br /><br />
<fieldset><legend>Dados do Cliente</legend>
<br />
Número do Orçamento (ID): <input type="text" name="id" value="<?php echo $id?>" size="6" />
Data: <input type="text" name="nome" disabled="disabled" value="<?php echo $res_id = $row['data'];?>" /><br /><br />
Nome do Cliente: <input type="text" name="nome" disabled="disabled" value="<?php echo $res_id = $row['nome'];?>" size="75" />
<br /><br />
Veiculo: <input type="text" name="marca" disabled="disabled" value="<?php echo $res_id = $row['marca'];?>" />
Modelo: <input type="text" name="modelo" disabled="disabled" value="<?php echo $res_id = $row['modelo'];?>" />
Placa: <input type="text" name="placa" disabled="disabled" value="<?php echo $res_id = $row['placa'];?>" />
<br />
</fieldset>
<br />

<fieldset><legend>Condições</legend>
<br />
Condição de Pagamento: <input type="text" name="cond_pagamento" value="<?php echo $res_id = $row['cond_pagamento'];?>" size="10" />
Data Inicio: <input type="text" name="datainicio" value="<?php echo $res_id = $row['datainicio'];?>" />
Data Témino: <input type="text" name="datatermino" value="<?php echo $res_id = $row['datatermino'];?>" /><br /><br />
Validade: <input type="text" name="validade" value="<?php echo $res_id = $row['validadeorcamento'];?>" />
Status: <input type="text" name="status" value="<?php echo $res_id = $row['status'];?>" />
<br /><br />
</fieldset>
<br />
<?php
}}
?>

<fieldset><legend>Serviços</legend>
<br /><br />
<?php

$id = $_GET['id'];
$seleciona2 = mysql_query("
SELECT orcamento.*, orcamento.data, DATE_FORMAT(data, '%d/%m/%Y') AS data_orc, detalhe_orcamento.*, mao_obra.* FROM orcamento 
INNER JOIN detalhe_orcamento ON detalhe_orcamento.orcamento_id = orcamento.id
INNER JOIN mao_obra ON mao_obra.id = detalhe_orcamento.mao_obra_id
WHERE orcamento.id = '$id'" ) or die (mysql_error());

if($seleciona2 == ''){
    echo 'Erro';

}else{
echo'aqui que tenho que colocar o form com os checkbox, e aqueles que estão gravados no banco vir como checked';
}
?>
</fieldset>
</form>
<br /><br />

</div>
</body>
</html> 
  • If you are going to rescue all the right workmanship! and how you will rescue the orcamento_id since in your form there is no value?

  • in fact Harry this code is a part of the form the part that resgato orcamento_id already managed to rescue as other data, although orcamento_id and other data as customer data, vehicle data will only be for visualization, this data is edited in other Forms...

  • I’ll put an example like another answer who knows you understand, because on this screen you can not do ok!?

1 answer

3


The variable $checkboxcheckado would be the return as the orcamento_id bringing an array with the items that are recorded in the table of your database.

Example:

<?php
    $checkbox = array(array("id" => 1, "value" => 1.50),
                      array("id" => 2, "value" => 2.50),
                      array("id" => 3, "value" => 3.50));

    $checkboxcheckado = array(1,3);

?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>checkbox</title>
</head>
<body>
    <form action="roto.php" method="post" enctype="multipart/form-data" name="form1">
    <?php foreach($checkbox as $value): ?>
        <input type="checkbox" name="id[]"  <?php echo in_array((int)$value['id'],$checkboxcheckado) ?' checked="checked"':'';?>     value="<?php echo $value['id'];?>">
        <input type="hidden"   name="valores_<?php echo $value['id'];?>" value="<?php echo $value['value'];?>">
    <?php endforeach; ?>
        <button>Enviar</button>
    </form>
    <?php
        if (isset($_POST['id'])):
            foreach($_POST['id'] as $key => $value):
                echo $value . ' - ' . $_POST['valores_'.$value];
                echo '<br>';
            endforeach;
        endif;
    ?>
</body>
</html>

Generated html

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>checkbox</title>
</head>
<body>
    <form action="roto.php" method="post" enctype="multipart/form-data" name="form1">
            <input type="checkbox" name="id[]" checked="checked" value="1">
        <input type="hidden"   name="valores_1" value="1.5">
            <input type="checkbox" name="id[]"  value="2">
        <input type="hidden"   name="valores_2" value="2.5">
            <input type="checkbox" name="id[]"  checked="checked" value="3">
        <input type="hidden"   name="valores_3" value="3.5">
            <button>Enviar</button>
    </form>
    </body>
</html>

Realize that the Id 1 and 3 are checked="checked" in its configuration, then, are checked.

In your code:

<?php   include("includes/config.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<div class="header"><img src="imagens/logo.png"style="width:300px;height:150px;"></div>
<div class="nav"><?php include("menu.php") ?></div>
<body>
<div class="corpo">

<form name="editar" enctype="multipart/form-data" method="post">

<?php
    $id = $_GET['id'];
    $seleciona = mysql_query("
    SELECT orcamento.*, clientes.*, veiculos.* FROM orcamento
    INNER JOIN clientes ON clientes.id = orcamento.cliente_id
    INNER JOIN veiculos ON veiculos.id = orcamento.cliente_id

    WHERE orcamento.id = '$id'" ) or die (mysql_error());


    if($seleciona == ''){
        echo 'Erro';

    }else{
            while($res_id = $row = mysql_fetch_assoc($seleciona)){

?>

    <br /><br />
    <fieldset><legend>Dados do Cliente</legend>
    <br />
    Número do Orçamento (ID): <input type="text" name="id" value="<?php echo $id?>" size="6" />
    Data: <input type="text" name="nome" disabled="disabled" value="<?php echo $res_id = $row['data'];?>" /><br /><br />
    Nome do Cliente: <input type="text" name="nome" disabled="disabled" value="<?php echo $res_id = $row['nome'];?>" size="75" />
    <br /><br />
    Veiculo: <input type="text" name="marca" disabled="disabled" value="<?php echo $res_id = $row['marca'];?>" />
    Modelo: <input type="text" name="modelo" disabled="disabled" value="<?php echo $res_id = $row['modelo'];?>" />
    Placa: <input type="text" name="placa" disabled="disabled" value="<?php echo $res_id = $row['placa'];?>" />
    <br />
    </fieldset>
    <br />

    <fieldset><legend>Condições</legend>
    <br />
    Condição de Pagamento: <input type="text" name="cond_pagamento" value="<?php echo $res_id = $row['cond_pagamento'];?>" size="10" />
    Data Inicio: <input type="text" name="datainicio" value="<?php echo $res_id = $row['datainicio'];?>" />
    Data Témino: <input type="text" name="datatermino" value="<?php echo $res_id = $row['datatermino'];?>" /><br /><br />
    Validade: <input type="text" name="validade" value="<?php echo $res_id = $row['validadeorcamento'];?>" />
    Status: <input type="text" name="status" value="<?php echo $res_id = $row['status'];?>" />
    <br /><br />
    </fieldset>
    <br />
<?php
}}
?>

<fieldset><legend>Serviços</legend>
<br /><br />
<?php

    $id = $_GET['id'];
    $seleciona2 = mysql_query("
    SELECT orcamento.*, orcamento.data, DATE_FORMAT(data, '%d/%m/%Y') AS data_orc, detalhe_orcamento.*, mao_obra.* FROM orcamento 
    INNER JOIN detalhe_orcamento ON detalhe_orcamento.orcamento_id = orcamento.id
    INNER JOIN mao_obra ON mao_obra.id = detalhe_orcamento.mao_obra_id
    WHERE orcamento.id = '$id'" ) or die (mysql_error());

    if($seleciona2 == ''){
        echo 'Erro';

    }else{
        $checkboxcheckado = array();
        while ($cp = mysql_fetch_assoc($seleciona2)){
         $checkboxcheckado[] = $cp['mao_obra_id'];
        }
?>
<form id="form1" action="" enctype=" multipart/form-data" method="post">
    <?php
            // consulta do select de Serviços
            $selec = "SELECT * FROM mao_obra";
            $exec = mysql_query($selec) or die(mysql_error());
        while($dados = mysql_fetch_assoc($exec)){
            $valor_id        =  $dados['id'];
            $valor_mao_obra  =  $dados['mao_obra'];
            $valor_preco     =  $dados['preco'];

    ?>
        <input style="margin-left:30px" name="mao_obra_id[]" <?php echo in_array((int)$valor_id,$checkboxcheckado) ?' checked="checked"':'';?> type="checkbox" value="<?php echo $valor_id ?>"/>
        &nbsp;&nbsp;<?php echo $valor_mao_obra ?>
        <input type="hidden" name="preco<?php echo $valor_id ?>" value="<?php echo $valor_preco ?>" />
        &nbsp;&nbsp;<?php echo $valor_preco ?>
    <?php 
        } 
    ?>
        <input type="text" name="orcamento_id" />
        <input type="submit" name="enviar" value="Adicionar Orçamento" />
</form>
<?php } ?>
</fieldset>
</form>
<br />
<br />
</div>
</body>
</html>

Obs: I can’t debug your code, so I don’t know if it’s perfect. My example can copy and paste into another file that works perfectly and can follow as tutorial.

  • Harry edited the code up there, that’s the form I’m using put an echo in the part where I’m having difficulties

  • @user3715576 is more or less like this!

  • 1

    Dude! you’re a master yourself, I was going to break my head a lot and I don’t know if I could do it, I’m already studying the files of "help" although it was another class you gave me, thank you very much, it worked perfect!

Browser other questions tagged

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