Skip to content

Instantly share code, notes, and snippets.

@sugarHoge
Created June 13, 2013 09:14
Show Gist options
  • Save sugarHoge/5772347 to your computer and use it in GitHub Desktop.
Save sugarHoge/5772347 to your computer and use it in GitHub Desktop.
連動プルダウン
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
function changeSelect(){
$.ajax({
type: "POST",
url: "<?php echo $this->url; ?>/ajax",
dataType: 'json',
data:"hoge="+$("#hoge").val(),
error: function(){
alert("error");
},
success: function(data){
$("#select").empty();
for(var i in data){
$("#select").append("<option value='" + data[i] + "'>" + data[i] + "</option>");
}
}
});
}
</script>
</head>
<body>
<form name="form1" method="post">
<select id="hoge" name="hoge" onchange="changeSelect()">
<option value=""></option>
<option value="token1">token1</option>
<option value="token2">token2</option>
<option value="token3">token3</option>
</select>
<select id="select">
</select>
</form>
</body>
</html>
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
$req = $this->getRequest();
$this->url = $req->getBaseUrl()."/".$req->getModuleName()."/".$req->getControllerName();
$this->view->assign('url', $this->url);
}
public function indexAction()
{
// action body
}
public function ajaxAction()
{
//ビューを使わない
$this->_helper->viewRenderer->setNoRender();
$req = $this->getRequest();
$arr = array(
'token1'=>array(
'token_1111111111', 'token_2222222222', 'token_3333333333'
),
'token2'=>array(
'token_aaaaaaaaaa', 'token_bbbbbbbbbb', 'token_cccccccccc'
),
'token3'=>array(
'token_あああああ', 'token_いいいいい', 'token_えええええ'
)
);
echo Zend_Json::encode($arr[$req->getParam('hoge')]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment