Undefined index
は定義していない配列を使用したときに発生するエラーです。issetを使用して使おうとしている配列が定義されているか確認することで解決できると思います。
PHP
1<?php
2if (isset($_POST["name"]) &&
3 isset($_POST["pass"]) &&
4 isset($_POST["pass2"])
5) {
6 $staff_name = $_POST['name'];
7 $staff_pass = $_POST['pass'];
8 $staff_pass2 = $_POST['pass2'];
9}
#追記
上のコードをそのまま使うと$_POST["name"]
、$_POST["pass"]
、$_POST["pass2"]
が定義されていないときには$staff_name
、$staff_pass
、$staff_pass2
という変数が定義されないことになりますから、それが原因でUndefined variable
というエラーが発生します。$_POST["name"]
、$_POST["pass"]
、$_POST["pass2"]
が定義されていないときにはしなくて良い処理をif文の中に入れることで解決できると思います。
質問の趣旨とは関係ありませんが、oneclick="history.back()"
ではなく、onclick="history.back()"
ではないかと思いますがいかがでしょう。
PHP
1<?php
2if (isset($_POST["name"]) &&
3 isset($_POST["pass"]) &&
4 isset($_POST["pass2"])
5) {
6 $staff_name = $_POST['name'];
7 $staff_pass = $_POST['pass'];
8 $staff_pass2 = $_POST['pass2'];
9 $staff_name = htmlspecialchars($staff_name, ENT_QUOTES, 'UTF-8');
10 $staff_pass = htmlspecialchars($staff_pass, ENT_QUOTES, 'UTF-8');
11 $staff_pass2 = htmlspecialchars($staff_pass2, ENT_QUOTES, 'UTF-8');
12
13 if ($staff_name == ' ') {
14
15 print 'スタッフ名が入力されていません<br />';
16
17 } else {
18
19 print 'スタッフ名:';
20 print $staff_name;
21 print '<br />';
22 }
23
24 if ($staff_pass == ' ') {
25
26 print 'パスワードが入力されていません<br />';
27
28 }
29
30 if ($staff_pass != $staff_pass2) {
31
32 print 'パスワードが一致しません<br />';
33
34 }
35
36 if ($staff_name == '' || $staff_pass == '' || $staff_pass != $staff_pass2) {
37
38 print '<form>';
39 print '<input type="button" oneclick="history.back()" value="戻る">';
40 print '</form>';
41
42 } else {
43 $staff_pass = md5($staff_pass);
44 print '<form method="post" action="staff_add_done.php">';
45 print '<input type="hidden" name="name" value="' . $staff_name . '">';
46 print '<input type="hidden" name="pass" value="' . $staff_pass . '">';
47 print '<br />';
48 print '<input type="button" oneclick="history.back()" value="戻る">';
49 print '<input type="submit" value="OK">';
50 print '</form>';
51
52 }
53}
54?>
#追記2
aciton属性
ではなくて、action属性
です。
HTML
1<!DOCTYPE html>
2<html>
3<head>
4 <meta charset="UTF-8">
5 <title>ろくまる農園</title>
6</head>
7<body>
8
9スタッフ追加<br/>
10<br/>
11<form method="post" action="staff_add_check.php"><!-- action="staff_add_check.php"に修正 -->
12 スタッフ名を入力してください<br/>
13 <input type="text" name="name" style="width:200px"><br/>
14 パスワードを入力して下さい<br/>
15 <input type="password" name="pass" style="width:100px"><br/>
16 パスワードをもう一度入力してください<br/>
17 <input type="password" name="pass2" style="width:100px"><br/>
18 <br/>
19 <input type="button" onclick="history.back()" value="戻る">
20 <input type="submit" value="OK">
21</body>
22</html>
PHP
1<?php
2if (isset($_POST["name"]) &&
3 isset($_POST["pass"]) &&
4 isset($_POST["pass2"])
5) {
6 $staff_name = $_POST['name'];
7 $staff_pass = $_POST['pass'];
8 $staff_pass2 = $_POST['pass2'];
9 $staff_name = htmlspecialchars($staff_name, ENT_QUOTES, 'UTF-8');
10 $staff_pass = htmlspecialchars($staff_pass, ENT_QUOTES, 'UTF-8');
11 $staff_pass2 = htmlspecialchars($staff_pass2, ENT_QUOTES, 'UTF-8');
12
13 if ($staff_name == ' ') {
14
15 print 'スタッフ名が入力されていません<br />';
16
17 } else {
18
19 print 'スタッフ名:';
20 print $staff_name;
21 print '<br />';
22 }
23
24 if ($staff_pass == ' ') {
25
26 print 'パスワードが入力されていません<br />';
27
28 }
29
30 if ($staff_pass != $staff_pass2) {
31
32 print 'パスワードが一致しません<br />';
33
34 }
35
36 if ($staff_name == '' || $staff_pass == '' || $staff_pass != $staff_pass2) {
37 print '<form>';
38 print '<input type="button" onclick="history.back()" value="戻る">';
39 print '</form>';
40
41 } else {
42 $staff_pass = md5($staff_pass);
43 print '<form method="post" action="staff_add_done.php">';
44 print '<input type="hidden" name="name" value="' . $staff_name . '">';
45 print '<input type="hidden" name="pass" value="' . $staff_pass . '">';
46 print '<br />';
47 print '<input type="button" onclick="history.back()" value="戻る">';
48 print '<input type="submit" value="OK">';
49 print '</form>';
50 }
51}
52?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/05/18 11:39
2017/05/18 12:04
退会済みユーザー
2017/05/18 12:05
2017/05/18 12:09
退会済みユーザー
2017/05/18 12:18
2017/05/18 12:24
退会済みユーザー
2017/05/18 12:33
2017/05/18 12:34 編集
退会済みユーザー
2017/05/18 12:40