質問編集履歴

1

コードの追記

2023/06/06 03:20

投稿

iwahaku
iwahaku

スコア4

test CHANGED
File without changes
test CHANGED
@@ -15,60 +15,186 @@
15
15
 
16
16
  ```ここに言語名を入力
17
17
  《contact.php》お問合せフォーム
18
+ 入力フォームこちらになります。
18
19
  <?php
19
20
 
20
- $name = "";
21
+ $name = "";
21
- $kana = "";
22
+ $kana = "";
22
- $tel = "";
23
+ $tel = "";
23
- $email = "";
24
+ $email = "";
24
- $body = "";
25
+ $body = "";
25
-
26
+
26
- if($_SERVER['REQUEST_METHOD'] === 'POST')
27
+ if($_SERVER['REQUEST_METHOD'] === 'POST')
27
- {
28
+ {
28
- $returnFlg = false;
29
+ $returnFlg = false;
29
- if(! isset($_POST['name']))
30
+ if(! isset($_POST['name']))
30
- {
31
+ {
31
- $name = htmlspecialchars($_POST['name']);
32
+ $name = htmlspecialchars($_POST['name']);
32
- $returnFlg = true;
33
+ $returnFlg = true;
33
- }
34
+ }
34
-
35
+
35
- if(! isset($_POST['kana']))
36
+ if(! isset($_POST['kana']))
36
- {
37
+ {
37
- $kana = htmlspecialchars($_POST['kana']);
38
+ $kana = htmlspecialchars($_POST['kana']);
38
- $returnFlg = true;
39
+ $returnFlg = true;
39
- }
40
+ }
40
-
41
+
41
- $tel = htmlspecialchars($_POST['tel']);
42
+ $tel = htmlspecialchars($_POST['tel']);
42
-
43
+
43
- if(! isset($_POST['email']))
44
+ if(! isset($_POST['email']))
44
- {
45
+ {
45
- $email = htmlspecialchars($_POST['email']);
46
+ $email = htmlspecialchars($_POST['email']);
46
- $returnFlg = true;
47
+ $returnFlg = true;
47
- }
48
+ }
48
-
49
+
49
- if(! isset($_POST['body']))
50
+ if(! isset($_POST['body']))
50
- {
51
+ {
51
- $body = htmlspecialchars($_POST['body']);
52
+ $body = htmlspecialchars($_POST['body']);
52
- $returnFlg = true;
53
+ $returnFlg = true;
53
- }
54
+ }
54
-
55
+
55
- if(!$returnFlg)
56
+ if(!$returnFlg)
56
- {
57
+ {
58
+ if(count($errors) === 0){
57
- $_SESSION['name']= $name;
59
+ $_SESSION['name']= $name;
58
- $_SESSION['kana'] = $kana;
60
+ $_SESSION['kana'] = $kana;
59
- $_SESSION['tel']= $tel;
61
+ $_SESSION['tel']= $tel;
60
- $_SESSION['email']= $email;
62
+ $_SESSION['email']= $email;
61
- $_SESSION['body'] = $body;
63
+ $_SESSION['body'] = $body;
64
+
62
-
65
+ exit;
63
- header('Location:http://localhost/confirm.php');
66
+ header('Location:http://localhost/confirm.php');
64
- exit;
67
+
65
- }
68
+ }
66
-
67
-
68
- }
69
+ }
70
+
71
+ }
69
72
  ?>
70
73
 
71
74
  <!DOCTYPE html>
75
+ <html>
76
+ <head>
77
+ <meta charset="UTF-8">
78
+ <title>サーバーサイド基礎 課題form</title>
79
+ <link rel="stylesheet" type="text/css" href="base.css">
80
+ </head>
81
+ <body>
82
+ <!--header挿入 -->
83
+ <header>
84
+ <?php include($_SERVER['DOCUMENT_ROOT']."/header.php");?>
85
+ </header>
86
+
87
+ <section>
88
+ <div id="contact_box">
89
+ <h2><b>お問い合わせ</b></h2>
90
+ <form action="contact.php" method="post" id="formContact">
91
+ <h3>下記の項目をご記入の上送信ボタンを押してください</h3>
92
+ <p>送信頂いた件につきましては、当社より折り返しご連絡を差し上げます。</p>
93
+ <p>なお、ご連絡までに、お時間を頂く場合もございますので予めご了承ください。</p>
94
+ <p><span class="required">*</span>は必須項目となります。</p>
95
+ <dl>
96
+ <dt><label for="name">氏名</label><span class="required">*</span></dt>
97
+ <dt><span id="name-msg" class="required"></span></dt>
98
+ <dd><input type="text" name="name" id="name" placeholder="山田太郎" value="<?=$name?>"></dd>
99
+
100
+ <dt><label for="kana">フリガナ</label><span class="required">*</span></dt>
101
+ <dt><span id="kana-msg" class="required"></span></dt>
102
+ <dd><input type="text" name="kana" id="kana" placeholder="ヤマダタロウ" ></dd>
103
+
104
+ <dt><label for="tel">電話番号</label></dt>
105
+ <dt><span id="tel-msg" class="required"></span></dt>
106
+ <dd><input type="text" name="tel" id="tel" placeholder="09012345678" ></dd>
107
+
108
+ <dt><label for="email">メールアドレス</label><span class="required">*</span></dt>
109
+ <dt><span id="email-msg" class="required"></span></dt>
110
+ <dd><input type="text" name="email" id="email" placeholder="test@test.co.jp" ></dd>
111
+ </dl>
112
+ <h3><label for="body">お問い合わせ内容をご記入ください<span class="required">*</span></label></h3>
113
+
114
+ <dl>
115
+ <dd><textarea name="body" id="body"></textarea></dd>
116
+ <dt><span id="body-msg" class="required"></span></dt>
117
+ <dd>
118
+ <!-- <button id="send" class="send" name="submit">送 信</button> -->
119
+ </dd>
120
+ </dl>
121
+ </form>
122
+ <dl>
123
+ <dd>
124
+ <button id="send" class="send" name="submit">送 信</button>
125
+ </dd>
126
+ </dl>
127
+ </div>
128
+ </section>
129
+
130
+ <!--footer挿入-->
131
+ <?php include($_SERVER['DOCUMENT_ROOT']."/footer.php");?>
132
+
133
+ <script>
134
+ window.onload = function() {
135
+ document.getElementById('send').onclick = function(){
136
+
137
+ let returnFlg = false;
138
+ let messages = [];
139
+
140
+ const name = document.getElementById('name').value;
141
+ if(name === ""){
142
+ returnFlg = true;
143
+ document.getElementById('name-msg').innerHTML = "名前は必須項目です。";
144
+ messages.push("名前は必須項目です。");
145
+ }else if(name.length >= 10){
146
+ returnFlg = true;
147
+ document.getElementById('name-msg').innerHTML = "氏名は10文字以内で入力してください。";
148
+ messages.push("氏名は10文字以内で入力してください。");
149
+ }
150
+
151
+ const kana = document.getElementById('kana').value;
152
+ if(kana === ""){
153
+ returnFlg = true;
154
+ document.getElementById('kana-msg').innerHTML = "フリガナは必須項目です。";
155
+ messages.push("フリガナは必須項目です。");
156
+ }else if(kana.length >= 10){
157
+ returnFlg = true;
158
+ document.getElementById('kana-msg').innerHTML = "フリガナは10文字以内で入力してください。";
159
+ messages.push("フリガナは10文字以内で入力してください。");
160
+ }
161
+
162
+ const tel = document.getElementById('tel').value;
163
+ if (!tel.match(/^0\d{9,10}$/)) {
164
+ returnFlg = true;
165
+ document.getElementById('tel-msg').innerHTML = "電話番号は正しい形式で入力してください。";
166
+ messages.push("電話番号は正しい形式で入力してください。");
167
+ }
168
+
169
+ const email = document.getElementById('email').value;
170
+ if(email === ""){
171
+ returnFlg = true;
172
+ document.getElementById('email-msg').innerHTML = "メールアドレスは必須項目です。";
173
+ messages.push("メールアドレスは必須項目です。");
174
+ }else if (!email.match(/.+@.+\..+/)) {
175
+ returnFlg = true;
176
+ document.getElementById('email-msg').innerHTML = "メールアドレスは正しい形式で入力してください。";
177
+ messages.push("メールアドレスは正しい形式で入力してください。");
178
+ }
179
+
180
+ const body = document.getElementById('body').value;
181
+ if(body === ""){
182
+ returnFlg = true;
183
+ document.getElementById('body-msg').innerHTML = "お問い合わせ内容は必須項目です。";
184
+ messages.push("お問い合わせ内容は必須項目です。");
185
+ }
186
+
187
+ if(returnFlg){
188
+ alert(messages.join('\n'));
189
+ return false;
190
+ }
191
+
192
+ document.forms.formContact.submit();
193
+ }
194
+ }
195
+ </script>
196
+ </body>
197
+ </html>
72
198
  《comfirm.php》確認フォーム
73
199
  <?php
74
200
  session_start(); //SESSIONを使うときは最初にスタートさせる