javascriptでエラー
Q&A
php(HTML)で休業日のメンテナンスフォームを作成しています。
そのフォームは10日分入力できるようにしているのですが、同じ日を2か所に入力して送信ボタンをクリックすると、「同じ日が指定されています。」とエラーが出るようにしています。
送信ボタンを連打すると、複数回メッセージが出ないように、一度メッセージを消して再度メッセージを表示させるようにしたいのですが、連打すると、デバッグツールにて下記のエラーが出ます。
Uncaught TypeError: Cannot read properties of null (reading 'remove')
at HTMLButtonElement.<anonymous> (holiday_mainte.php:221:48)
該当行は、ソースの93行目の
document.getElementById('same1_date_msg').remove();
です。
要はnullのプロパティを削除しようとして発生しているものと思ってますが、デバッグツールで確認したところ、idがsame1_date_msgのメッセージは要素に確かに表示されており、原因が掴めませんでした。
原因究明に協力いただければ幸いです。
ソースコードを添付いたします。
繰り返しになりますが、エラーになったのは93行目です。
<?php ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('button').addEventListener('click', function(){
//エラーメッセージ
let closed1Msg = document.getElementById('closed1_msg');
let closed2Msg = document.getElementById('closed2_msg');
let closed3Msg = document.getElementById('closed3_msg');
let closed4Msg = document.getElementById('closed4_msg');
let closed5Msg = document.getElementById('closed5_msg');
let closed6Msg = document.getElementById('closed6_msg');
let closed7Msg = document.getElementById('closed7_msg');
let closed8Msg = document.getElementById('closed8_msg');
let closed9Msg = document.getElementById('closed9_msg');
let closed10Msg = document.getElementById('closed10_msg');
let same1DateMsg = document.getElementById('same1_date_msg');
let nullDateMsg = document.getElementById('null_date_msg');
//エラーメッセージ用親
let holiday1 = document.getElementById('holiday1');
let holiday2 = document.getElementById('holiday2');
let holiday3 = document.getElementById('holiday3');
let holiday4 = document.getElementById('holiday4');
let holiday5 = document.getElementById('holiday5');
let holiday6 = document.getElementById('holiday6');
let holiday7 = document.getElementById('holiday7');
let holiday8 = document.getElementById('holiday8');
let holiday9 = document.getElementById('holiday9');
let holiday10 = document.getElementById('holiday10');
let error1 = document.getElementById('error1');
let error2 = document.getElementById('error2');
//チェック用値
let closed1 = document.getElementById('closed1').value;
let closed2 = document.getElementById('closed2').value;
let closed3 = document.getElementById('closed3').value;
let closed4 = document.getElementById('closed4').value;
let closed5 = document.getElementById('closed5').value;
let closed6 = document.getElementById('closed6').value;
let closed7 = document.getElementById('closed7').value;
let closed8 = document.getElementById('closed8').value;
let closed9 = document.getElementById('closed9').value;
let closed10 = document.getElementById('closed10').value;
let ampm1 = document.getElementById('ampm1').value;
let ampm2 = document.getElementById('ampm2').value;
let ampm3 = document.getElementById('ampm3').value;
let ampm4 = document.getElementById('ampm4').value;
let ampm5 = document.getElementById('ampm5').value;
let ampm6 = document.getElementById('ampm6').value;
let ampm7 = document.getElementById('ampm7').value;
let ampm8 = document.getElementById('ampm8').value;
let ampm9 = document.getElementById('ampm9').value;
let ampm10 = document.getElementById('ampm10').value;
let close1 = [closed2,closed3,closed4,closed5,closed6,closed7,closed8,closed9,closed10]
let close2 = [closed1,closed3,closed4,closed5,closed6,closed7,closed8,closed9,closed10]
let close3 = [closed1,closed2,closed4,closed5,closed6,closed7,closed8,closed9,closed10]
let close4 = [closed1,closed2,closed3,closed5,closed6,closed7,closed8,closed9,closed10]
let close5 = [closed1,closed2,closed3,closed4,closed6,closed7,closed8,closed9,closed10]
let close6 = [closed1,closed2,closed3,closed4,closed5,closed7,closed8,closed9,closed10]
let close7 = [closed1,closed2,closed3,closed4,closed5,closed6,closed8,closed9,closed10]
let close8 = [closed1,closed2,closed3,closed4,closed5,closed6,closed7,closed9,closed10]
let close9 = [closed1,closed2,closed3,closed4,closed5,closed6,closed7,closed8,closed10]
let close10 = [closed1,closed2,closed3,closed4,closed5,closed6,closed7,closed8,closed9]
console.log(closed1);
for(let dates1 of close1){
if(same1DateMsg === null && closed1 !== '' && closed1 === dates1) {
let msg = '同じ日が指定されています。';
let ptag = document.createElement('p');
ptag.id = 'same1_date_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
error1.appendChild(ptag);
event.preventDefault();
}
else{}
if(same1DateMsg !== null && closed1 !== '' && closed1 === dates1) {
document.getElementById('same1_date_msg').remove();
let msg = '同じ日が指定されています。';
let ptag = document.createElement('p');
ptag.id = 'same1_date_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
error1.appendChild(ptag);
event.preventDefault();
}
else{}
if(same1DateMsg !== null && closed1 !== '' && closed1 !== dates1) {
document.getElementById('same1_date_msg').remove();
}
}
if(nullDateMsg === null && (!closed1) && (!closed2) && (!closed3) && (!closed4) && (!closed5) && (!closed6) && (!closed7) && (!closed8) && (!closed9) && (!closed10)){
let msg = '休業日を設定します。';
let ptag = document.createElement('p');
ptag.id = 'null_date_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
error2.appendChild(ptag);
event.preventDefault();
}
else{}
if(nullDateMsg !== null && (!closed1) && (!closed2) && (!closed3) && (!closed4) && (!closed5) && (!closed6) && (!closed7) && (!closed8) && (!closed9) && (!closed10)){
document.getElementById('null_date_msg').remove();
let msg = '休業日を設定します。';
let ptag = document.createElement('p');
ptag.id = 'null_date_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
error2.appendChild(ptag);
event.preventDefault();
}
else{}
if(nullDateMsg !== null && closed1 !== '' ||
nullDateMsg !== null && closed2 !== '' ||
nullDateMsg !== null && closed3 !== '' ||
nullDateMsg !== null && closed4 !== '' ||
nullDateMsg !== null && closed5 !== '' ||
nullDateMsg !== null && closed6 !== '' ||
nullDateMsg !== null && closed7 !== '' ||
nullDateMsg !== null && closed8 !== '' ||
nullDateMsg !== null && closed9 !== '' ||
nullDateMsg !== null && closed10 !== '' ){
document.getElementById('null_date_msg').remove();
}
if(closed1Msg === null && closed1 !== '' && ampm1 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed1_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday1.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed1Msg !== null && closed1 !== '' && ampm1 == 0){
document.getElementById('closed1_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed1_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday1.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed1Msg !== null && closed1 !== '' && ampm1 != 0){
document.getElementById('closed1_msg').remove();
}
if(closed1Msg !== null && closed1 === '' && ampm1 == 0){
document.getElementById('closed1_msg').remove();
}
if(closed2Msg === null && closed2 !== '' && ampm2 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed2_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday2.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed2Msg !== null && closed2 !== '' && ampm2 == 0){
document.getElementById('closed2_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed2_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday2.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed2Msg !== null && closed2 !== '' && ampm2 != 0){
document.getElementById('closed2_msg').remove();
}
if(closed2Msg !== null && closed2 === '' && ampm2 == 0){
document.getElementById('closed2_msg').remove();
}
if(closed3Msg === null && closed3 !== '' && ampm3 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed3_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday3.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed3Msg !== null && closed3 !== '' && ampm3 == 0){
document.getElementById('closed3_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed3_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday3.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed3Msg !== null && closed3 !== '' && ampm3 != 0){
document.getElementById('closed3_msg').remove();
}
if(closed3Msg !== null && closed3 === '' && ampm3 == 0){
document.getElementById('closed3_msg').remove();
}
if(closed4Msg === null && closed4 !== '' && ampm4 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed4_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday4.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed4Msg !== null && closed4 !== '' && ampm4 == 0){
document.getElementById('closed4_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed4_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday4.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed4Msg !== null && closed4 !== '' && ampm4 != 0){
document.getElementById('closed4_msg').remove();
}
if(closed4Msg !== null && closed4 === '' && ampm4 == 0){
document.getElementById('closed4_msg').remove();
}
if(closed5Msg === null && closed5 !== '' && ampm5 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed5_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday5.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed5Msg !== null && closed5 !== '' && ampm5 == 0){
document.getElementById('closed5_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed5_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday5.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed5Msg !== null && closed5 !== '' && ampm5 != 0){
document.getElementById('closed5_msg').remove();
}
if(closed5Msg !== null && closed5 === '' && ampm5 == 0){
document.getElementById('closed5_msg').remove();
}
if(closed6Msg === null && closed6 !== '' && ampm6 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed6_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday6.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed6Msg !== null && closed6 !== '' && ampm6 == 0){
document.getElementById('closed6_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed6_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday6.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed6Msg !== null && closed6 !== '' && ampm6 != 0){
document.getElementById('closed6_msg').remove();
}
if(closed6Msg !== null && closed6 === '' && ampm6 == 0){
document.getElementById('closed6_msg').remove();
}
if(closed7Msg === null && closed7 !== '' && ampm7 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed7_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday7.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed7Msg !== null && closed7 !== '' && ampm7 == 0){
document.getElementById('closed7_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed7_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday7.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed7Msg !== null && closed7 !== '' && ampm7 != 0){
document.getElementById('closed7_msg').remove();
}
if(closed7Msg !== null && closed7 === '' && ampm7 == 0){
document.getElementById('closed7_msg').remove();
}
if(closed8Msg === null && closed8 !== '' && ampm8 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed8_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday8.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed8Msg !== null && closed8 !== '' && ampm8 == 0){
document.getElementById('closed8_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed8_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday8.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed8Msg !== null && closed8 !== '' && ampm8 != 0){
document.getElementById('closed8_msg').remove();
}
if(closed8Msg !== null && closed8 === '' && ampm8 == 0){
document.getElementById('closed8_msg').remove();
}
if(closed9Msg === null && closed9 !== '' && ampm9 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed9_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday9.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed9Msg !== null && closed9 !== '' && ampm9 == 0){
document.getElementById('closed9_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed9_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday9.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed9Msg !== null && closed9 !== '' && ampm9 != 0){
document.getElementById('closed9_msg').remove();
}
if(closed9Msg !== null && closed9 === '' && ampm9 == 0){
document.getElementById('closed9_msg').remove();
}
if(closed10Msg === null && closed10 !== '' && ampm10 == 0){
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed10_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday10.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed10Msg !== null && closed10 !== '' && ampm10 == 0){
document.getElementById('closed10_msg').remove();
let msg = '時間帯を選択してください。';
let ptag = document.createElement('p');
ptag.id = 'closed10_msg';
let text = document.createTextNode(msg);
ptag.appendChild(text);
holiday10.appendChild(ptag);
event.preventDefault();
}
else{}
if(closed10Msg !== null && closed10 !== '' && ampm10 != 0){
document.getElementById('closed10_msg').remove();
}
if(closed10Msg !== null && closed10 === '' && ampm10 == 0){
document.getElementById('closed10_msg').remove();
}
},false)
},false)
</script>
<noscript>
JavaScriptを実行できません。
</noscript>
参考:PHPソース
<?php
session_start();
header('Expires:-1');
header('Cache-Control:');
header('Pragma:');
function generateToken(){
$bytes = openssl_random_pseudo_bytes(16);
return bin2hex($bytes);
}
$token = generateToken();
$_SESSION['token'] = $token;
function escape($val)
{
return htmlspecialchars($val, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
?>
<!doctype html>
<html lang=ja>
<head>
<meta charset="utf-8">
<title>休日メンテナンス</title>
<meta name="description" content="Salon Yuri-rinの管理者ページ">
<!--リセットcss-->
<link rel="stylesheet" href="https://unpkg.com/ress/dist/ress.min.css">
<!--css-->
<link rel="stylesheet" href="css/calender_mainte_style.css">
<!--レスポンシブ対応-->
<meta name="viwport" content="width=device-width, initial-scale=1">
<!--Googleフォント-->
<link href="https://fonts.googleapis.com/css2?family=Sawarabi+Gothic&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Shippori+Mincho:wght@500&display=swap" rel="stylesheet">
</head>
<body>
<h1>休日メンテナンス</h1>
<form method="post" class=holiday_mainte id="holiday_mainte" action="holiday_mainte_conf.php">
<input type="hidden" name="token" class="token" id="token" value="<?=$token?>">
<div class="holiday1" id="holiday1">
<label for="closed1">休業日1</label>
<input type="date" name="closed1" class="closed1" id="closed1">
<select name="ampm1" id="ampm1">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday2" id="holiday2">
<label for="closed2">休業日2</label>
<input type="date" name="closed2" class="closed2" id="closed2">
<select name="ampm2" id="ampm2">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday3" id="holiday3">
<label for="closed3">休業日3</label>
<input type="date" name="closed3" class="closed3" id="closed3">
<select name="ampm3" id="ampm3">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday4" id="holiday4">
<label for="close4">休業日4</label>
<input type="date" name="closed4" class="closed4" id="closed4">
<select name="ampm4" id="ampm4">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday5" id="holiday5">
<label for="close5">休業日5</label>
<input type="date" name="closed5" class="closed5" id="closed5">
<select name="ampm5" id="ampm5">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday6" id="holiday6">
<label for="close6">休業日6</label>
<input type="date" name="closed6" class="closed6" id="closed6">
<select name="ampm6" id="ampm6">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday7" id="holiday7">
<label for="close7">休業日7</label>
<input type="date" name="closed7" class="closed7" id="closed7">
<select name="ampm7" id="ampm7">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday8" id="holiday8">
<label for="close8">休業日8</label>
<input type="date" name="closed8" class="closed8" id="closed8">
<select name="ampm8" id="ampm8">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday9" id="holiday9">
<label for="close9">休業日9</label>
<input type="date" name="closed9" class="closed9" id="closed9">
<select name="ampm9" id="ampm9">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div class="holiday10" id="holiday10">
<label for="close10">休業日10</label>
<input type="date" name="closed10" class="closed10" id="closed10">
<select name="ampm10" id="ampm10">
<option value=0></option>
<option value=1>午前</option>
<option value=2>午後</option>
<option value=3>終日</option>
</select>
</div>
<div id="error1">
</div>
<div id="error2">
</div>
<div class="buttons">
<button type="button" class="button_re" name="button_re" id="button_re" onclick="history.back()">戻る</button>
<button type="submit" class="button" name="button" id="button">確定</button>
</div>
</form>
<?php require_once('js/holiday_mainte_chk_js.php')?>
</body>
</html>