weixin_33696822 2017-11-21 05:46 采纳率: 0%
浏览 23

Zend Framework中的Ajax Post

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

  • 写回答

1条回答 默认 最新

  • 游.程 2017-11-23 11:16
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题