Yii Framework Forum: Problem In Dropdown List From Database - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem In Dropdown List From Database posting data using json Rate Topic: -----

#1 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 16 November 2012 - 07:32 AM

I am newbie . i want to register client information, it use drop down list for state from database table states.
My problem is that when i clik link to create client...a new dialog form open and displays nothing???????
when i remove the dropdown code it works fine..but i want to use dropdown in field of state of client form.

clientcontroller.php (actioncreate() snippet in controller file of client.php)
public function actionCreate()
{
$model=new client;

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['client']))
{
$model->attributes=$_POST['client'];
if($model->save())
{
if (Yii::app()->request->isAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'success',
'div'=>"Client successfully added"
));
exit;
}
else
$this->redirect(array('view','id'=>$model->id));
}
}

if (Yii::app()->request->isAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'failure',
'div'=>$this->renderPartial('_form', array('model'=>$model), true)));
exit;
}
else
$this->render('create',array('model'=>$model,));
}



In client/admin.php (a link is to create client,only snippet is here ).

<?php echo CHtml::link('Add New State', "", // the link for open the dialog
array(
'style'=>'cursor: pointer; font-size:20px; text-decoration: underline;',
'onclick'=>"{addstate(); $('#dialogstate').dialog('open');}"));
?>
</div><!-- add-form -->

<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
'id'=>'dialogstate',
'options'=>array(
'title'=>'Create Number Assignment',
'autoOpen'=>false,
'modal'=>true,
'width'=>550,
'height'=>470,
),
));
?>

<div class="divForForm"></div>

<?php $this->endWidget();?>


<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'assignment-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'state',
'code',
'status',

array(
'class'=>'CButtonColumn',
),
),
));
?>



<script type="text/javascript">
// here is the magic
function addstate()
{
<?php echo CHtml::ajax(array(
'url'=>array('states/create'),
'data'=> "js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#dialogstate div.divForForm').html(data.div);
// Here is the trick: on submit-> once again this function!
$('#dialogstate div.divForForm form').submit(addstate);
}
else
{
$('#dialogstate div.divForForm').html(data.div);
setTimeout(\"$('#dialogstate').dialog('close') \",3000);
}

} ",
))
?>;
return false;

}

</script>

plz help in dropdown list to generate?
0

#2 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,332
  • Joined: 21-February 11
  • Location:Greece

Posted 16 November 2012 - 08:49 AM

Hi,

Could you post the code with dropdown?
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 17 November 2012 - 01:34 AM

View PostKonApaz, on 16 November 2012 - 08:49 AM, said:

Hi,

Could you post the code with dropdown?


my dropdown list code:
<div class="row">
<?php echo $form->labelEx($model,'state'); ?>
<?php echo $form->dropDownList($model,'state', CHtml::listData(States::model()->findAll(),'state')),

<?php echo $form->error($model,'state'); ?>
</div>

oh yes i have changed my drop down list code as follow.
<?php echo $form->dropDownList($model,'state', CHtml::listData(States::model()->findAll(array('order' => 'state')),'code','state'));?>

its working now fine..its the effect of ur friendship
0

#4 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,332
  • Joined: 21-February 11
  • Location:Greece

Posted 17 November 2012 - 02:32 AM

View Posthameed, on 17 November 2012 - 01:34 AM, said:

my dropdown list code:
<div class="row">
<?php echo $form->labelEx($model,'state'); ?>
<?php echo $form->dropDownList($model,'state', CHtml::listData(States::model()->findAll(),'state')),

<?php echo $form->error($model,'state'); ?>
</div>

oh yes i have changed my drop down list code as follow.
<?php echo $form->dropDownList($model,'state', CHtml::listData(States::model()->findAll(array('order' => 'state')),'code','state'));?>

its working now fine..its the effect of ur friendship


Yes, dropDownList in this case require ($model,'field', <accociative array>) where the associative array is the CHtml::listData(States::model()->findAll(array('order' => 'state')),'code','state')
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 20 November 2012 - 05:29 AM

View PostKonApaz, on 17 November 2012 - 02:32 AM, said:

Yes, dropDownList in this case require ($model,'field', <accociative array>) where the associative array is the CHtml::listData(States::model()->findAll(array('order' => 'state')),'code','state')



Thanks KonApaz, i done it now.
0

#6 User is offline   Tirta Lalondong 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 20-April 14

Posted 01 May 2014 - 09:32 AM

View Posthameedhamdani, on 20 November 2012 - 05:29 AM, said:

Thanks KonApaz, i done it now.


It took me a while to figure it out, but it works perfectly fine now.
Thank you :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users