Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Zend(1.12.17)和Ajax的动态Dropdoown列表_Php_Mysql_Ajax_Zend Framework_Drop Down Menu - Fatal编程技术网

Php 使用Zend(1.12.17)和Ajax的动态Dropdoown列表

Php 使用Zend(1.12.17)和Ajax的动态Dropdoown列表,php,mysql,ajax,zend-framework,drop-down-menu,Php,Mysql,Ajax,Zend Framework,Drop Down Menu,我是Zend的新手,很难掌握如何使用Ajax动态填充下拉菜单。我创建了一个不使用Zend框架的工作版本,但现在将代码放入Zend比我原来想象的要困难得多。当我尝试运行我的应用程序时,Chrome的开发者工具输出到控制台: GET http://example.com/crm/getcities.php?name=CDS%20Midwinter%20Meeting 404 (Not Found)tradeshowOptions @ tradeshows:423onchange @ tradesho

我是Zend的新手,很难掌握如何使用Ajax动态填充下拉菜单。我创建了一个不使用Zend框架的工作版本,但现在将代码放入Zend比我原来想象的要困难得多。当我尝试运行我的应用程序时,Chrome的开发者工具输出到控制台:

GET http://example.com/crm/getcities.php?name=CDS%20Midwinter%20Meeting 404 (Not Found)tradeshowOptions @ tradeshows:423onchange @ tradeshows:484 
在我的应用程序控制器文件夹中,有一个名为CrmController.php的控制器,如下所示:

<?php
class CrmController extends Zend_Controller_Action {

public function init() {        
    if(!Zend_Auth::getInstance()->getIdentity() || !in_array('crm', unserialize(Zend_Auth::getInstance()->getIdentity()->permission))){
        $this->_helper->redirector('index', 'fm');
    } 
    $this->view->headLink()->appendStylesheet('/public/css/crm.css');
    $this->_auth = Zend_Auth::getInstance()->getIdentity();

    $this->_users = new Application_Model_User;       
    $this->_crms = new Application_Model_Crm;  
    $this->_products = new Application_Model_Product;

    $this->_helper->ajaxContext->addActionContext('crm', 'html')
                                   ->initContext();
}

public function tradeshowsAction()
{    
}

销售客户关系管理
  • 展会
  • 展会 var name=“”; var city=“”; var year=“”; //获取要填充第二个下拉列表的城市列表。 功能tradeshowOptions(tsname){ name=tsname; 如果(tsname==“”){ document.getElementById(“txtHint”).innerHTML=“”; 返回; }否则{ if(window.XMLHttpRequest){ //IE7+、Firefox、Chrome、Opera、Safari的代码 xmlhttp=新的XMLHttpRequest(); }否则{ //IE6、IE5的代码 xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText; } }; open(“GET”、“getcities.php?name=“+name,true”); xmlhttp.send(); name=tsname; console.log(名称); } } //获取要填充第三个下拉列表的城市列表。 功能选项(tscity){ 城市=城市; 如果(tscity==“”){ document.getElementById(“txtHint2”).innerHTML=“”; 返回; }否则{ if(window.XMLHttpRequest){ //IE7+、Firefox、Chrome、Opera、Safari的代码 xmlhttp=新的XMLHttpRequest(); }否则{ //IE6、IE5的代码 xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ document.getElementById(“txtHint2”).innerHTML=xmlhttp.responseText; } }; open(“GET”、“GET_year.php?name=“+name+”&city=“+city,true”); xmlhttp.send(); 城市=城市; console.log(名称+“”+城市); } } //获取数据列表。 函数数据集(tsyear){ 年=年; 如果(tsyear==“”){ document.getElementById(“txtHint3”).innerHTML=“”; 返回; }否则{ if(window.XMLHttpRequest){ //IE7+、Firefox、Chrome、Opera、Safari的代码 xmlhttp=新的XMLHttpRequest(); }否则{ //IE6、IE5的代码 xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ document.getElementById(“txtHint3”).innerHTML=xmlhttp.responseText; } }; open(“GET”、“GET_data.php?name=“+name+”&city=“+city+”&year=“+year,true”); xmlhttp.send(); console.log(名称+城市+年份); } } 选择贸易展: 选择一个展销会:

    您可以考虑查看MVC模式指南,ZEngFramework是围绕:./P> 我建议您理解MVC,否则您将与框架抗争,将代码塞进它不希望的地方。例如,getcities.php是一个应该分离为模型类、视图脚本和控制器操作的组合。您不知道将此文件放在何处,因为它实际上不属于您的应用程序

    以下是我将采取的方法。。。首先,让我们创建一个示例控制器,用于响应城市列表的AJAX请求:
    /**
     * Model class to represent cities and interact with database.
     *
     */
    class Application_Model_Cities extends Zend_Db_Table_Abstract
    {
        // Tell the class what table to use.
        protected $_name = 'tradeshows';
    
        public function getCities($name = false)
        {
            if($name === false){
              // If no name supplied, match all.
              $name = '%';
            }
            $sql = 'SELECT * FROM tradeshows WHERE name LIKE ? GROUP BY city ORDER BY city asc';
            return $this->getAdapter()->fetchAll($sql,$name);
        }
    }
    

    将模型类放在应用程序的“models”目录中。看起来您当前正在config.php中手动连接数据库。您应该考虑引导与ZANDB数据库连接。我的示例模型依赖于默认数据库连接:

    您可以通过URL调用上述代码,如下所示:您应该看到一个以JSON格式显示的城市列表

    我认为这足以让你找到正确的方向。不要在生产中使用这段代码,为了本例的目的,我采用了一些快捷方式来简化工作

    <!DOCTYPE html>
    <html>
    <head></head>
    <body>
    <form>  
        Select City:
        <select name="cities" onchange="yearOptions(this.value)">
                <option>Please select a city...</option>
                <?php
                    // Database Connection
                    require('config.php');
    
                    // Global Variables from _GET
                    $name = strval($_GET['name']);
    
                    // MySQL Query
                  $sql = "SELECT * FROM tradeshows WHERE name LIKE '$name' GROUP BY city ORDER BY city asc;";
    
                    // Output Each MySQL Result in Dropdown Menu
                  $i = 0;
                    foreach ($conn->query($sql) as $row) {
                        echo "<option value='$row[city]'>$row[city]</option>";
                        $i++;
                  }
                ?>
        </select>
    </form>
    </body>
    </html>
    
    <?php
    class NewController extends Zend_Controller_Action
    {
    
        public function init()
        {
            $contextSwitch = $this->_helper->getHelper('contextSwitch');
    
            // The helper will detect a request for JSON and setup the getcities
            // action to return JSON automatically.  No view script required.
            $contextSwitch->addActionContext('getcities', 'json')->initContext();
        }
    
        public function getcitiesAction()
        {
            // Instantiate cities model.
            $cities_model = new Application_Model_Cities();
    
            // Get list of cities by simply getting all records.
            $cities_list = $cities_model->getCities();
    
            // JSON helper will emit all view variables in JSON format.
            $this->view->cities_list = $cities_list;
    
        }
    }
    
    /**
     * Model class to represent cities and interact with database.
     *
     */
    class Application_Model_Cities extends Zend_Db_Table_Abstract
    {
        // Tell the class what table to use.
        protected $_name = 'tradeshows';
    
        public function getCities($name = false)
        {
            if($name === false){
              // If no name supplied, match all.
              $name = '%';
            }
            $sql = 'SELECT * FROM tradeshows WHERE name LIKE ? GROUP BY city ORDER BY city asc';
            return $this->getAdapter()->fetchAll($sql,$name);
        }
    }