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

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

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

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

HTML

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

Q&A

解決済

3回答

472閲覧

html php ラジオボタンのPOSTcheck エラーをなくす方法について

kai37

総合スコア12

PHP

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

HTML

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

0グッド

0クリップ

投稿2018/12/13 03:02

初めて質問させていただきます。つたない部分等あるかと思いますがよろしくお願いいたします。

前提・実現したいこと

ある学校に通っていて、チームを作ってシステムを制作する課題になります。
学校の生徒の情報を管理するようなシステムを作成しています。
phpを使用していて、htmlのフォームでPOSTで値を送ってデータベースにインサートしていくようなものです。

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

Notice: Undefined index: date in /var/www/html/health/health_use_register.php on line 42
このような文がたくさん出てしまっていて
フォームから送信→確認画面→登録しました という流れなんですが
確認画面に編集ボタンも実装していて、それを押すと初めのフォームに値をhidden でPOSTして送るみたいなことをしています。
POSTした時は上のエラーメッセージはでません。なので初めに送られてきてもないPOSTを受け取ろうとしているエラーなのかなと思っています。
あとどうしてもわからなかったのがradioボタンの値を編集ボタン押した後にPOSTでうけとれるのはわかるのですが、それを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 = $_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']; ?> <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" value="<?php echo $student_num; ?>"></td> </tr> <tr> <th>氏名:</th> <td><input type="text" name="student_name" 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="内科">内科 </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="確定">

health_use_register_check.php

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>保健室利用メモ確認</title> </head> <body> <?php $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ページで確認できます。

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

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

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

m.ts10806

2018/12/13 03:37

質問編集画面でコード部分を選択し<code>ボタンを押して編集完了してください。コードが見やすくなり、コピペもワンクリックでできるようになります
guest

回答3

0

「Notice: Undefined index」が配列変数を参照しているところで発生しているなら、
データがないのに参照しているってことです。

$date = $_POST['date'];

POST送信されたデータにdateに該当するデータが含まれないということです。

そのphpページを初めて表示すると、
当然POST送信してくるものがないので、
$_POST['~~']系の参照はすべて「Notice: Undefined index」エラーになります。

POST送信は、フォームが表示されてから送信ボタンを押してページ遷移したときに、
起こります。
その時、未記入の項目も、$_POST['~~']で参照すると「Notice: Undefined index」エラーになります。

POSTされていてもいなくても、とりあえず$date変数で受け取りたいという場合には、
yambejpさんの示すとおりfilter_input()使うのが手っ取り早いです。
ですが、例えば$date変数が、未記入でカラなのか、POST送信されずカラなのかの区別をつけるためには、
$_SERVER['REQUEST_METHOD']を点検してGETなら初見(POST送信していない)、
POSTならPOST送信されている、とみることもできますので参考までに。

投稿2018/12/13 03:29

編集2018/12/13 03:32
退会済みユーザー

退会済みユーザー

総合スコア0

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

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

kai37

2018/12/14 01:04

ありがとうございます! とても参考になりました。
guest

0

ベストアンサー

$student_num = $_POST['student_num'];

のような受け方をしている変数へのpostデータの代入している箇所をすべて

PHP

1$student_num = filter_input(INPUT_POST,'student_num');

のような書き方に書き換えて下さい

投稿2018/12/13 03:20

yambejp

総合スコア114784

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

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

kai37

2018/12/14 01:07

ありがとうございます!filter_inputを知らなかったので教えていただきありがとうございます!
guest

0

health_use_register.php
でPOSTデータ送られないときありませんか?
(多分最初health_use_register.phpに来るので送られないのではと思います)

データ送られてないときは
$_POST['date'];dataっていうキーがないのでエラーになります

対策としては

php

1// php7では 2$date = $_POST['date'] ?? ''; 3 4// php5では 5$date = isset($_POST['date']) ? $_POST['date'] : ''; 6 7// filter_inputでやるなら 8// getなら 9$date = filter_input(INPUT_GET, 'date'); 10// postなら 11$date = filter_input(INPUT_POST, 'date'); 12

です

投稿2018/12/13 03:25

rururu3

総合スコア5545

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

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

kai37

2018/12/14 01:06

そうですね、初めにhealth_use_register.phpを表示しているのでエラーが履かれてたみたいです。 filter_inputのおかげでエラー消えました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問