Retrieve checkbox as checked with bank data?

3

The code below for data entry via checkbox , but do I need to retrieve the checkbox saved in the database for a possible 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> 
    
asked by anonymous 09.06.2014 / 02:47

1 answer

3

The variable $checkboxcheckado would be the return according to the orcamento_id by bringing an array with the items that are stored in the referring 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>

Html generated

<!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>

Note that% w and 3 are Id in your configuration, so they 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>

Note: I can not debug your code, so I do not know if it's perfect. My example can copy and paste into another file that works perfectly and can follow as a tutorial.

    
09.06.2014 / 03:02