質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

button

HTMLで用いる<button>タグです。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

4回答

284閲覧

HTML・PHPでradiobuttonの値をPOSTしてPOST先でボタンの値をcheckedにしたい

kai37

総合スコア12

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

button

HTMLで用いる<button>タグです。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2018/12/14 01:42

前提・実現したいこと

フォームからradiobuttonの値をPOSTして受け取った先で「このフォームの値で登録しますか?」の確認画面を出すのですが
そのまま登録する分には問題ないのですが、編集と押したら入力していたフォームに値をPOSTでいれなおして編集をかける方法を考えています。

発生している問題・エラーメッセージ

値はしっかり受け取れているのですが、ボタンをcheckedにするにはif文などで1ならcheckedにするみたいなことをたくさん書けばいいのでしょうか?
もとスマートなやり方があれば教えてほしいです。

該当のソースコード

health_use_register.php メインのフォームです

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>保健室利用メモフォーム</title> </head> <body> <h3>保健室利用メモ</h3> <form action="health_use_register_check.php" method="post"> <?php $dsn = 'mysql:dbname=CareSupport;host=localhost;charset=utf8mb4'; $user = 'root'; $password = 'rootg10'; $date = filter_input(INPUT_POST,'date'); $student_num = filter_input(INPUT_POST,'student_num'); $student_name = filter_input(INPUT_POST,'student_name'); $student_teacher = filter_input(INPUT_POST,'student_teacher'); $symptom = filter_input(INPUT_POST,'symptom'); $symptom_time = filter_input(INPUT_POST,'symptom_time'); $symptom_cause = filter_input(INPUT_POST,'symptom_cause'); $taiou = filter_input(INPUT_POST,'taiou'); $symptom_fix = filter_input(INPUT_POST,'symptom_fix'); $memo = filter_input(INPUT_POST,'memo'); $symptom_type = filter_input(INPUT_POST,'symptom_type'); $entry_time = filter_input(INPUT_POST,'entry_time'); $exit_time = filter_input(INPUT_POST,'exit_time'); $staff = filter_input(INPUT_POST,'staff'); ?> <table> <tr> <th>日付:</th> <td><input type="date" id="today" name="date" value="<?php echo $date; ?>"></td> </tr> <tr> <th>入室時間:</th> <td><input type="time" id="time" name="entry_time"></td> </tr> <tr> <th>退出時間:</th> <td><input type="time" id="time" name="exit_time" value="<?php echo $exit_time; ?>"></td> </tr> <tr> <th>学籍番号:</th> <td><input type="text" name="student_num" placeholder="16XX0101" value="<?php echo $student_num; ?>"></td> </tr> <tr> <th>氏名:</th> <td><input type="text" name="student_name" placeholder="電子太郎" value="<?php echo $student_name; ?>"></td> </tr> <tr> <th>性別:</th> <td> <input type="radio" name="gender" value="1">男 <input type="radio" name="gender" value="2">女 </td> </tr> <tr> <th>担任:</th> <td><input type="text" name="student_teacher" value="<?php echo $student_teacher; ?>"></td> </tr> <tr> <th>症状:</th> <td><input type="text" name="symptom" value="<?php echo $symptom; ?>"></td> </tr> <tr> <th>いつから:</th> <td><input type="text" name="symptom_time" value="<?php echo $symptom_time; ?>"></td> </tr> <tr> <th>原因は?:</th> <td><input type="text" name="symptom_cause" value="<?php echo $symptom_cause; ?>"></td> </tr> <tr> <th>対応:</th> <td><input type="text" name="taiou" value="<?php echo $taiou; ?>"></td> </tr> <tr> <th>処置:</th> <td><input type="text" name="symptom_fix" value="<?php echo $symptom_fix ?>"></td> </tr> <tr> <th>症状種別:</th> <td> <input type="radio" name="symptom_type" value="外科">外科 <input type="radio" name="symptom_type" value="内科">内科 <input type="radio" name="symptom_type" value="その他">その他</td> </tr> <tr> <th>メモ:</th> <td><textarea name="memo" value="<?php echo $memo; ?>"></textarea></td> </tr> <tr> <th>対応者:</th> <td><input type="text" name="staff" value="<?php echo $staff; ?>"></td> </tr> </table> <input type="submit" value="確定"> </form> </body> </html>

