我是zend的新手,我正在尝试使用post方法从网格中删除数据,但是当我点击“是”按钮时,它就像get方法一样工作。该网址显示了此[http://project.com/client/delete/id/10?id=10&del=Yes]。请有人帮帮我。
这是我的控制器代码。
public function deleteAction()
{
if($this->getRequest()->isPost())
{
$del = $this->getRequest()->getPost('del');
print_r($del);die;
if($del=='Yes')
{
$id =$this->getRequest()->getPost('id');
$client = new Application_Model_DbTable_Client();
$id = $this->getRequest()->getparam('id');
$client->deleteClient($id);
}
我的delete.phtml代码
您确定要删除
吗?<?php foreach($this->client as $clients): ?>
'<?php echo $this->escape($clients['firstname']); ?>'
'<?php echo $this->escape($clients['lastname']); ?>'
'<?php echo $this->escape($clients['email']); ?>'
</p>
<?php endforeach; ?>
<form action="<?php echo $this->url(array('action'=>'delete')); ?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $this->escape($clients["Id"]); ?>"/>
<input type="submit" name="del" value="Yes" />
<input type="submit" name="del" value="No" />
</div>
</form>
表
array(7) {
[0]=>
array(4) {
["Id"]=>
string(1) "1"
["firstname"]=>
string(5) "tuhin"
["lastname"]=>
string(6) "biswas"
["email"]=>
string(15) "tuhin@gmail.com"
}
[1]=>
array(4) {
["Id"]=>
string(1) "2"
["firstname"]=>
string(5) "Ankur"
["lastname"]=>
string(7) "raiyani"
["email"]=>
string(15) "Ankur@gmail.com"
}
[2]=>
array(4) {
["Id"]=>
string(1) "5"
["firstname"]=>
string(5) "Tejas"
["lastname"]=>
string(5) "Patel"
["email"]=>
string(15) "tejas@gmail.com"
}
[3]=>
array(4) {
["Id"]=>
string(1) "6"
["firstname"]=>
string(6) "Ranjan"
["lastname"]=>
string(5) "sahoo"
["email"]=>
string(16) "ranjan@gmail.com"
}
[4]=>
array(4) {
["Id"]=>
string(1) "7"
["firstname"]=>
string(5) "Mansi"
["lastname"]=>
string(5) "Joshi"
["email"]=>
string(15) "mansi@gmail.com"
}
[5]=>
array(4) {
["Id"]=>
string(1) "8"
["firstname"]=>
string(5) "tuhin"
["lastname"]=>
string(6) "biswas"
["email"]=>
string(15) "tuhin@gmail.com"
答案 0 :(得分:0)
我认为问题在于你将id
放在哪里$id = $this->getRequest()->getparam('id');
$id =$this->getRequest()->getPost('id');
如果条件有不同的方式,你将在一个变量中分配两次变量。
我试图修改它可能适合您的代码,这是未经测试的。 请尝试以下方法: -
控制器中的
public function deleteAction() {
if($this->getRequest()->isPost()){
$del = $this->getRequest()->getPost('del');
if($del=='Yes')
{
$id =$this->getRequest()->getPost('Id');
$client = new Application_Model_DbTable_Client();
$client->deleteClient($id);
}
exit();
} else {
$id = $this->_getParam('Id', 0);
$client = new Application_Model_DbTable_Client();
//render the id to the view
$this->view->clientId = $client->getClientId($id);
}
}
您的Model Dbtable客户端类。添加此方法。
/*In your Db Client class Table*/
public function getClientId($id)
{
$id = (int)$id; //cast to integer.
//here the id in your client table
//change it to fit it your needs
$row = $this->fetchRow('Id = ' .$id);
if(!$row)
{
throw new Exception("Could not find this $id");
}
return $row->toArray();
}
并在您的ViewHTml中 你确定要删除吗?
<?php foreach ($this->client as $clients) {
echo $this->escape($clients->firstname);
echo $this->escape($clients->lastname);
echo $this->escape($clients->email);
}?>
<form action="<?php echo $this->url(array('action'=>'delete')); ?>" method="post">
<input type="hidden" name="Id" value="<?php echo $this->clientId['Id']; ?>" />
<input type="submit"name="del" value="Yes" />
<input type="submit" name="del" value="No" />
</form>