当用户输入他/她的MerryParentEmail地址时,应该从数据库中获取相应的名称并显示在MerryParentName输入文本框中。我已经尝试过使用ajax,jquery和php这样做了。但是现在,当我使用ajax,jquery和cakephp时,没有显示相应的名称。
警报输出:数据%5BMerryParent%5D%5Bemail%5D = b
有人可以指出我哪里出错吗?谢谢。
default.ctp(布局文件)
<script type="text/javascript">
$(document).ready(function(){
$("#MerryParentEmail").keyup(function(){
//txt=$("#MerryParentEmail").val();
txt=$("#MerryParentEmail").serialize();
alert(txt);
$.post("../merry_parents/getname",txt,function(result){
$("#MerryParentName").val(result);
});
});
});
</script>
view.ctp
<?php
echo $this->Session->flash();
//echo $this->Form->create('MerryParent', array('default'=>false));
echo $this->Form->create('MerryParent');
echo '<fieldset>';
echo '<legend>Parent Information</legend>';
echo $this->Form->input('MerryParent.email');
/*if (isset($name))
echo $this->Form->input('MerryParent.name', array('label'=>'Parent/Guardian Name','value'=>$name));
else*/
echo $this->Form->input('MerryParent.name', array('label'=>'Parent/Guardian Name'));
echo '</fieldset>';
//echo $this->Form->end('Submit');
?>
merry_parents_controller.php
<?php
class MerryParentsController extends AppController{
public $many_children_flag='false';
var $name='MerryParents';
function beforeFilter()//executed before any controller action logic
{ parent::beforeFilter();
$this->Security->validatePost=false; //set to false to completely skip the validation of POST request
//parent::beforeFilter();
if(isset($this->Security) && $this->RequestHandler->isAjax()){
$this->Security->enabled = false;
Configure::write('debug', 0);
$this->autoRender=false;
}
}
function getname(){
if (!empty($this->data)){
//var_dump($this->data);
$merryparent_info=$this->MerryParent->getMerryParents($this->data['MerryParent']['email']);
//print_r($merryparent_info);
$name=$merryparent_info['MerryParent']['name'];
$this->set('name',$name);
//$this->render('/merry_parents/ajax_input');
}
}
function view(){
//$name='';
//var_dump($this->data);
//$this->getname();
}
merry_parent.php模型
function getMerryParents($field_value){
if (is_int($field_value))
$conditions=array('merryParent.id'=>$field_value);
else
$conditions=array('merryParent.email'=>$field_value);
//debug($conditions);
$merryparent_info=$this->find('first',array(
'conditions'=>$conditions,
'recursive'=>-1 //fetches merry_parents table data only not the associated data
));
//debug($merryparent_info);
return $merryparent_info;
}
答案 0 :(得分:1)
尝试:
//add
$this->layout = false;
//and instead of setting name simply echo the name from your function, like
echo $name;
希望有所帮助