我们正在尝试发布到同一操作的两种表单。 我们正在尝试从第一种形式检索输入字段txt1的值并将其传递给控制器列表,其中该值被发送以在第二种形式的for循环中使用以动态创建表单。 但是这个错误正在显示: “未定义的变量:计数器[APP \ View \ Lists \ add.ctp,第107行] CakePHP:快速开发php框架 CakePHP 2.6.1“ 我附加了控制器和视图的代码
查看:
<script>
$(document).ready(function() {
$('#txtt1').on('change',function(){
var paper=$(this).val();
//alert('hello');
if(paper<=0)
alert('No of members should be greater than zero');
if(paper!='')
//$('#apap').hide();
});
});
</script>
<h2>Member Details</h2>
<div id="post1">
</div>
<?php
echo $this->form->create('List',array('action'=>'add'));
echo $this->form->input('txt1',array('id'=>'txtt1'));
//echo $this->form->input('txt2',array('id'=>'txt2'));
echo $this->Js->submit('Enter No. of Panel Members: ',array('update'=>'#success','id'=>'apap','class'=>'butstyle'));
?>
</div>
<div id='success'>
<?php
$options =array('Mr.'=>'Mr.','Mrs.'=>'Mrs.','Ms.'=>'Ms.','Dr.'=>'Dr.','Prof'=>'Prof','Arch'=>'Arch');
echo $this->Html->script('jquery.min');
for($i=1;$i<=$counter;$i++)
{
echo $this->form->create('List',array('action'=>'add','type'=>'file','id'=>'frm'));?>
<?php
echo $this->form->input('salutation',array('type'=>'select','options'=>$options,'empty'=>'--Select--'));?>
<?php
echo $this->form->input('name');?>
<?php
echo $this->form->input('designation',array('type'=>'select','options'=>array($designation,'Others'=>'Others'),'empty'=>'--Select--'));?>
<div id="y" style="display: none">
Designation:
<input type="text" id="txtx" name="data[List][desig_txt]"/>
</div>
<?php
echo $this->form->input('specialization',array('type'=>'select','options'=>array($specialization,'Others'=>'Others'),'empty'=>'--Select--'));?>
<div id="x" style="display: none">
Specialization:
<input type="text" id="txty" name="data[List][spec_txt]" />
</div>
<?php echo
$this->form->input('university',array('type'=>'select','options'=>array($university,'Others'=>'Others'),'empty'=>'--Select--'));?>
<div id="w" style="display: none">
University:
<input type="text" id="txtw" name="data[List][univ_txt]" />
</div>
<?php
echo $this->form->input('college',array('type'=>'select','empty'=>'--Select--'));?>
<div id="z" style="display: none">
College:
<input type="text" id="txtz" />
</div>
<?php
echo $this->form->input('address');
echo $this->form->input('phone');
echo $this->form->input('email');
echo $this->form->input('papercode');
//echo $this->from->checkbox('approved');
echo $this->form->end('Save');
?>
<?php } ?>
</div>
控制器:
function add($paper=NULL)
{
$this->loadModel('Designation');
$this->loadModel('Specialization');
$this->loadModel('University');
//$this->loadModel('College');
$this->set('designation',$this->Designation->find("list",array(
'fields'=>array('desig_name','desig_name'))));
$this->set('specialization',$this->Specialization->find("list",array(
'fields'=>array('spec_name','spec_name'))));
//echo $paper;
$this->set('paper',$paper);
$this->set('university',$this->University->find("list",array('fields'=>array('univ_name','univ_name'))));
$this->set('counter','');
if(!empty($this->data))
{ //print_r($this->data);
if(empty($this->data['Panel'][0]['name']))
{ $count1=$this->data['Panel']['firstform'];
$count2=$this->data['Panel']['secondform'];
$counter=$count1+$count2;
//print_r('llalla');
if($this->request->is('ajax'))
{ print_r($counter);
$this->set('counter',$counter);
$this->render('add','ajax');
}
}
else{
print_r('balala');
$counter=$this->data['Panel']['extra'];
print_r('MR'.$counter);
for($i=0;$i<$counter;$i++)
{
print_r($i);
if($this->data['Panel'][$i]['designation']=="Others")
{
$desig=$this->data['Panel'][$i]['desig_txt'];
$this->loadModel('Designation');
$this->Panel->create();
$this->request->data['Designation']['desig_name']=$this->request->data['Panel'][$i]['desig_txt'];
$designationid=$this->Designation->find('count',array('fields'=>array('desig_id')))+1;
$this->request->data['Designation']['desig_id']= $designationid;
//print_r($this->request->data['Designation']['desig_name']);
if($this->Designation->save($this->request->data))
{ echo "hello";}
}
else
{
$desig=$this->data['Panel'][$i]['designation'];
}
if($this->data['Panel'][$i]['specialization']=="Others")
{
$this->loadModel('Specialization');
$spec=$this->data['Panel'][$i]['spec_txt'];
$this->Panel->create();
$this->request->data['Specialization']['spec_name']=$this->request->data['Panel'][$i]['spec_txt'];
$specializationid=$this->Specialization->find('count',array('fields'=>array('spec_id')))+1;
$this->request->data['Specialization']['spec_id']= $specializationid;
//print_r($this->request->data['Specialization']['spec_name']);
if($this->Specialization->save($this->request->data))
{ echo "hello";}
}
else
{
$spec=$this->data['Panel'][$i]['specialization'];
}
//if($this->Designation->save($univ)){ echo "hello";}
if($this->data['Panel'][$i]['university']=="Others")
{
$this->loadModel('University');
$univ=$this->data['Panel'][$i]['univ_txt'];
$this->Panel->create();
$this->request->data['University']['univ_name']=$this->request->data['Panel'][$i]['univ_txt'];
$universityid=$this->University->find('count',array('fields'=>array('univ_id')))+1;
$this->request->data['University']['univ_id']= $universityid;
//print_r($this->request->data['University']['univ_name']);
if($this->University->save($this->request->data))
{ echo "hello";}
}
else
{
$univ=$this->data['Panel'][$i]['university'];
}
//if($this->Designation->save($univ)){ echo "hello";}
if($this->data['Panel'][$i]['college']=="Others")
{
$this->loadModel('College');
$college=$this->data['Panel'][$i]['college_txt'];
$this->Panel->create();
$this->request->data['College']['college_name']=$this->request->data['Panel'][$i]['college_txt'];
$collegeid=$this->College->find('count',array('fields'=>array('college_id')))+1;
$this->request->data['College']['college_id']= $collegeid;
//print_r($this->request->data['University']['univ_name']);
if($this->College->save($this->request->data))
{ echo "hello";}
}
else
{
$college=$this->data['Panel'][$i]['college'];
}
$this->Panel->query('insert into panels(salutation,name,designation,specialization,university,college,address,phone,email,papercode) values("'.$this->data['Panel'][$i]['salutation'].'","'.$this->data['Panel'][$i]['name'].'","'.$desig.'","'.$spec.'","'.$univ.'","'.$college.'","'.$this->data['Panel'][$i]['address'].'","'.$this->data['Panel'][$i]['phone'].'","'.$this->data['Panel'][$i]['email'].'","'.$this->data['Panel'][$i]['papercode'].'")');
}
$this->redirect(array('controller'=>'panels','action'=>'index',$this->data['Panel'][$i-1]['papercode']));
}
}
}
答案 0 :(得分:0)
当数据进入保存时,你已经设置了'$ rd'变量。当页面加载时,它首次出现,然后它不会进入你的if条件
if(!empty($this->data['List']['txt1'])) { echo 'kkl'; if($this->RequestHandler->isAjax()) { echo 'dddd'; $this->set('rd',$this->data['List']['txt1']); $this->render('add','ajax'); } }
你应该在函数的开头设置'rd'变量,就像这样
$this->set('rd','');
现在rd变量将在页面加载或保存数据中同时设置两种情况