こんにちは、時間割システムを作っている初心者です。
やりたいのは、学部セレクトボックスを選択するとその学部に属している学科をDBから読み取り、セレクトボックスに表示したいです。
以下のページを参照して試してみました。
https://qiita.com/katsuakikon/items/7a8fcaf85b342cb61bcc
ソースコードは以下のようです。
PHP
1 2//ParticipantController.phpの中に次のようなFunctionを定義しました。 3 4 public function getOptions($id = null) 5 { 6 $this->autoRender = false; 7 $this->viewBuilder()->setLayout(false); 8 9 $courses = $this->Participant->Course->getOptions($id); // アソシエーションされてます。 10 $this->set('courses',$courses); 11 }
PHP
1 public function getOptions($id = null) 2 { 3 // デフォルト値 4 $result = ["" => "選択してください"]; 5 6 if(!empty($id)) 7 { 8 $result = $this->find('list', ['keyField' => 'course_idx', 'valueField' => 'course_name']) 9 ->where(['section_idx' => $id]) 10 ->andWhere(['is_used' => 1]) 11 //->toArray(); 12 } 13 return $result; 14 }
HTML
1 <div class="form-group"> 2 <div class="col-lg-12"> 3 <?= $this->Form->label('学部'); ?> 4 <?php 5 echo $this->Form->select('section_idx', 6 ['options' => $sections], 7 ['empty' => '(学部を選択してください。)','class'=>'form-group','id' => "section_idx"]); 8 ?> 9 </div> 10 </div> 11 12 <div class="form-group"> 13 <div class="col-lg-12"> 14 <?= $this->Form->label('学科'); ?> 15 <?php 16 if(isset($courses)) 17 { 18 echo $this->Form->select('course_idx', 19 ['options' => $courses], 20 ['empty' => '(学科を選択してください。)','class'=>'form-group','id' => "course_idx"] 21 ); 22 } 23 else 24 { 25 echo $this->Form->select('course_idx', 26 ['options' => ''], 27 ['empty' => '(学科を選択してください。)','class'=>'form-group','id' => "course_idx"] 28 ); 29 } 30 ?> 31 </div> 32 </div> 33 34<script> 35$("#section_idx").bind("change keyup", function() { 36 var id = $("#section_idx").val(); 37 $("#course_idx").load( "./getOptions/"+id); 38}); 39</script> 40
すると、学部セレクトボックスが変わると学科セレクトボックスには動きはありますが、データが何も表示されません。セレクトボックスを押してみるとスクリーンショットのように開かれます。(下に何かありそうですが下が切れてしまいます。)
DebugToolBarのHistory(XHRのGet)で確認すると、成功的にSQLも実行され、$coursesの値も問題なく入っていますが、実行後のページには$coursesもなく、何も表示されません。
何が問題なのかぜんぜん気が付かないし、データは問題なく値に入ったので表示させたいのですが上手くできませんでした。
何かアドバイスいただければと思います。
よろしくお願いいたします。
「追加」Logに残っているWarningメッセージがありました。以下のようです。
2017-11-01 11:47:21 Notice: Notice (8): Object of class Cake\ORM\Query could not be converted to int in [D:\Bitnami\wampstack-7.1.7-0\apache2\htdocs\djks\trunk\src\Model\Table\CourseTable.php, line 166]
Request URL: /participant/getOptions/4
Referer URL: http://localhost:8765/participant/add
017-11-01 11:47:21 Error: [Error] Unsupported operand types in D:\Bitnami\wampstack-7.1.7-0\apache2\htdocs\djks\trunk\src\Model\Table\CourseTable.php on line 166
Request URL: /participant/getOptions/4
Referer URL: http://localhost:8765/participant/add
「追加」上の2つのエラーを解決しましたが、まだ表示されません。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。