ここにPOSTして編集ボタンが押されたらメインのフォームに戻してます
health_use_registar_check.php

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>保健室利用メモ確認</title> </head> <body> <?php //session_start(); $date = $_POST['date']; $student_num = $_POST['student_num']; $student_name = $_POST['student_name']; $student_teacher = $_POST['student_teacher']; $symptom = $_POST['symptom']; $symptom_time = $_POST['symptom_time']; $symptom_cause = $_POST['symptom_cause']; $taiou = $_POST['taiou']; $symptom_fix = $_POST['symptom_fix']; $memo = $_POST['memo']; $symptom_type = $_POST['symptom_type']; $entry_time = $_POST['entry_time']; $exit_time = $_POST['exit_time']; $staff = $_POST['staff']; $gender = $_POST['gender']; ?> <table> <tr> <th>日付:</th> <td><?php echo $date; ?></td> </tr> <tr> <th>入室時間:</th> <td><?php echo $entry_time; ?></td> </tr> <tr> <th>退出時間:</th> <td><?php echo $exit_time; ?></td> </tr> <tr> <th>学籍番号:</th> <td><?php echo $student_num; ?></td> </tr> <tr> <th>氏名:</th> <td><?php echo $student_name; ?></td> </tr> <tr> <th>性別:</th> <td><?php echo $gender; ?></td> </tr> <tr> <th>担任:</th> <td><?php echo $student_teacher; ?></td> </tr> <tr> <th>症状:</th> <td><?php echo $symptom; ?></td> </tr> <tr> <th>いつから:</th> <td><?php echo $symptom_time; ?></td> </tr> <tr> <th>原因は?:</th> <td><?php echo $symptom_cause; ?></td> </tr> <tr> <th>対応:</th> <td><?php echo $taiou; ?></td> </tr> <tr> <th>処置:</th> <td><?php echo $symptom_fix; ?></td> </tr> <tr> <th>症状種別:</th> <td><?php echo $symptom_type; ?></td> </tr> <tr> <th>メモ:</th> <td><?php echo $memo; ?></textarea></td> </tr> <tr> <th>対応者:</th> <td><?php echo $staff; ?></td> </tr> </table> <p>こちらの内容で登録しますか?</p> <form action="health_use_register_insert.php" method="post"> <input type="hidden" id="today" name="date" value="<?php echo $date; ?>"> <input type="hidden" id="time" name="entry_time" value="<?php echo $entry_time; ?>"> <input type="hidden" name="exit_time" value="<?php echo $exit_time; ?>"> <input type="hidden" name="student_num" value="<?php echo $student_num; ?>"> <input type="hidden" name="student_name" value="<?php echo $student_name; ?>"> <input type="hidden" name="gender" value="1" value="<?php echo $gender; ?>"> <input type="hidden" name="student_teacher" value="<?php echo $student_teacher; ?>"> <input type="hidden" name="symptom" value="<?php echo $symptom; ?>"> <input type="hidden" name="symptom_time" value="<?php echo $symptom_time; ?>"> <input type="hidden" name="symptom_cause" value="<?php echo $symptom_cause; ?>"> <input type="hidden" name="taiou" value="<?php echo $taiou; ?>"> <input type="hidden" name="symptom_fix" value="<?php echo $symptom_fix ?>"> <input type="hidden" name="symptom_type" value="<?php echo $symptom_type; ?>"> <input type="hidden" name="memo" value="<?php echo $memo; ?>"> <input type="hidden" name="staff" value="<?php echo $staff; ?>"> <input type="submit" value="確定"> </form> <br> <form action="health_use_register.php" method="post"> <input type="hidden" id="today" name="date" value="<?php echo $date; ?>"> <input type="hidden" id="time" name="entry_time" value="<?php echo $entry_time; ?>"> <input type="hidden" name="exit_time" value="<?php echo $exit_time; ?>"> <input type="hidden" name="student_num" value="<?php echo $student_num; ?>"> <input type="hidden" name="student_name" value="<?php echo $student_name; ?>"> <input type="hidden" name="gender" value="1" value="<?php echo $gender; ?>"> <input type="hidden" name="student_teacher" value="<?php echo $student_teacher; ?>"> <input type="hidden" name="symptom" value="<?php echo $symptom; ?>"> <input type="hidden" name="symptom_time" value="<?php echo $symptom_time; ?>"> <input type="hidden" name="symptom_cause" value="<?php echo $symptom_cause; ?>"> <input type="hidden" name="taiou" value="<?php echo $taiou; ?>"> <input type="hidden" name="symptom_fix" value="<?php echo $symptom_fix ?>"> <input type="hidden" name="symptom_type" value="<?php echo $symptom_type; ?>"> <input type="hidden" name="memo" value="<?php echo $memo; ?>"> <input type="hidden" name="staff" value="<?php echo $staff; ?>"> <input type="submit" value="編集"> </form> </body> </html>

