Skip to content

Instantly share code, notes, and snippets.

@valdiney
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valdiney/9234395 to your computer and use it in GitHub Desktop.
Save valdiney/9234395 to your computer and use it in GitHub Desktop.
Meu exemplo!!
<?php
$link = mysql_connect("localhost", 'root','');
$bancoDeDados = mysql_select_db("projeto");
if($bancoDeDados) { /*...*/ }
else {
echo "Erro!";
}
?>
<section id="functions">
<h1>Cadastrando um Usuario no sistema</h1>
<form method="post" action="functions.php?funcao=gravar">
<article>
<table>
<tr>
<td><label for="nome">Nome:</label></td>
</tr>
<tr>
<td><input type="text" id="nome" name="nome" placeholder="Seu nome..."></td>
</tr>
<tr>
<td><label for="nascimento">Data de Nascimento:</label></td>
</tr>
<tr>
<td><input type="date" id="nascimento" name="nascimento"></td>
</tr>
<tr>
<td><label for="perfil">Perfil:</label></td>
</tr>
<tr>
<td>
<select name="perfil" id="perfil">
<option value="admin">Administrador</option>
<option value="common">Usuario comum</option>
</select>
</td>
</tr>
<tr>
<td><label for="estado">Estado:</label></td>
</tr>
<tr>
<td>
<select name="estado" id="estado">
<option>Qual o seu Estado?:</option>
<?php
$visualizarEstados = mysql_query("SELECT * FROM estado");
while( $linha = mysql_fetch_array( $visualizarEstados ) ) {
$nome = $linha['nome'];
?>
<option><?php echo $nome ?></option>
<?php
} // end while...
?>
</select>
</td>
</tr>
<tr>
<td><label for="estado">Cidade:</label></td>
</tr>
<tr>
<td>
<select name="cidade" id="cidade">
<option>Qual é a sua Cidade?:</option>
<?php
$visualizarCidades = mysql_query("SELECT * FROM cidade");
while( $linha = mysql_fetch_array( $visualizarCidades ) ) {
$nome = $linha['nome'];
?>
<option><?php echo $nome; ?></option>
<?php
} // end while...
?>
</select>
</td>
</tr>
<tr>
<td><button type="submit" id="enviar">Cadastrar</button></td>
</tr>
</table>
</form>
<article>
</section>
@renatosuero
Copy link

Imagina que tu tem uma página chamada cidades.php ,nela tu recebe um post $uf (formato SP,BA,etc...)

$uf = $_POST["uf"];

$option = "";
$sql = "select * from cidade where estado = {$uf}";
$query = mysql_error($sql) or die(mysql_error());
while($row = mysql_fetch_array($query))
{
$option .="<option value='{$row['id'}'>{$row['nome']}";
}
echo $option;

Atualizando teus selects

Qual o seu Estado?: <?php $visualizarEstados = mysql_query("SELECT * FROM estado"); while( $linha = mysql_fetch_array( $visualizarEstados ) ) { $nome = $linha['nome']; ?> <option value="<?php echo $linha['id'];?>"><?php echo $nome; ?></option> <?php } // end while... ?> </select> O select cidade ficará assim

o Jquery pra resolver é algo tipo
$("#estado").on("change",function(){
var uf = $("#estado").val();
$.post("cidades.php",{uf:uf},function(retorno){
$("#cidade").html(retorno);
});
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment