Dropdownlist Onchange

Hi All,

I had status_id dropDownList on _form below that populate inspection_date and date_dismantled with dates from the value of 2 hiddenFields (today & today+7days) using onChange event.




<?php

/* @var $this InspectionController */

/* @var $model Inspection */

/* @var $form CActiveForm */

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'inspection-form',

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>false,

        'htmlOptions' => array('enctype' => 'multipart/form-data')

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'date_inspected'); ?>

		<?php $this->widget('zii.widgets.jui.CJuiDatePicker',

                        array(

                            'attribute' => 'date_inspected',

                            'model' => $model,

                            // additional javascript options for the date picker plugin

                            'options' => array(

                                'autoSize' => true,

                                'showAnim' => 'slideDown',

                                'showOn' => 'button',

                                'changeMonth' => true,

                                'changeYear' => true,

                                'dateFormat' => 'yy-mm-dd',

                            ),

                            'htmlOptions' => array(

                                'style' => 'height:20px;',

                            ),

                    ));?>

		<?php echo $form->error($model,'date_inspected'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'status_id'); ?>

		<?php echo $form->dropDownList($model,'status_id', $model->getStatusOptions(), 

                        array(

                            'prompt'=>'Select Status',

                            'onChange'=>'if (this.value == 3)

                                {

                                    this.form.elements["Inspection[deluge_id]"].value = 1

                                    this.form.elements["Inspection[los_id]"].value = 1

                                    this.form.elements["Inspection[deboard_id]"].value = 1

                                    this.form.elements["Inspection[inspection_date]"].value = ""

                                    this.form.elements["Inspection[date_dismantled]"].value = this.form.elements["today"].value

                                }

                                else if (this.value == "")

                                {

                                    this.form.elements["Inspection[inspection_date]"].value = ""

                                    this.form.elements["Inspection[date_dismantled]"].value = ""

                                }

                                else

                                {

                                    this.form.elements["Inspection[inspection_date]"].value = this.form.elements["today_+7days"].value                   

                                    this.form.elements["Inspection[date_dismantled]"].value = ""

                                }'

                            )); ?>

		<?php echo $form->error($model,'status_id'); ?>

	</div>


        <?php echo CHtml::hiddenField('today', date('Y-m-d')) ?>

        <?php echo CHtml::hiddenField('today_+7days', date('Y-m-d', strtotime('+7 days '))) ?>

        

        <div class="row">

            <div class="span-4">

		<?php echo $form->labelEx($model,'deluge_id'); ?>

		<?php echo $form->dropDownList($model,'deluge_id', $model->getDelugeOptions()); ?>

		<?php echo $form->error($model,'deluge_id'); ?>

	</div>


	<div class="span-4 last">

		<?php echo $form->labelEx($model,'los_id'); ?>

		<?php echo $form->dropDownList($model,'los_id', $model->getLosOptions()); ?>

		<?php echo $form->error($model,'los_id'); ?>

	</div>


		<?php echo $form->labelEx($model,'deboard_id'); ?>

		<?php echo $form->dropDownList($model,'deboard_id', $model->getDeboardOptions()); ?>

		<?php echo $form->error($model,'deboard_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'inspection_date'); ?>

		<?php $this->widget('zii.widgets.jui.CJuiDatePicker',

                        array(

                            'attribute' => 'inspection_date',

                            'model' => $model,

                            // additional javascript options for the date picker plugin

                            'options' => array(

                                'autoSize' => true,

                                'showAnim' => 'slideDown',

                                'showOn' => 'button',

                                'changeMonth' => true,

                                'changeYear' => true,

                                'dateFormat' => 'yy-mm-dd',

                            ),

                            'htmlOptions' => array(

                                'style' => 'height:20px;',

                            ),

                    ));?>

		<?php echo $form->error($model,'inspection_date'); ?>

	</div>

        

        <div class="row">

		<?php echo $form->labelEx($model,'date_dismantled'); ?>

		<?php $this->widget('zii.widgets.jui.CJuiDatePicker',

                        array(

                            'attribute' => 'date_dismantled',

                            'model' => $model,

                            // additional javascript options for the date picker plugin

                            'options' => array(

                                'autoSize' => true,

                                'showAnim' => 'slideDown',

                                'showOn' => 'button',

                                'changeMonth' => true,

                                'changeYear' => true,

                                'dateFormat' => 'yy-mm-dd',

                            ),

                            'htmlOptions' => array(

                                'style' => 'height:20px;',

                            ),

                    ));?>

		<?php echo $form->error($model,'date_dismantled'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


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


</div><!-- form -->



If I want to populate the inspection_date with the value of date_inspected +7 days then what codes should I add or change here:




	<?php echo $form->dropDownList($model,'status_id', $model->getStatusOptions(), 

                        array(

                            'prompt'=>'Select Status',

                            'onChange'=>'if (this.value == 3)

                                {

                                    this.form.elements["Inspection[deluge_id]"].value = 1

                                    this.form.elements["Inspection[los_id]"].value = 1

                                    this.form.elements["Inspection[deboard_id]"].value = 1

                                    this.form.elements["Inspection[inspection_date]"].value = ""

                                    this.form.elements["Inspection[date_dismantled]"].value = this.form.elements["today"].value

                                }

                                else if (this.value == "")

                                {

                                    this.form.elements["Inspection[inspection_date]"].value = ""

                                    this.form.elements["Inspection[date_dismantled]"].value = ""

                                }

                                else

                                {

                                    this.form.elements["Inspection[inspection_date]"].value = this.form.elements["today+7days"].value                   

                                    this.form.elements["Inspection[date_dismantled]"].value = ""

                                }'

                            )); ?>

		<?php echo $form->error($model,'status_id'); ?>

	</div>


        <?php echo CHtml::hiddenField('today', date('Y-m-d')) ?>

        <?php echo CHtml::hiddenField('today+7days', date('Y-m-d', strtotime('+7 days '))) ?>



Hi,

What output is coming with your current code… Please can you post your screen. I can say you were you have to add particular code to populate value…

Can create date in javascript itself.

example


 var actualDate = new Date(); 

    var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+7); // create new 7 day increased date

    

    var curr_date = newDate.getDate();

    var curr_month = newDate.getMonth() + 1; 

    if(curr_month < 10){

        curr_month='0'+curr_month; //prepand zero if month is less than 10

    }

    if(curr_date < 10){

        curr_+curr_date; //prepand zero if day is less than 10

    }

    var curr_year = newDate.getFullYear();

    var newDateString =( curr_year+ "-" +curr_month+ "-" + curr_date); //set your date format here

    console.log(newDateString);

    $('#id').val(newDateString);

status_id dropDownlist (Status on attached screen shot) has 3 options; Green Tag, Red Tag & Dismantled with id of 1,2&3 respectively.

If Status is Green Tag or Red Tag, I want the inspection_date (Next Inspection Due) populated with the value of date_inspected (Date Inspected) +7 days instead of this:




this.form.elements["Inspection[inspection_date]"].value = this.form.elements["today+7days"].value


//today+7days hiddenField

<?php echo CHtml::hiddenField('today+7days', date('Y-m-d', strtotime('+7 days '))) ?>



I tried this code but it’s not working:




this.form.elements["Inspection[inspection_date]"].value = this.form.elements["Inspection[date_inspected]"].value+7 days