Sari Rahal
Sari Rahal

Reputation: 1955

yii, action calling a different view

I am currently working on a Yii framework project, and I would like to create a button that is an easy pick for the users. Basically, they push the button, the onclick calls an action, the action sets an array and $_POST the array to the current page (refreshes the page) The page will use that array to loads accordingly.

Any help would be nice. If I am doing this and there is a better way, please let me know.

VIEW for the complete view ( http://mysticpaste.com/view/ZWOdXXkESf?2 )

<a href="/index.php/ticket/easypicks/<?php echo $ticket['ID'];?>">Easy Pick</a>

<?php 
if(isset($_POST['my_picks']))    //if it's $_POSTed
{
$my_picks =$_POST['my_picks'];   //set the variable
}
if(!isset($my_picks)){           //if the variable isn't set
    $my_picks = Picks::model()->find_tickets_by_ID($ticket_ID);
}
print_r($my_picks); ?>           //print out the array

Controller

public function actionUpdate($id)
{
    $model=$this->loadModel($id);
    if(isset($_POST['Ticket']))
    {
        $model->attributes=$_POST['Ticket'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->ID));
    }
    $this->render('update',array(
        'model'=>$model,
    ));
}

public function actionEasypicks($id){
    $model=$this->loadModel($id);

    $my_picks = Ticket::model()->easy_pick(); // returns an array of 16
    //This is where I think my issue is.  I would like to go to page
    // 'localhost/index.php/ticket/update/3' but what I am redirect to is
    // 'localhost/index.php/ticket/easypicks/3'
    // currently this gives me an "Undefined index"
    $url = 'localhost/index.php/ticket/update/3';
    $this->redirect($_POST[$url],$my_picks = $my_picks);
}

Upvotes: 0

Views: 108

Answers (1)

Zombiesplat
Zombiesplat

Reputation: 953

This is better suited to have one action of update and let it handle the special scenario for running easy picks by setting a hidden input.

public function actionUpdate($id)
{
    $model=$this->loadModel($id);

    if(isset($_POST['easy_picks'] && $_POST['easy_picks']){
        $my_picks = Ticket::model()->easy_pick(); // returns an array of 16
        $model->attributes = $my_picks;
        if($model->save())
            $this->redirect(array('view','id'=>$model->ID));
    }

    if(isset($_POST['Ticket']))
    {
        $model->attributes=$_POST['Ticket'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->ID));
    }

    $this->render('update',array(
        'model'=>$model,
    ));
}

Then for your javascript stuff

function easy_picks(){
    var url = '/index.php/ticket/update/'+<?php echo $ticket_ID; ?>;
    $.ajax({
        type: "POST",
        url: url,
        data: { easy_picks : 1},
        success: function(){
            //do something here like a refresh or redirect
        }
    });
}

Upvotes: 1

Related Questions