I'm very very new in Zend Framework. I wanted delete a row in db via $.post in jQuery that was not done. I also did a lot of research on Google and Youtube, but I did not get result, Unfortunately. Please help me. ZF version: 1.11.2
application/Bootstrap.php:
protected function _initDb(){
$con=array('host'=>'127.0.0.1','username'=>'root','password'=>'','dbname'=>'sample_db');
$db=Zend_Db::factory('Pdo_Mysql',$con);$db->query("SET NAMES 'utf8'");
Zend_Registry::set('db',$db);
}
application/controllers/DashboardController.php:
public function indexAction(){
$this->_helper->layout->setLayout('a');
}
application/views/scripts/dashboard/index.phtml:
<a href="javascript:deleteTest('62989c12369ea3c1')">DELETE</a>
public/js/0.js:
function deleteTest(id){
if(confirm('Are you sure?'))
$.post('http://127.0.0.1/Sample4/application/models/Guestdb.php',{funcName:'Delete_Test',id:id},function(r){alert(r)})
}
application/models/Guestdb.php:
class Model_Guestdb{
public function Delete_Test(){
$db=Zend_Registry::get('db');
$r=$db->query("DELETE FROM `prac` WHERE `id`='".trim((new Zend_Filter_Decrypt(array('adapter'=>'mcrypt','key'=>'thisisakeytolock','vector'=>'myvector')))->filter(hex2bin($this->getRequest()->getPost()['id'])))."'");
echo$r?'t':'f';
}
}
$a=new Model_Guestdb();
if(isset($_POST['funcName']))call_user_func(array($a,$_POST['funcName']));
elseif(isset($_GET['funcName']))call_user_func(array($a,$_GET['funcName']));
Output:
Fatal error: Uncaught Error: Class 'Zend_Registry' not found in C:\xampp\htdocs\Sample4\application\models\Guestdb.php:6 Stack trace: #0 C:\xampp\htdocs\Sample4\application\models\Guestdb.php(35): Model_Guestdb->Delete_Test() #1 {main} thrown in C:\xampp\htdocs\Sample4\application\models\Guestdb.php on line 6
Sorry for my english
Please help me
Thanks in advance
I solved this problem as follows:
application/views/scripts/dashboard/index.phtml:
<a href="javascript:del('210')">DELETE</a>
public/js/0.js:
function del(id){
if(confirm('Are you sure?'))
$.post('ajax',{func:'del',table:'prac',id:id},
function(r){r=='t'?location.reload():alert('Error!')})
}
application/controllers/DashboardController.php:
<?php
class DashboardController extends Zend_Controller_Action{
public function ajaxAction(){
$this->_helper->layout->disableLayout();
$ajax=$this->getRequest()->getPost();$func=$ajax['func'];
(new Model_Guestdb)->$func($ajax['table'],$ajax['id']);
}
}
application/models/Guestdb.php:
class Model_Guestdb{
public function del($table,$id){
echo Zend_Registry::get('db')->query
("DELETE FROM `$table` WHERE `id`='$id'")?'t':'f';
}
}
Thanks for friends comments.