Ajax Select dinamico funciona no localhost do windows mas no servidor linux não com cakephp 2.4!! :( alguma dica ?
12/08/2021
0
//No layout default esta assim!:
<!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>
<?php echo $this->Html->charset(); ?>
<title>
<?php echo $title_for_layout; ?>
</title>
<?php
echo $this->Html->script(''jquery'');
echo $this->Html->meta(''icon'');
echo $this->Html->css(''cake.generic'');
?>
</head>
<body>
<div id="container">
<div id="header">
<h1>Dynamic Select Box Demonstration</h1>
</div>
<div id="content">
<?php echo $this->Session->flash(); ?>
<?php echo $content_for_layout; ?>
</div>
<div id="footer">
footer
</div>
</div>
<?php //echo $this->element(''sql_dump''); ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<!-- scripts_for_layout -->
<?php echo $scripts_for_layout; ?>
<!-- Js writeBuffer -->
<?php
if (class_exists(''JsHelper'') && method_exists($this->Js, ''writeBuffer'')){ echo $this->Js->writeBuffer(); }
// Writes cached scripts
?>
</body>
</html>
//No controller esta assim :
<?php
App::uses(''AppController'', ''Controller'');
class HomesController extends AppController {
var $components = array(''RequestHandler'');
var $helpers = array(''Js'');
function beforeFilter() {
$this->loadModel(''Estado'');
$this->loadModel(''Cidade'');
}
function index() {
$this->set(''title_for_layout'', ''Exemplo de select box dinamico com cidades e estados do Brasil'');
$this->set(''estados'', $this->Estado->find(''list''));
}
public function add() {
$this->loadModel(''Estado'');
$this->loadModel(''Cidade'');
$this->set(''title'', ''Adicionar Cidade'');
$this->set(''estados'', $this->Estado->find(''list''));
}
public function getPorCidade() {
$bolete = $this->request->data[''Cidade''][''estado_id''];
$this->layout = true;
if($bolete == ''''){
$discriminas = ''<< erro >>'';
}else{
$discriminas = $this->Cidade->find(''list'', array(
''conditions'' => array(''Cidade.estado_id'' => $bolete),
''recursive'' => -1
));
}
$this->set(''discriminas'',$discriminas);
$this->layout = ''ajax'';
}
}
//nos models está assim:
<?php
class Home extends AppModel {
public $useTable = false;
}
?>
<?php
App::uses(''AppModel'', ''Model'');
class Estado extends AppModel {
public $displayField = ''uf'';
public $hasMany = array(
''Cidade'' => array(
''className'' => ''Cidade'',
''foreignKey'' => ''estado_id'',
''dependent'' => false,
''conditions'' => '''',
''fields'' => '''',
''order'' => '''',
''limit'' => '''',
''offset'' => '''',
''exclusive'' => '''',
''finderQuery'' => '''',
''counterQuery'' => ''''
)
);
}
<?php
App::uses(''AppModel'', ''Model'');
class Cidade extends AppModel {
public $displayField = ''nome'';
public $belongsTo = array(
''Estado'' => array(
''className'' => ''Estado'',
''foreignKey'' => ''estado_id'',
''conditions'' => '''',
''fields'' => '''',
''order'' => ''''
)
);
}
?>
nas views está assim:
index:
<?php //echo $this->Html->script(''views/Home/listar_cidades.js''); nao precisa?>
<table width="100%">
<td><div class="actions"><?php echo $this->Html->link(''Adicionar'', array(''action'' => ''add'')); ?></div></td><td></td>
</table>
<div class="form">
<?php echo $this->Form->create(''Cidade''); ?>
<fieldset>
<legend><?php echo __(''Exemplo de select dinamico com cidades e estados do Brasil''); ?></legend>
<?php echo $this->Form->input(''estado_id'', array(''type'' => ''select'', ''options'' => $estados, ''id'' => ''estados'', ''empty'' => ''selecione um estado'')) ?>
<?php echo $this->Form->input(''cidade_id'', array(''type'' => ''select'', ''id'' => ''cidada'', ''empty'' => ''selecione uma cidade'')) ?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
<?php
$this->Js->get(''#estados'')->event(''change'',
$this->Js->request(array(
''controller''=>''Homes'',
''action''=>''getPorCidade''
), array(
''update''=>''#cidada'',
''async'' => true,
''method'' => ''post'',
''dataExpression''=>true,
''data''=> $this->Js->serializeForm(array(
''isForm'' => true,
''inline'' => true
))
))
);
?>
get_Por_Cidade:
<?php foreach ($discriminas as $key => $value): ?>
<option value="<?php echo $key; ?>"><?php echo /*$key." - ".*/$value;?></option>
<?php endforeach; ?>
add:
<?php echo $this->Html->script(''views/Home/listar_cidades.js''); ?>
<table width="100%">
<td><div class="actions"><?php echo $this->Html->link(''Lista'', array(''action'' => ''index'')); ?></div></td>
</table>
<div class="form">
<?php //echo $this->Form->create(null); ?>
<fieldset>
<?php echo $this->Form->create(''Cidade''); ?>
<?php echo $this->Form->input(''estado_id'', array(''type'' => ''select'', ''options'' => $estados, ''id'' => ''estados'', ''empty'' => ''selecione um estado'')) ?>
<?php echo $this->Form->input(''cidade_id'', array(''type'' => ''select'', ''id'' => ''cidades'', ''empty'' => ''selecione uma cidade'')) ?>
<?php echo $this->Form->end(''Salvar''); ?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
<?php echo $this->Html->script(''views/Home/listar_cidades.js''); ?>
<?php
$this->Js->get(''#estados'')->event(''change'',
$this->Js->request(array(
''controller''=>''Homes'',
''action''=>''getPorCidade''
), array(
''update''=>''#cidades'',
''async'' => true,
''method'' => ''post'',
''dataExpression''=>true,
''data''=> $this->Js->serializeForm(array(
''isForm'' => true,
''inline'' => true
))
))
);
?>
<!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>
<?php echo $this->Html->charset(); ?>
<title>
<?php echo $title_for_layout; ?>
</title>
<?php
echo $this->Html->script(''jquery'');
echo $this->Html->meta(''icon'');
echo $this->Html->css(''cake.generic'');
?>
</head>
<body>
<div id="container">
<div id="header">
<h1>Dynamic Select Box Demonstration</h1>
</div>
<div id="content">
<?php echo $this->Session->flash(); ?>
<?php echo $content_for_layout; ?>
</div>
<div id="footer">
footer
</div>
</div>
<?php //echo $this->element(''sql_dump''); ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<!-- scripts_for_layout -->
<?php echo $scripts_for_layout; ?>
<!-- Js writeBuffer -->
<?php
if (class_exists(''JsHelper'') && method_exists($this->Js, ''writeBuffer'')){ echo $this->Js->writeBuffer(); }
// Writes cached scripts
?>
</body>
</html>
//No controller esta assim :
<?php
App::uses(''AppController'', ''Controller'');
class HomesController extends AppController {
var $components = array(''RequestHandler'');
var $helpers = array(''Js'');
function beforeFilter() {
$this->loadModel(''Estado'');
$this->loadModel(''Cidade'');
}
function index() {
$this->set(''title_for_layout'', ''Exemplo de select box dinamico com cidades e estados do Brasil'');
$this->set(''estados'', $this->Estado->find(''list''));
}
public function add() {
$this->loadModel(''Estado'');
$this->loadModel(''Cidade'');
$this->set(''title'', ''Adicionar Cidade'');
$this->set(''estados'', $this->Estado->find(''list''));
}
public function getPorCidade() {
$bolete = $this->request->data[''Cidade''][''estado_id''];
$this->layout = true;
if($bolete == ''''){
$discriminas = ''<< erro >>'';
}else{
$discriminas = $this->Cidade->find(''list'', array(
''conditions'' => array(''Cidade.estado_id'' => $bolete),
''recursive'' => -1
));
}
$this->set(''discriminas'',$discriminas);
$this->layout = ''ajax'';
}
}
//nos models está assim:
<?php
class Home extends AppModel {
public $useTable = false;
}
?>
<?php
App::uses(''AppModel'', ''Model'');
class Estado extends AppModel {
public $displayField = ''uf'';
public $hasMany = array(
''Cidade'' => array(
''className'' => ''Cidade'',
''foreignKey'' => ''estado_id'',
''dependent'' => false,
''conditions'' => '''',
''fields'' => '''',
''order'' => '''',
''limit'' => '''',
''offset'' => '''',
''exclusive'' => '''',
''finderQuery'' => '''',
''counterQuery'' => ''''
)
);
}
<?php
App::uses(''AppModel'', ''Model'');
class Cidade extends AppModel {
public $displayField = ''nome'';
public $belongsTo = array(
''Estado'' => array(
''className'' => ''Estado'',
''foreignKey'' => ''estado_id'',
''conditions'' => '''',
''fields'' => '''',
''order'' => ''''
)
);
}
?>
nas views está assim:
index:
<?php //echo $this->Html->script(''views/Home/listar_cidades.js''); nao precisa?>
<table width="100%">
<td><div class="actions"><?php echo $this->Html->link(''Adicionar'', array(''action'' => ''add'')); ?></div></td><td></td>
</table>
<div class="form">
<?php echo $this->Form->create(''Cidade''); ?>
<fieldset>
<legend><?php echo __(''Exemplo de select dinamico com cidades e estados do Brasil''); ?></legend>
<?php echo $this->Form->input(''estado_id'', array(''type'' => ''select'', ''options'' => $estados, ''id'' => ''estados'', ''empty'' => ''selecione um estado'')) ?>
<?php echo $this->Form->input(''cidade_id'', array(''type'' => ''select'', ''id'' => ''cidada'', ''empty'' => ''selecione uma cidade'')) ?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
<?php
$this->Js->get(''#estados'')->event(''change'',
$this->Js->request(array(
''controller''=>''Homes'',
''action''=>''getPorCidade''
), array(
''update''=>''#cidada'',
''async'' => true,
''method'' => ''post'',
''dataExpression''=>true,
''data''=> $this->Js->serializeForm(array(
''isForm'' => true,
''inline'' => true
))
))
);
?>
get_Por_Cidade:
<?php foreach ($discriminas as $key => $value): ?>
<option value="<?php echo $key; ?>"><?php echo /*$key." - ".*/$value;?></option>
<?php endforeach; ?>
add:
<?php echo $this->Html->script(''views/Home/listar_cidades.js''); ?>
<table width="100%">
<td><div class="actions"><?php echo $this->Html->link(''Lista'', array(''action'' => ''index'')); ?></div></td>
</table>
<div class="form">
<?php //echo $this->Form->create(null); ?>
<fieldset>
<?php echo $this->Form->create(''Cidade''); ?>
<?php echo $this->Form->input(''estado_id'', array(''type'' => ''select'', ''options'' => $estados, ''id'' => ''estados'', ''empty'' => ''selecione um estado'')) ?>
<?php echo $this->Form->input(''cidade_id'', array(''type'' => ''select'', ''id'' => ''cidades'', ''empty'' => ''selecione uma cidade'')) ?>
<?php echo $this->Form->end(''Salvar''); ?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
<?php echo $this->Html->script(''views/Home/listar_cidades.js''); ?>
<?php
$this->Js->get(''#estados'')->event(''change'',
$this->Js->request(array(
''controller''=>''Homes'',
''action''=>''getPorCidade''
), array(
''update''=>''#cidades'',
''async'' => true,
''method'' => ''post'',
''dataExpression''=>true,
''data''=> $this->Js->serializeForm(array(
''isForm'' => true,
''inline'' => true
))
))
);
?>
Evandro Farias
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)