Zend Framework中的Ajax Post


                    

我在Zend Framework中非常新。
我想通过jQuery中的$ .post删除db中的一行,但未完成。
我在Google和Youtube上也做了很多研究,但不幸的是我没有得到结果。
请帮我。
ZF版本:1.11.2 </ p>

application / Bootstrap.php:</ p>

 受保护的函数_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);
}
</ code> </ pre>

application / controllers / DashboardController.php:</ p>

 公共函数indexAction(){
     $ this-> _ helper-> layout-> setLayout('a');
}
</ code> </ pre>

application / views / scripts / dashboard / index.phtml:</ p>

  删除
</ code> </ pre>

public / js / 0.js:</ p>

 函数deleteTest(id){
     如果(确认('确定吗?'))
         $ .post('http://127.0.0.1/Sample4/application/models/Guestdb.php',{funcName:'Delete_Test',id:id},function(r){alert(r)})
}
</ code> </ pre>

application / models / Guestdb.php:</ p>

  Model_Guestdb类{
     公共功能Delete_Test(){
     $ db = Zend_Registry :: get('db');
         $ r = $ db-> query(“从prac那里删除'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'])));
</ code> </ pre>

输出:</ p>

 致命错误:未捕获错误:在C:\ xampp \ htdocs \ Sample4 \ application \ models \ Guestdb.php:6中找不到类'Zend_Registry'堆栈跟踪:#0 C:\ xampp \ htdocs \ Sample4  \ application \ models \ Guestdb.php(35):Model_Guestdb-> Delete_Test()#1 {main}抛出在第6行的C:\ xampp \ htdocs \ Sample4 \ application \ models \ Guestdb.php中
</ code> </ pre>

抱歉我的英语

请帮助我

预先感谢</ p>
     </ div>

展开原文

原文

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个回答


我如下解决了这个问题:</ p>

application / views / scripts / dashboard / index.phtml:</ p>

  删除
</ code> </ pre>

public / js / 0.js:</ p>

  function del(id){
     如果(确认('确定吗?'))
         $ .post('ajax',{func:'del',table:'prac',id:id},
             function(r){r =='t'?location.reload():alert('Error!')})
}
</ code> </ pre>

application / controllers / DashboardController.php:</ p>

  <?php
DashboardController类扩展了Zend_Controller_Action {
     公共功能ajaxAction(){
         $ this-> _ helper-> layout-> disableLayout();
         $ ajax = $ this-> getRequest()-> getPost(); $ func = $ ajax ['func'];
         (新Model_Guestdb)-> $ func($ ajax ['table'],$ ajax ['id']);
     }
}
</ code> </ pre>

application / models / Guestdb.php:</ p>

  Model_Guestdb类{
     公共功能del($ table,$ id){
         回声Zend_Registry :: get('db')-> query
         (“从'$ table那里删除,'id ='$ id''))?'t':'f';
     }
}
</ code> </ pre>

感谢朋友的评论。</ p>
     </ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问