よろしくお願いします!!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答4

0

ボタンをcheckedにするにはif文などで1ならcheckedにするみたいなことをたくさん書けばいいのでしょうか?

はい。

もとスマートなやり方があれば教えてほしいです。

入力フォーム自体の情報を配列などで設定ファイルで持っておくこと。
するとフォーム自体の生成も変更も楽です。

php

1<?php 2$input = [ 3 "name"=>[ 4 "type"=>"text", 5 "label"=>"名前", 6 ], 7 "sex"=>[ 8 "type"=>"radio", 9 "label"=>"性別", 10 "default"=>"0", 11 "list"=>["男","女"] 12 ], 13 "years"=>[ 14 "type"=>"radio", 15 "label"=>"年代", 16 "default"=>"1", 17 "list"=>["10代","20代","30代"] 18 ], 19]; 20 21$form = ""; 22foreach($input as $name=>$setting){ 23 24 $form .="<p>".$setting["label"]."\n"; 25 $default = ""; 26 if($default == "" && array_key_exists("default",$setting)){ 27 $default = $setting["default"]; 28 } 29 30 $nameval = filter_input(INPUT_POST,$name); 31 if(!is_null($nameval) && !empty($nameval)){ 32 $default = $nameval; 33 } 34 switch($setting["type"]){ 35 case "text": 36 case "hidden": 37 $form .='<input type="'.$setting["type"].'" name="'.$name.'" value="'.$default.'">'."\n"; 38 break; 39 case "radio": 40 foreach($setting["list"] as $val=>$label){ 41 $checked = ""; 42 if($val == $default){ 43 $checked = " checked"; 44 } 45 $form .='<input type="radio" name="'.$name.'" value="'.$val.'"'.$checked.'>'.$label."\n"; 46 47 } 48 break; 49 } 50 $form .="</p>\n"; 51} 52 53echo $form;

出力結果

HTML

1<p>名前 2<input type="text" name="name" value=""> 3</p> 4<p>性別 5<input type="radio" name="sex" value="0" checked>男 6<input type="radio" name="sex" value="1">女 7</p> 8<p>年代 9<input type="radio" name="years" value="0">10代 10<input type="radio" name="years" value="1" checked>20代 11<input type="radio" name="years" value="2">30代 12</p>

設定配列のところと連携させているforeachで回しているところのパターンを増やせば
割と幾らでも汎用的に対応可能です。
設定配列は別ファイルに持っておいたほうが望ましいですね。

投稿2018/12/14 02:33

m.ts10806

総合スコア80850

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2018/12/14 02:55 編集

ちなみにXSS対応などセキュリティ対策は入れていません。 ベースが出来上がったら適宜いれてください
kai37

2018/12/18 05:18

ありがとうございます!
guest

0

もとスマートなやり方があれば教えてほしいです。

一般的には何らかのフレームワークを使用します。
ただ、web 投稿の基礎があまりわかっていないようなので、先に以下の記事を参考にしてみてください。

【PHP初心者向け】セキュアな掲示板を最小構成から作る

最近メンテされていない記事なので、多少気になるところはありますが、基本的な考え方は学べると思います。

投稿2018/12/14 02:52

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kai37

2018/12/18 05:18

ありがとうございます!
guest

0

項目が少ないのなら、関数を作ってしまうのが手っ取り早いですね。

<input type="radio" name="gender" value="1" <?php printChecked($gender, 1) ?>>男 <input type="radio" name="gender" value="2" <?php printChecked($gender, 2) ?>>女
function printChecked($value1, $value2) { if ($value1 == $value2) { echo 'checked'; } }

投稿2018/12/14 03:15

kasa0

総合スコア578

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kai37

2018/12/17 01:36

