Monday, March 7, 2022

[FIXED] auto generate text field value when dropdown is select

Issue

hello i'm using Yii framework and oracle database. i need some help to solve this problem. i've a form for sending an email. my form looks like this

email desc: _______|v| (dropdown value form database)

email subject : _________ (this field will automatically filled with data of selected email subject)

email body : _______ (this field will automatically filled with data of selected email subject)

my table:

email_desc| subject| body_email

payment1 | payment for house rent | address: **** city: jakarta

ex: i choose email_desc as payment1, email subject will automatically filled as "payment for rent" and email body will automatically filled as address: **** city: jakarta

here is my form

<div class="control-group">
    <?php echo $form->labelEx($model,'EMAIL_DESC', array('class'=>'control-label')); ?>
    <?php echo $form->dropDownlist($model,'EMAIL_DESC', 
        (CHtml::listData (TrnProjImplement::model()->getList(),'EMAIL_DESC','EMAIL_DESC')),
            array(
            'empty'=>'--Pilih salah satu--',
            'value'=>$model->EMAIL_DESC,
            'id'=>"dropDown",
            'ajax'=>array(
                'type'=>'GET',
                'url'=>CController::createUrl('email/field'),
                'update'=>'#subject',
                'data'=>array('EMAIL_DESC'=>'js: this.value'),
            ),
            ));
            ?>
        <span class="help-inline text-error"><?php echo $form->error($model,'EMAIL_DESC'); ?></span>        
</div>

    <div class="control-group">
        <?php echo $form->labelEx($model,'SUBJECT', array('class'=>'control-label')); ?>
        <?php echo $form->textField($model,'SUBJECT',array('size'=>60,'maxlength'=>100, 'style'=>'width:600px', 'id'=>"subject"));?> 
        <?php //echo CHtml::activeTextField($model, 'SUBJECT', 
        // array(
            // 'ajax'=>array(
                // 'type'=>'POST',
                // 'url'=>Yii::app()->createUrl('email/create'),
                // 'id'=>'subject',
            // ),
        // ));
        ?> 
        <span class="help-inline text-error"><?php echo $form->error($model,'SUBJECT'); ?></span>
    </div>

    <!--<div class="control-group" id="body-email">-->
    <div class="control-group">
        <?php echo $form->labelEx($model,'BODY_EMAIL', array('class'=>'control-label')); ?>
        <?php echo $form->textArea($model,'BODY_EMAIL',array('size'=>1000,'maxlength'=>1000,'style'=>'height:200px; width:600px;', 'id'=>"body-email")); ?>
        <span class="help-inline text-error"><?php echo $form->error($model,'BODY_EMAIL'); ?></span>
    </div>

javascript

<script>
function insertField(){
        var desc = document.getElementById("dropDown").value;
        var subj = document.getElementById("subject").value;
        var body = document.getElementById("body-email").value;

        $.ajax({
            type : "POST",
            url : "<?php echo Yii::app()->createUrl("emaildetail/field"); ?>",
            data : {
                "desc" : desc,
                "subj" : subj,
                "body" : body,
            },
            success: function (){
                alert('asd');
            },
        });
</script>

my controller

public function actionField(){

if(isset($_POST['desc'])){
    $connection=Yii::app()->db;

    $desc = Yii::app()->request->getPost('desc');
    $subj = Yii::app()->request->getPost('subj');
    $body = Yii::app()->request->getPost('body');

    $sql = 'SELECT SUBJECT FROM EMAIL_DETAIL WHERE EMAIL_DESC = $desc';
    $sql2 = 'SELECT BODY_EMAIL FROM EMAIL_DETAIL WHERE EMAIL_DESC = $desc';

    $model2 = EMAIL_DETAIL::model()->findByAttributes(array('EMAIL_BODY'=>$desc));

    if(isset($desc)){ 
        $subj = $model->SUBJECT;
        $body = $model->EMAIL_BODY;
    } 
}
}

i could get the value of email_desc but i still cant automatically fill other field


Solution

You can change your code as following solution.

Please change your form as below code :

<div class="control-group">
    <?php echo $form->labelEx($model,'EMAIL_DESC', array('class'=>'control-label')); ?>
    <?php echo $form->dropDownlist($model,'EMAIL_DESC', 
        (CHtml::listData (TrnProjImplement::model()->getList(),'EMAIL_DESC','EMAIL_DESC')),
            array(
            'empty'=>'--Pilih salah satu--',
            'value'=>$model->EMAIL_DESC,
            'id'=>"dropDown",
                'ajax'=>array(
                    'type'=>'POST',
                    'url'=>CController::createUrl('email/field'),
                    'data'=>array('EMAIL_DESC'=>'js: this.value'),
                    'success'=> 'function(response) {if (response.status == "success") {
                                 $("#SUBJECT").val(response.subj);
                                 $("#BODY_EMAIL").val(response.body);
                            } else {
                                 $("#SUBJECT").val("");
                                 $("#BODY_EMAIL").val("");
                            } 
                     }',
                    'error'=> 'function(){alert("AJAX call error..!!!!!!!!!!");}',
                ),
            ));
            ?>
    <span class="help-inline text-error">
        <?php echo $form->error($model,'EMAIL_DESC'); ?>
    </span>        
</div>
<div class="control-group">
    <?php echo $form->labelEx($model,'SUBJECT', array('class'=>'control-label')); ?>
    <?php echo $form->textField($model,'SUBJECT',array('size'=>60,'maxlength'=>100, 'style'=>'width:600px', 'id'=>"subject"));?> 
    <span class="help-inline text-error"><?php echo $form->error($model,'SUBJECT'); ?></span>
</div>
<div class="control-group">
    <?php echo $form->labelEx($model,'BODY_EMAIL', array('class'=>'control-label')); ?>
    <?php echo $form->textArea($model,'BODY_EMAIL',array('size'=>1000,'maxlength'=>1000,'style'=>'height:200px; width:600px;', 'id'=>"body-email")); ?>
    <span class="help-inline text-error"><?php echo $form->error($model,'BODY_EMAIL'); ?></span>
</div>

Please change your Controller code :

<?php

public function actionField() {
    if (Yii::app()->request->isAjaxRequest){

        $desc = Yii::app()->request->getPost('EMAIL_DESC');
        $model = EMAIL_DETAIL::model()->findByAttributes(array('EMAIL_BODY' => $desc));

        if (!empty($model)) {
            $data['status'] = 'success';
            $data['subj'] = $model->SUBJECT;
            $data['body'] = $model->EMAIL_BODY;
        } else {
            $data['status'] = 'error';
            $data['subj'] = '';
            $data['body'] = '';
        }
        echo json_encode($data);
       Yii::app()->end(); 
    } else
    throw new CHttpException(400, '404_error_message');
}
?>

I hope this will helps you. Thanks!



Answered By - NikuNj Rathod

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.