选择下拉列表时自动生成文本字段值

时间:2017-09-06 02:54:32

标签: javascript php oracle email yii

你好我正在使用Yii框架和oracle数据库。我需要一些帮助来解决这个问题。我有一个发送电子邮件的表格。我的表格看起来像这样

电子邮件desc:_______ | v | (下拉值表单数据库)

电子邮件主题:_________(此字段将自动填充所选电子邮件主题的数据)

电子邮件正文:_______(此字段将自动填充所选电子邮件主题的数据)

我的桌子:

email_desc |主题| body_email

payment1 |房屋租金支付|地址:****城市:雅加达

例如:我选择email_desc作为payment1,电子邮件主题将自动填写为“支付租金”,电子邮件正文将自动填写为地址:**** city:jakarta

这是我的表格

<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>

我的控制器

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;
    } 
}
}

我可以获得email_desc的值,但我仍然无法自动填写其他字段

1 个答案:

答案 0 :(得分:0)

您可以将代码更改为以下解决方案。

请按以下代码更改表单

<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>

请更改控制器代码:

<?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');
}
?>

我希望这会对你有所帮助。谢谢!