0

2 つの選択ボックスを持つ ZF フォームがあります。どちらも 2 つの DB テーブルから入力する必要があります。最初の選択ボックスは、フォームが最初にレンダリングされるときに入力されます。(したがって、これは完了し、正常に動作します)次に、ユーザーが値を選択したときに最初の選択ボックスの値を取得して 2 番目の選択ボックスを埋め、それを選択 SQL に渡して 2 番目のデータセットを取得します。

また、ページを更新したくありません。(だから ajax/javascript/jquery)

私は自分の見解(.phtml)で次のことをしています

<script type="text/javascript">

$(document).ready(function(){
$('#make').change(function($e){
    $e.preventDefault();
     var href= "index/load";
     var data = 'make_id='+$('#make').val();
     $.ajax({ type: "POST",
           url: href,
          data: data,
          success: function(response){
            location.href = 'index/load';
         }
     });
});
});

</script>

しかし、コントローラーアクションで次を使用して、ajax投稿から渡された値にアクセスできません

$this->getRequest()->getParams('make_id');
4

2 に答える 2

0

dataajaxリクエストの一部には、次のJSONようなオブジェクトが必要{make_id: something}です。そのため、次の形式でパラメーターを送信する必要があります。

$(document).ready(function(){
  $('#make').change(function($e){
  $e.preventDefault();
   var href= "index/load";
   var data = $('#make').val();
   $.ajax({ type: "POST",
     url: href,
     data: {make_id: data},
     success: function(response){
     location.href = 'index/load';
   }
 });

});

于 2013-02-27T11:36:06.803 に答える
0

OK、それを行う簡単な方法を見つけました。私のビューの phtml には、次のものがあります。

 <body>
 <?php
     $this->form->setAction($this->url());
     echo $this->form;

  ?>

   <script type="text/javascript">

   $(document).ready(function(){
       $('#select1').change(function(){
           $('#Myform').submit();
           return false;
       });
   });

   </script>
</body>

そして、コントローラーのアクションで私は持っています

public function viewAction()
{

        $form= new Application_Form_Myform();
        $selectbox1 = $form->getElement('select1');
        $selectbox1->setMultiOptions($this->populateselectbox1()); //This function fetch data from the db and make an array
        if ($this->getRequest()->getPost('select1')!=""){
            $selectbox2 = $form->getElement('select2');
            $selected = $this->getRequest()->getPost('select1');
            $select->setMultiOptions($this->populatselectbox2($selected)); //This function fetch data from the db and make an array
            $selectbox1->setValue($selected);
        }
        $this->view->form = $form;
    }
于 2013-02-27T12:50:04.397 に答える