Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal, estou recuperando os dados gravados no BD (MySQL) para poder editá-los, todos os campos estão retornando os valores certinhos, porém não consigo recuperar os checkbox que foram marcados. Alguém pode me dar essa forcinha?
Segue abaixo o código da minha página:
<?php
include "sql.php";
if(isset($_POST['done'])){
$id = $_POST['id'];
$nome = $_POST['nome'];
$data_cadastro = $_POST['data_cadastro'];
$n_cadastro = $_POST['n_cadastro'];
$rg = $_POST['rg'];
$cpf = $_POST['cpf'];
$data_niver = $_POST['data_niver'];
$endereco = $_POST['endereco'];
$n = $_POST['n'];
$bairro = $_POST['bairro'];
$cidade = $_POST['cidade'];
$cep = $_POST['cep'];
$telefone = $_POST['telefone'];
$celular = $_POST['celular'];
$email = $_POST['email'];
$bra = $_POST['bra'];
$situacao = $_POST['situacao'];
$obs = $_POST['obs'];
$aero = $_POST['aero'];
$sql = mysql_query("UPDATE tbl_cadastro SET nome='$nome', data_cadastro='$data_cadastro', n_cadastro='$n_cadastro', rg='$rg', cpf='$cpf', data_niver='$data_niver', endereco='$endereco', n='$n', bairro='$bairro', cidade='$cidade', cep='$cep', telefone='$telefone', celular='$celular', email='$email', bra='$bra', situacao='$situacao', obs='$obs', aero='$aero' WHERE id='$id'")or die(mysql_error());
$linha = mysql_affected_rows();
if($linha == 1){
$erro = "Dados alterados com sucesso!";
} else{
$erro = "Não foi possivel alterar os dados";
}
}
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM tbl_cadastro WHERE id = '$id'");
$nome = @mysql_result($sql, 0, "nome");
$data_cadastro = @mysql_result($sql, 0, "data_cadastro");
$n_cadastro = @mysql_result($sql, 0, "n_cadastro");
$rg = @mysql_result($sql, 0, "rg");
$cpf = @mysql_result($sql, 0, "cpf");
$data_niver = @mysql_result($sql, 0, "data_niver");
$endereco = @mysql_result($sql, 0, "endereco");
$n = @mysql_result($sql, 0, "n");
$bairro = @mysql_result($sql, 0, "bairro");
$cidade = @mysql_result($sql, 0, "cidade");
$cep = @mysql_result($sql, 0, "cep");
$telefone = @mysql_result($sql, 0, "telefone");
$celular = @mysql_result($sql, 0, "celular");
$email = @mysql_result($sql, 0, "email");
$bra = @mysql_result($sql, 0, "bra");
$situacao = @mysql_result($sql, 0, "situacao");
$obs = @mysql_result($sql, 0, "obs");
$aero = @mysql_result($sql, 0, "aero");
?>
<style type="text/css">
.campo{
width:400px;
}
.style2 {font-family: verdana}
.style8 {font-size: 12px}
.style9 {font-size: 11px}
</style>
<form name="form1" action="atualagenda.php" method="POST" style="padding-top:40px;">
<?php
if(isset($erro)){
print '<div style="width:80%; background:#ff6600; color:#fff; padding: 5px 0px 5px 0px; text-align:center; margin: 0 auto;">'.$erro.'</div>';
}
?>
<table border="0" width="80%" bgcolor="#f0f0f0" style="border:1px solid #ccc; margin:0 auto; position:relative;">
<thead>
<tr>
<th colspan="2">.:: Atualizar Ficha de Cadastro ::.</th>
</tr>
<tr>
<td width="14%"><span class="style8 style2"><strong><strong>Data do Cadastro</strong></strong></span></td>
<td width="86%" class="style9 style2 style9">
<label>
<input name="data_cadastro" type="text" id="data_cadastro" value="<?php echo $data_cadastro; ?>" size="15" maxlength="10" />
<em>(Ex.: 00/00/0000)</em></label>
</td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Nº Cadastro</strong></strong></span></td>
<td><input name="n_cadastro" type="text" class="style2 style9" id="n_cadastro" value="<?php echo $n_cadastro; ?>" size="16" maxlength="16" /></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Nome</strong></strong></span></td>
<td><span class="style9 style2 style9">
<label>
<input name="nome" type="text" id="nome" value="<?php echo $nome; ?>" size="70" maxlength="70" />
</label>
</span></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>RG</strong></strong></span></td>
<td class="style9 style2 style9">
<input name="rg" type="text" id="rg" value="<?php echo $rg; ?>" size="18" maxlength="18" />
<em>(Ex.: 00.000.000-0)</em></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>CPF</strong></strong></span></td>
<td class="style9 style2 style9">
<input name="cpf" type="text" id="cpf" value="<?php echo $cpf; ?>" size="18" maxlength="18" />
<em>(Ex.: 000.000.000-00)</em></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Data de Aniversário</strong></strong></span></td>
<td class="style9 style2 style9">
<label>
<input name="data_niver" type="text" id="data_niver" value="<?php echo $data_niver; ?>" size="15" maxlength="10" />
<em>(Ex.: 00/00/0000)</em></label>
</td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Endereço</strong></strong></span></td>
<td><span class="style9 style2 style9">
<label>
<input name="endereco" type="text" id="endereco" value="<?php echo $endereco; ?>" size="90" maxlength="90" />
</label>
</span></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>N</strong></strong></span></td>
<td><span class="style9 style2 style9">
<label>
<input name="n" type="text" id="n" value="<?php echo $n; ?>" size="10" maxlength="10" />
</label>
</span></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Bairro</strong></strong></span></td>
<td><span class="style9 style2 style9">
<label>
<input name="bairro" type="text" id="bairro" value="<?php echo $bairro; ?>" size="40" maxlength="40" />
</label>
</span></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Cidade</strong></strong></span></td>
<td><span class="style9 style2 style9">
<label>
<input name="cidade" type="text" id="cidade" value="<?php echo $cidade; ?>" size="35" maxlength="35" />
</label>
</span></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>CEP</strong></strong></span></td>
<td class="style9 style2 style9">
<label>
<input name="cep" type="text" id="cep" value="<?php echo $cep; ?>" size="10" maxlength="10" />
<em>(Ex.: 000000-000)</em></label> </td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Telefone</strong></strong></span></td>
<td class="style9 style2 style9">
<label>
<input name="telefone" type="text" id="telefone" value="<?php echo $telefone; ?>" size="15" maxlength="15" />
<em>(Ex.: (00) 0000-0000)</em></label>
</td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Celular</strong></strong></span></td>
<td class="style9 style2 style9">
<input name="celular" type="text" id="celular" value="<?php echo $celular; ?>" size="15" maxlength="15" />
<em>(Ex.: (00) 0000-0000)</em></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>E-Mail</strong></strong></span></td>
<td><span class="style9 style2 style9">
<label>
<input name="email" type="text" id="email" value="<?php echo $email; ?>" size="85" maxlength="85" />
</label>
</span></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>BRA</strong></strong></span></td>
<td><input name="bra" type="text" class="style2 style9" id="bra" value="<?php echo $bra; ?>" size="35" maxlength="35" /></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Modalidades</strong></strong></span></td>
<td><span class="style9 style2 style9">
<label> <strong>
<input name="aero" type="checkbox" id="aero" value="<?php echo $aero; ?>" />
Aeromodelismo
<input name="heli" type="checkbox" id="heli" value="on" />
Helimodelismo
<input name="vcc" type="checkbox" id="vcc" value="on" />
V.C.C.
<input name="auto" type="checkbox" id="auto" value="on" />
Automodelismo</strong></label>
</span></td>
</tr>
<tr>
<td><span class="style8 style2"><strong><strong>Situação Financeira</strong></strong></span></td>
<td><input name="situacao" type="text" class="style2 style9" id="situacao" value="<?php echo $situacao; ?>" size="90" maxlength="90" /></td>
</tr>
<tr>
<td><p class="style2 style8"><strong>Observações</strong></p>
<p class="style2 style8"> </p>
<p class="style2 style8"> </p>
<p class="style2 style8"> </p>
<p class="style2 style8"> </p></td>
<td><span class="style2 style9">
<label>
<textarea name="obs" id="obs" cols="90" rows="10"><?php echo $obs; ?></textarea>
</label>
</span></td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Atualizar Cadastro" />
<input type="button" name="button" id="button" onclick="javascript:location.href='edicao.php';" value="Cancelar" />
<input type="button" name="button" id="button" onclick="javascript:location.href='edicao.php';" value="Voltar" />
<input type="hidden" name="done" value="" /><input name="id" type="hidden" value="<?php echo $id; ?>" /></td>
</tr>
</tbody>
</table>
</form>
Obrigado pessoal!
Amigo @dan_visualdm não faça flood no fórum. Se ninguém o respondeu até o momento
é porque não sabemos a resposta ou não tivemos tempo de fazê-lo. Lembre-se que está
é uma comunidade voluntária onde todos contruibuimos uns com os outros sempre que podemos. Ninguém tem tal obrigação.
Leia http://forum.imasters.com.br/topic/375800-orientacoes-para-uma-boa-participacao/
Quanto ao checkbox, acredito eu que ele salve algum valor em seu banco de dados e sendo assim você quer trazer a checkbox marcada no momento da edição certo? Se este for o problema basta verificar se o $aero possui valor e usar o html checked.
Ex:
<?php $check = (!empty($aero)) ? 'checked="checked"' : ''; ?>
<input name="aero" type="checkbox" id="aero" <?php echo $check; ?> />Quando está ativado(marcado) o valor dele é on?
Se sim, basta na hora de pegar os dados do bd fazer um if ternário, algo assim:
$recordaudio = ($ln -> record_audio == 1 ? 'checked="checked"':'');
e no input
<input type="checkbox" name="recordaudio" value="1" <?php echo $recordaudio;?> >
Para cada campo checkbox que possa ter, terá que ver se o valor dele é 'on' no seu caso e atribuir o valor checked=checked,
caso o valor seja diferente, ele retorna nulo, ou seja no iinput de nada vai alterar um valor nulo('');
$ln -> record_audio é o campo do banco de dados,
== 1 no meu caso é 0 e 1 os valores,
no seu deve ser on,off, suponho.
só alterar,
Qualquer dúvida só perguntar /applications/core/interface/imageproxy/imageproxy.php?img=http://forum.imasters.com.br/public/style_emoticons/default/rolleyes.gif&key=059f4015935c07d78cff7b710ea971b8c55160eca53a3829eba1c5cfa965ff83" alt="rolleyes.gif" />
Aproveitando o tema, tá me dando um nó em um negócio aqui...aff...
Vejam o esquema:
<form name="form1" action="<?php $_SERVER['REQUEST_URI']; ?>" method="post">
<div>
<a href="print.php?id=<?php echo $_POST["idlist"]; ?>" target="_blank" title="Visualizar">
<img src="btn/btnvis.png" width="88" height="24" alt="" border="0" />
</a>
</div>
...início de um loop...
<input onclick="contador(this);" type="checkbox" name="idlist[]" id="idlist<?php echo $cont;?>" value="<?php echo $lin['id']; ?>" />
...outros campos...
...final deste loop...
</form>
Gostaria de editar direto pelo link passando a ID dentro do form, sem precisar submeter, tem como?
E Ajax? Alguém com alguma dica?
Grato a comunidade.
@ademirs O que você quer é feito com javascript com certeza, não entendi muito bem o que é,
mas explique melhor sua dúvida em um NOVO tópico na sessão de javascript do fórum ok?
Estou com esse mesmo problema, vou acompanha o tópico!