この方法なんですが、別のフォームからPOSTで送られてきた値を関数で処理できるのでしょうか?
kasa0

2018/12/17 01:46

どこからPOSTされようが、またPOSTされていなかろうが、$genderで判断しているだけなので、処理はできますよ。 最初にこのページを開いたときは、POSTはされていないので、$genderを初期化する必要はあります。
kai37

2018/12/18 05:18

ありがとうございます!
guest

0

ベストアンサー

汎用的にやるならこんな感じ
(ちょっと見やすく書き換えました)

PHP

1<?php 2 3$text=[]; 4$h=[]; 5$checked=[]; 6 7$list=["date","student_num","student_name","student_teacher","symptom", 8 "symptom_time","symptom_cause","taiou","symptom_fix","memo", 9 "symptom_type","entry_time","exit_time","staff" 10 ]; 11 12foreach($list as $val){ 13 $text[$val]=filter_input(INPUT_POST,$val); 14 $h[$val]=htmlspecialchars($text[$val]); 15} 16 17$checked=[ 18 "gender"=>[1=>"",2=>""], 19 "symptom_type"=>["外科"=>"","内科"=>"","その他"=>""], 20 ]; 21 22foreach(array_keys($checked) as $val){ 23 $checked[$val][filter_input(INPUT_POST,$val)]=" checked"; 24} 25 26print <<<eof 27<h3>保健室利用メモ</h3> 28<form method="post"> 29<table> 30<tr> 31<th>日付:</th> 32<td><input type="date" id="today" name="date" value="{$h["date"]}"></td> 33</tr> 34<tr> 35<th>入室時間:</th> 36<td><input type="time" id="time" name="entry_time" value="{$h["entry_time"]}"></td> 37</tr> 38<tr> 39<th>退出時間:</th> 40<td><input type="time" id="time" name="exit_time" value="{$h["exit_time"]}"></td> 41</tr> 42<tr> 43<th>学籍番号:</th> 44<td><input type="text" name="student_num" placeholder="16XX0101" value="{$h["student_num"]}"></td> 45</tr> 46<tr> 47<th>氏名:</th> 48<td><input type="text" name="student_name" placeholder="電子太郎" value="{$h["student_name"]}"></td> 49</tr> 50<tr> 51<th>性別:</th> 52<td> 53 54<label><input type="radio" name="gender" value="1"{$checked["gender"][1]}>男</label> 55<label><input type="radio" name="gender" value="2"{$checked["gender"][2]}>女</label> 56</td> 57</tr> 58<tr> 59<th>担任:</th> 60<td><input type="text" name="student_teacher" value="{$h["student_teacher"]}"></td> 61</tr> 62<tr> 63<th>症状:</th> 64<td><input type="text" name="symptom" value="{$h["symptom"]}"></td> 65</tr> 66<tr> 67<th>いつから:</th> 68<td><input type="text" name="symptom_time" value="{$h["symptom_time"]}"></td> 69</tr> 70<tr> 71<th>原因は?:</th> 72<td><input type="text" name="symptom_cause" value="{$h["symptom_cause"]}"></td> 73</tr> 74<tr> 75<th>対応:</th> 76<td><input type="text" name="taiou" value="{$h["taiou"]}"></td> 77</tr> 78<tr> 79<th>処置:</th> 80<td><input type="text" name="symptom_fix" value="{$h["symptom_fix"]}"></td> 81</tr> 82<tr> 83<th>症状種別:</th> 84<td> 85<label><input type="radio" name="symptom_type" value="外科"{$checked["symptom_type"]["外科"]}>外科</label> 86<label><input type="radio" name="symptom_type" value="内科"{$checked["symptom_type"]["内科"]}>内科</label> 87<label><input type="radio" name="symptom_type" value="その他"{$checked["symptom_type"]["その他"]}>その他</label> 88</td> 89</tr> 90<tr> 91<th>メモ:</th> 92<td><textarea name="memo">{$h["memo"]}</textarea></td> 93</tr> 94<tr> 95<th>対応者:</th> 96<td><input type="text" name="staff" value="{$h["staff"]}"></td> 97</tr> 98</table> 99<input type="submit" value="確定"> 100</form> 101</body> 102</html> 103 104eof;

投稿2018/12/14 03:10

編集2018/12/14 03:23
yambejp

総合スコア114831

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kai37

2018/12/18 05:18

ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問