I'm completely new to Zend and am having a hard time grasping how to use Ajax to dynamically populate a dropdown menu. I created a working version that didn't use the Zend framework, but now putting my code into Zend is proving much more difficult than I had originally thought. When I try to run my application, Chrome's Developer Tools outputs to console:
GET http://example.com/crm/getcities.php?name=CDS%20Midwinter%20Meeting 404 (Not Found)tradeshowOptions @ tradeshows:423onchange @ tradeshows:484
In my application controller folder, I have a controller called CrmController.php which looks like this:
<?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()
{
}
I have a view entitled tradeshows.phtml which looks like:
<?php require_once('config.php'); ?>
<div class="row">
<div class="col-lg-12">
<h2 class="page-header">Sales CRM</h2>
<ol class="breadcrumb">
<li><a href="/crm/prospect">Leads</a></li>
<li><a href="/crm/prospect">Prospects</a></li>
<li><a href="/crm/accounts">Accounts</a></li>
<li><a href="/crm/activity">Activities</a></li>
<li class="active">Tradeshows</li>
</ol>
</div>
</div>
<h3>Tradeshows</h3>
<script>
var name = "";
var city = "";
var year = "";
// Gets the list of cities to populate the second drop down.
function tradeshowOptions(tsname) {
name = tsname;
if (tsname == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","getcities.php?name="+name,true);
xmlhttp.send();
name = tsname;
console.log(name);
}
}
// Get the list of cities to populate the third drop down.
function yearOptions(tscity) {
city = tscity;
if (tscity == "") {
document.getElementById("txtHint2").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint2").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","get_year.php?name="+name+"&city="+city,true);
xmlhttp.send();
city = tscity;
console.log(name + " " + city);
}
}
// Get the data list.
function dataSet(tsyear) {
year = tsyear;
if (tsyear == "") {
document.getElementById("txtHint3").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint3").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","get_data.php?name="+name+"&city="+city+"&year="+year,true);
xmlhttp.send();
console.log(name + " " + city + " " + year);
}
}
</script>
<form>
Select Tradeshow:
<select name="shows" onchange="tradeshowOptions(this.value)">
<option value="">Select a Tradeshow:</option>
<?php
$sql = 'SELECT * FROM tradeshows GROUP BY name ORDER BY name asc;';
$i = 0;
foreach ($conn->query($sql) as $row) {
echo "<option value='$row[name]'>$row[name]</option>";
$i++;
}
?>
</select>
</form>
<br>
<div id="txtHint"> </div>
<div id="txtHint2"> </div>
<div id="txtHint3"> </div>
<link href='/public/css/crm.css' rel='stylesheet' />
Last but not least, I have my getcities.php file, which I do not know where to place. I've also tried naming it getcities.ajax.phtml and putting it in the views, but nothing seems to work.
<!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>
Please feel free to rip apart my code. I would really like to learn. Also, I realize my code is not that DRY, and I would appreciate it if someone could offer some recommendations.
Thank you!