Reputation: 63
I am trying to validate the dropdownlist as the value of dropdownlist changes. I want to check is there an in the the table already of the selected job status. Below is my code:
<script>
function validate_dropdown(id)
{
alert("Selected id = "+id);
//var msg = <?php echo NotificationRules::model()->validate_job($_POST['id']);?>
//alert("Message from model func = "+msg);
}
</script>
<?php
echo $form->dropDownList($model, 'job_status_id', $jobstatuslist ,
array('empty'=>'Please Select job status (required)', 'onchange'=>'js:validate_dropdown(this.value)')
);
?>
I am trying to pass js variable id to php function and send back a message if there is already an entry for the selected job status. I am able to get selected value in js function validate_dropdown(), but not able to proceed further.Anybody pls help.......
Upvotes: 2
Views: 9890
Reputation: 63
I solved the problem by making an AJAX call...
Final working code:
Code of dropdown in view
<?php
echo $form->dropDownList($model, 'job_status_id', $jobstatuslist ,
array(//AJAX CALL.
'prompt' => 'Please Select job status (required)',
'value' => '0',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('NotificationRules/notificationPresent/'),
'data' => array("job_id" => "js:this.value"),
'success'=> 'function(data) {
if(data == 1)
{
alert("Rule is already present for this status, Please update existing rule.");
}
}',
'error'=> 'function(){alert("AJAX call error..!!!!!!!!!!");}',
)//end of ajax array().
));
?>
Code in controller(action)
<?php
public function actionNotificationPresent()
{
if (Yii::app()->request->isAjaxRequest)
{
//pick off the parameter value
$job_id = Yii::app()->request->getParam( 'job_id' );
if($job_id != '')
{
//echo "Id is received is ".$job_id;
$rulesModel = NotificationRules::model()->findAllByAttributes(array('job_status_id'=>$job_id));
if(count($rulesModel))
echo 1;
else
echo 0;
}//end of if(job_id)
else
{
//echo "Id id not received";
echo 0;
}
}//end of if(AjaxRequest).
}//end of NotificationPresent().
?>
Now i am getting an alert if there is any rule already with selected job status.
Upvotes: 0
Reputation: 8726
Check this bellow example. In this i'm displaying all the users in a drop down list. I'm keeping user id as option value and username as option label.
User Table:
id username
------------------
1 Heraman
2 Dileep
3 Rakesh
4 Kumar
<?php
$list=CHtml::listData(User::model()->findAll(), 'id', 'username');
echo CHtml::dropDownList('username', $models->username, $list, array('empty' => '---Select User---','onchange'=>'alert(this.value)'));
?>
In you case, you can use
'onchange'=>'validate_dropdown(this.value)
//Your script
<script>
function validate_dropdown(id)
{
alert("Selected id = "+id);
}
</script>
Upvotes: 1