我目前正在开发一个Yii框架项目,我想创建一个对用户来说很容易选择的按钮。基本上,他们按下按钮,onclick调用一个动作,动作设置一个数组和$ _POST数组到当前页面(刷新页面) 该页面将使用该数组相应地加载。
任何帮助都会很好。如果我这样做并且有更好的方法,请告诉我。
查看完整视图(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
控制器
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);
}
答案 0 :(得分:1)
这更适合有一个更新操作,让它通过设置隐藏输入来处理运行简单选择的特殊场景。
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,
));
}
然后为你的javascript东西
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
}
});
}