Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

javascript - give two function in one button in Yii framework

I have a question about Yii framework, i have problem with submit button, i want to given two fungsi save and update in one submit button, can anyone tell me how to set that function on form ?

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

i change 'Save' with 'Update' it's still have error Primary key added, how i can create two function update and save in one push button ?

    public function actionCreate()
{
    $model=new TblUasUts;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['TblUasUts']))
    {
        $model->attributes=$_POST['TblUasUts'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->nim_mhs));
    }
            if(isset($_POST['TblUasUts'])
    {
            $model->attributes=$_POST['TblUasUts'];
            if($model->update())
            $this->redirect(array('view','id'=>$model->nim_mhs));
     }                
    $this->render('update',array(
        'model'=>$model,
    ));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your form, you can use something like :

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

As far as processing different actions on the backend code, there are a few options, for example, you could :-

  • Direct your form to different URLs
  • Set a (hidden) field (for example ID) and parse for that.
  • Use the default action from the activeForm, which directs back to the invoking action, for example actionCreate(), or actionUpdate()

In light of your updates, please extend your controller as per my initial suggestion to have another action actionUpdate()

The main difference between the actionCreate(), or actionUpdate() actions is that the Create action create a new (empty) TblUasUts object, while the Update action populates the TblUasUts object from the database.

public function actionCreate()
{
    $model=new TblUasUts;
    ...
    ... Do things with $model ...
    ...
    $model->save();

}

public function actionUpdate
{
    // The id of the existing entry is passed in the url. for example
    // ...http:// .... /update/id/10
    //
    $model = TblUasUts::model()->findByPK($_GET['id']);
    ...
    ... Do things with $model ...
    ...
    $model->save();

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...