Cara, não é ‘algo comum’, mas achei interessante essa dúvida, e resolvi montar.
Em funcionamento:
http://www.wbruno.co…form-cidade.php
config.inc.php
-
<?php
-
-
-
//Evitando cache de arquivo
-
-
-
-
-
-
///*
-
//*/
init.inc.php
-
<?php
-
-
else
-
require_once BASE_PATH.‘config.inc.php’;
-
-
class Db
-
{
-
-
private function __construct(){}
-
{
-
return new mysqli( SERVIDOR, USUARIO, SENHA, BANCO );
-
}
-
{
-
if ( self::$instancia == null )
-
self::$instancia = self::conecta();
-
return self::$instancia;
-
}
-
}
-
function queryCidade( $where=null )
-
{
-
$mysqli = Db::pegaConexao();//pegando uma instância da conexão
-
$sql = "SELECT `id` AS `value`, `nome` AS `label` FROM `cidade` {$where}";
-
-
return $mysqli->query( $sql );
-
}
-
function comboCidade( $where=null )
-
{
-
$query = queryCidade( $where );
-
-
return montaSelect( $query );
-
}
-
function montaSelect( $query )
-
{
-
if( $query->num_rows )
-
while( $dados = $query->fetch_object() )
-
$opt .= ‘<option value="’.$dados->value.‘">’.$dados->label.‘</option>’."\n";
-
else
-
$opt = ‘<option value="0">Nenhum registro</option>’;
-
-
return $opt;
-
}
-
function cadastraCidade( $nome )
-
{
-
$mysqli = Db::pegaConexao();//pegando uma instância da conexão
-
$sql = "INSERT INTO `cidade`
-
(`id`, `nome`)
-
VALUES (NULL, ‘{$nome}’)";
-
-
return $mysqli->query( $sql );
-
}
-
function isPost()
-
{
-
if( $_SERVER[‘REQUEST_METHOD’] == ‘POST’ )
-
return true;
-
}
-
function getPost( $campo )
-
{
-
}
-
function getGet( $campo )
-
{
-
}
-
function filter( $var ){
-
//$str = mysql_real_escape_string( $var );
-
else
-
$str = $var;
-
return $str;
-
}
-
function jsonCidade( $id=null )
-
{
-
$where = ( $id ) ? " WHERE `id` = {$id} " : ”;
-
-
$where .= ‘ ORDER BY `nome`’;
-
$query = queryCidade( $where );
-
-
$json = ‘ [';
-
if( $query->num_rows > 0 )
-
while( $dados = $query->fetch_object() )
-
$json .= '{"nome":"'.$dados->label.'","id":"'.$dados->value.'"}, ';
-
else
-
$json .= '{"nome": "Não Encontrado"}';
-
-
$json .= ']‘;
-
-
return $json;
-
}
-
function existeCidade( $cidade )
-
{
-
$query = queryCidade( " WHERE nome = ‘{$city}’" );
-
if( $query->num_rows )
-
return $city.‘ já existe!’;
-
else
-
return cadastraCidade( $cidade );
-
}
-
retorno.php
-
<?php
-
include ‘init.inc.php’;
-
-
{
-
}
-
if( getPost(‘cidade’) )
-
{
-
}
form-cidade.php
-
<?php include ‘init.inc.php’; ?>
-
<html>
-
<head>
-
<title>Formulário de Cadastro</title>
-
<meta name="author" content="William Bruno" />
-
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
-
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
-
<script type="text/javascript">
-
$(document).ready(function(){
-
$("a[rel*='popup']").click(function( e ){
-
e.preventDefault();
-
-
window.open( $(this).attr(‘href’), $(this).attr(‘href’), param[1] );
-
});
-
});
-
function carregarCombo( param, combo )
-
{
-
$("img[src='ico-loading.gif']").show();
-
$.getJSON(
-
‘retorno.php’,
-
{param: param},
-
function(data){
-
-
resetaCombo( combo );
-
-
option[i] = document.createElement(‘option’);
-
$( option[i] ).attr( {value : obj.id} );
-
$( option[i] ).append( obj.nome );
-
-
$("select[name='"+combo+"']").append( option[i] );
-
});
-
$("img[src='ico-loading.gif']").hide();
-
});
-
}
-
function resetaCombo( el )
-
{
-
var option = document.createElement(‘option’);
-
$( option ).attr( {value : ‘0′} );
-
$( option ).append( ” );
-
$("select[name='"+el+"']").append( option );
-
}
-
</script>
-
</head>
-
<body>
-
<form action="" method="post">
-
<label>Cidade: <select name="cidade">
-
<option value=""></option>
-
</select></label>
-
<a href="cadastrar-cidade.php" rel="popup; width=300px, height=150px, top=150px, left=300px;">Cadastrar Cidade</a>
-
-
<img src="ico-loading.gif" alt="" style="display: none;" />
-
</form>
-
</body>
-
</html>
cadastrar-cidade.php
-
<?php
-
include ‘init.inc.php’;
-
$msg = null;
-
if( isPost() )
-
$msg = existeCidade( getPost(‘cidade’) ) ? ‘Cadastro efetuado’ : ‘Ocorreu um erro’;
-
?>
-
<html>
-
<head>
-
<title>Formulário de Cadastro</title>
-
<meta name="author" content="William Bruno" />
-
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
-
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
-
<script type="text/javascript">
-
$(document).ready(function(){
-
$("a.voltar").hide();
-
$("input[name='fechar']").click(function(){
-
fechar_pop();
-
});
-
$("input[name='enviar']").click(function(){
-
$("img[src='ico-loading.gif']").show();
-
$( this ).attr({ disabled: ‘disabled’});
-
$("input[name='cidade']").attr({ disabled: ‘disabled’});
-
$.ajax({
-
type: ‘POST’,
-
url: ‘retorno.php’,
-
data: ‘cidade=’+$("input[name='cidade']").val(),
-
success: function( data ){
-
if( data!=1 )
-
{
-
$("input[name='enviar'], input[name='cidade']").attr({ disabled: ”});
-
$("input[name='cidade']").val(”);
-
$("input[name='cidade']").focus();
-
-
$(‘#retorno’).html( data );
-
}
-
else
-
fechar_pop();
-
},
-
complete: function( xhr, status ){
-
$("img[src='ico-loading.gif']").hide();
-
}
-
});
-
return false;
-
});
-
});
-
function fechar_pop()
-
{
-
window.close();
-
if ( window.opener && !window.opener.closed )
-
window.opener.carregarCombo( ”, ‘cidade’ );
-
}
-
window.onbeforeunload = fechar_pop;
-
</script>
-
</head>
-
<body>
-
<?php if( !$msg ){ ?>
-
<form action="" method="post">
-
<label>Cidade: <input type="text" name="cidade" /></label>
-
<label><input type="submit" name="enviar" value="Enviar" /></label>
-
<img src="ico-loading.gif" alt="" style="display: none;" />
-
<p id="retorno"></p>
-
</form>
-
<a href="form-cidade.php" class="voltar">voltar</a>
-
</body>
-
</html>
ico-loading.gif

http://www.wbruno.co…ico-loading.gif
jquery-1.4.2.min.js
http://code.jquery.c…ry-1.4.2.min.js
Os códigos estão ai e funcionam.
