1

I am working on dependent dropdown in yii but when i send request using ajax it post all form values but i want to post only catid and want to get rid of all extra posting to make it light . below is the code of my view file please tell a way to post catid only

<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'foodproduct-form',
        'enableAjaxValidation' => false,
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
    ));
    ?>

    <div class="row">
        <p class="note">Fields with <span class="required">*</span> are required.</p>
    </div>
<div class="row">
    <?php echo $form->errorSummary($model); ?>
</div>

<div class="row">
    <?php
    $maincatAry = CommonAdminModel::getallmaincategories();
    echo CHtml::dropDownList('catid', 'title', $maincatAry, array
        (
        'empty' => 'Select Category',
        'ajax' => array
            (
            'type' => 'POST',
            'url' => CController::createUrl('foodproduct/getajaxmaincats'),
            'success' => 'js:function(data)
            { 
            var jsonData=$.parseJSON(data);
            if(jsonData.status=="nosubcatexist"){
               alert("nothing found");return false;
            } 
            var opt="<option value=>Select SubCategory</option>";
            $.each(jsonData,function(i,obj)
            {
                opt+="<option value=\'"+obj.id+"\'>"+obj.title+"</option>";
            });
            $("#subcatid").html(opt);    

            }',
            'error' => 'js:function(data){
                console.log(data);
            }'
        )
    ));
    ?>
</div>

<div class="row">
    <?php echo CHtml::dropDownList('subcatid', '', array()); ?>
</div>



<div class="row">
    <?php echo $form->labelEx($model, 'title'); ?>
    <?php echo $form->textField($model, 'title', array('size' => 60, 'maxlength' => 100)); ?>
    <?php echo $form->error($model, 'title'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model, 'description'); ?>
    <?php echo $form->textArea($model, 'description', array('rows' => 6, 'cols' => 50)); ?>
    <?php echo $form->error($model, 'description'); ?>
</div>

<div class="row">
    <?php echo CHtml::activeFileField($model, 'image'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model, 'availibility'); ?>
    <?php echo $form->textField($model, 'availibility', array('size' => 3, 'maxlength' => 3)); ?>
    <?php echo $form->error($model, 'availibility'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model, 'status'); ?>
    <?php echo $form->textField($model, 'status', array('size' => 8, 'maxlength' => 8)); ?>
    <?php echo $form->error($model, 'status'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model, 'shorturl'); ?>
    <?php echo $form->textField($model, 'shorturl', array('size' => 60, 'maxlength' => 255)); ?>
    <?php echo $form->error($model, 'shorturl'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model, 'slug'); ?>
    <?php echo $form->textField($model, 'slug', array('size' => 60, 'maxlength' => 255)); ?>
    <?php echo $form->error($model, 'slug'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

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

Here is code of my controller

 public function actiongetajaxmaincats() {
    $catid = $_POST['catid'];
    $qryAry = Foodcategory::model()->findByAttributes(array('id' => $catid, 'subcatexistence' => 'yes'));
    if (!empty($qryAry['id'])) {
        $qrysubcatAry = Foodsubcategory::model()->findAllByAttributes(array('catid' => $qryAry['id']));
        $ary = array();
        foreach ($qrysubcatAry as $i => $obj) {
            $ary[$i]['id'] = $obj['id'];
            $ary[$i]['title'] = $obj['title'];
        }
        echo json_encode($ary);
    } else {
        $ary = array();
        $ary = array('status' => 'nosubcatexist');
        echo json_encode($ary);
    }
}

Here is the image to describeHere is image of unwanted data posted via ajax , again i need only cat id nothing else

FahadAkram
  • 445
  • 1
  • 5
  • 25
  • Solved : 'data'=>array('catid'=>'js:this.value'), – FahadAkram May 23 '15 at 15:34
  • You may want to check this solution here (http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/). It does the same in a pretty much neat way with the update option. – Josep Alsina May 23 '15 at 16:04
  • every code had its own demand its not about pretty or un pretty and if you want success function and treat data in conditions there is not much difference b/w this code and mine – FahadAkram May 23 '15 at 17:27
  • It's been written with half of the lines and without conditional statement which makes it less prone to bugs and more mantainable – Josep Alsina May 24 '15 at 09:35

0 Answers0