2段階プルダウンで表示内容を切り替えたい
2段階プルダウンで表示内容を切り替えたい
2段階プルダウンで1段階目の選択を終えた後に選択ボックスを見えなくしたい
goo: https://oshiete.goo.ne.jp/qa/13030027.html
okwave:https://okwave.jp/qa/q10026854.html
無駄に答えてしまった。phpのプログラムは評価しづらい。
<!DOCTYPE html> <meta charset="utf-8"> <title></title> <style> </style> <body> <p> <select id="Y"></select> / <select id="M"></select> / <select id="D"></select> </p> <table border="1" id="T"></table> <script> async function ajax (path, args = { }, cbFunc = null, ...cbArgs) { let res = await fetch (path, { method: 'post', body: JSON.stringify (args) }); let txt = await res.text (); let json = JSON.parse(txt); return 'function' === typeof cbFunc ? cbFunc.call (...cbArgs): json; } function ary2tbody (a,b=document.createElement('tbody')){a.reduce((b,a)=>(a.reduce((c,d)=>(c.insertCell().append(d),c),b.insertRow()),b),b)} function replaceOptions (e, ary) {e.replaceChildren (...ary.map (a=> new Option (...a)))} function replaceY (y=(new Date).getFullYear(),n=10) {replaceOptions (Y,[...Array(n)].map ((_,i)=>[y-i]))} function replaceM () {replaceOptions (M,[...Array(12)].map ((_,i)=>[i+1]))} function replaceD (y=Y.value,m=M.value) { replaceOptions (D,[...Array((new Date (y,m,0)).getDate())].map ((_,i)=>[i+1]))} async function loadContent (y=Y.value, m=M.value, d=D.value) { ary2tbody(await ajax ('test.php', {date: `${y}-${m}-${d}`}), T); } function handler (event) { let e = event.target; switch (true) { case M == e : replaceD (); break; case D == e : loadContent (); break; } } document.addEventListener ('change', handler, false); [replaceY,replaceM,replaceD].map(f=>f()); </script>
test.php
<?php const MySQL_DBNAME = '********'; const MySQL_HOST = 'localhost'; const MySQL_USER = '********'; const MySQL_PASS = '********'; $db = sprintf ('mysql:dbname=%s; host=%s; charset=utf8mb4', MySQL_DBNAME, MySQL_HOST); $mysql = new PDO ($db, MySQL_USER, MySQL_PASS); $query = 'select * from table_name where :date < date';//ここは適当に $args = json_decode (file_get_contents ('php://input'), true); $hd = $mysql-> prepare ($query); $hd-> execute ($args); $result = $hd-> fetchAll ( PDO::FETCH_NUM); header ('Content-type: application/json; charset=utf-8'); header ('X-Content-Type-Options: nosniff'); echo json_encode ($result);//JSONに変換する ?>