Ajax 如何让按钮触发ATK4中的数据库更新?
我正在使用ATK4.1.1,并在页面上添加了一个按钮。我希望这个按钮在按下时触发对数据库表的更新,所以我在页面中创建了这样的代码Ajax 如何让按钮触发ATK4中的数据库更新?,ajax,atk4,Ajax,Atk4,我正在使用ATK4.1.1,并在页面上添加了一个按钮。我希望这个按钮在按下时触发对数据库表的更新,所以我在页面中创建了这样的代码 $f=$p->add('Form',null,null,array('form_empty'))->setFormClass('horizontal bottom-padded'); $b1=$f->add('Button')->set('Finished'); $b1->js("click")->univ
$f=$p->add('Form',null,null,array('form_empty'))->setFormClass('horizontal bottom-padded');
$b1=$f->add('Button')->set('Finished');
$b1->js("click")->univ()->ajaxec($this->api->url(null, array("s"=>"D")));
$b1->js('click', $b1->js()->load(
$this->api->url(null,array('s'=>'D'))
) );
.. .. .. ..
if (isset($_GET['s'])) {
$ss=$this->add('Model_SprintStatus')
->loadBy(array('member_id'=>$p->api->auth->get('id')));
$new_status=$_GET['s'];
$ss->set('status',$new_status);
$ss->update();
}
当我访问此页面时,它显示ok,但当单击按钮时,我会收到一个错误消息method
BaseException
Method is not defined for this object
Additional information:
method: url
arguments: Array ( [0] => [1] => Array ( [s] => D )
我使用了下面一节中名为“重新加载的剖析”的示例。当我遇到这个错误时,我以这个示例为例,使用与示例相同的代码创建了一个新页面,并且从该页面中也得到了一个类似的错误
BaseException
Method is not defined for this object
Additional information:
method: url
arguments: Array ( [0] => [1] => Array ( [side] => 1 [myajax] => 1 ) )
除了尝试上述ajaxec系列,我还尝试了以下产品
$b1->js('click', $b1->js()->load( $this->api->url(null,array('s'=>'D'))));
及
但他们也犯了同样的错误
也许我错过了一些东西,或者可能是ATK4.1.1和4.2之间发生了变化,但我现在无法升级,因为我试图在截止日期前完成升级,那么我必须用什么方法从ATK4.1.1中的按钮单击执行此更新
谢谢,答案是
// create a form to put the button in with minimal styling
$f=$p->add('Form',null,null,array('form_empty'))->setFormClass('horizontal bottom-padded');
// Add the button with a confirm request and do an ajax call
// passing a new GET parameter called state
$f->add('Button')->set('Update')
->js('click')->univ()->confirm('Confirm ?')
->ajaxec($this->api->getDestinationURL(null,array('state'=>'D')));
// If the parameter is set, this is executed by the ajax callback
if (isset($_GET['state'])) {
//add the model (change name to yours)
// You can either use load($id) if you already have the primary key
// or an array of fields and use loadBy to get the records to be updated
$ss=$this->add('Model_SprintStatus')->loadBy(array(
'member_id'=>$p->api->auth->get('id')
)
);
// set some variables to hold the old status from the model
// and the new one which was in the GET parameter we passed
$old_status=$ss->get('status');
$new_status=$_GET['state'];
// Set the status column to the new value and execute the update
$ss->set('status',$new_status);
$ss->update();
// and provide some feedback to the user that it worked !
$this->js()->univ()->successMessage('Updated')
->page($this->api->getDestinationURL())->execute();
}
如果您不想向用户提供任何反馈,您可以省略按钮定义上的->确认()和包含成功消息的整行,并且您可能还可以通过在If(isset($\u GET…中更改按钮的类来更改按钮的状态。有一种更简单的方法:
if($this->add('Button')->setLabel('Clickme')->isClicked()){
// ....
$this->js()->univ()->successMessage('Updated')
->page($this->api->getDestinationURL())->execute();
}
这只是在中简要记录的。你能确认addButton上的语法吗?在4.1中,我得到一个错误,说这个对象addButton没有定义方法。好的-为addButton制定了正确的语法,并修改了上面的内容,但是现在当按下按钮时出现ajax错误,没有关于原因的信息-有什么建议吗帖子URL是
if($this->add('Button')->setLabel('Clickme')->isClicked()){
// ....
$this->js()->univ()->successMessage('Updated')
->page($this->api->getDestinationURL())->execute();
}