###前提・実現したいこと
PHPで、アンケートフォームを作っており、
「バリデーションチェックをして下の表に記述。
エラーがあれば表示し、チェックボックスの中身は保持する。
また、値が複数ある場合はすべて表示する」
ということを実現しようと思っています。
###発生している問題・エラーメッセージ
ここを参考にして配列を用いた表示を作成していましたが、
選択していない部分の値が全て
Undefined offset: x(xは選択していない部分の値)と
なって出てきてしまいます。
また、複数を選択しても、最終的に出力される答えは常に、値が大きい物のみになってしまいます。
###該当のソースコード
<?php if (empty($_POST['attribute'])) { $errors['attribute'] = "属性が入力されていません。"; }/* elseif (htmlspecialchars(ctype_digit(strval($_POST['attribute'])), ENT_QUOTES) == false) { $errors['attribute'] = "不正なPOSTです。(属性1)"; } */elseif (intval($_POST['attribute']) > 22) { $errors['attribute'] = "不正なPOSTです。(属性)"; } else { $attribute = $_POST['attribute']; } if (intval($_POST['attribute']) == 22) { $x = $_POST['attribute_custom']; if (($x == null) && (intval($_POST['attribute']) == 23)) { $errors['attribute_custom'] = "属性が入力されていません。(カスタム1)"; } else if ((str_replace(' ' ,'', $_POST['attribute_custom']) == "") && (intval($_POST['attribute']) == 23)) { $errors['attribute_custom'] = "属性が入力されていません。(カスタム2)"; } else { $attribute_custom = $_POST['attribute_custom']; } } ?> <?php $attribute2 = array( 1 => 'a', 2 => 'b', 3 => 'c', //同じようなソースの繰り返し 23 => 'x',); $check_attribute = array(); if (isset($_POST['attribute']) && isset($attribute)) { for($i=0;$i<count(intval($attribute));$i++){ $check_attribute[$attribute[$i]]="checked"; } $check_attribute[intval($attribute)]="checked"; foreach ((array)intval($attribute) as $key => $value) { $select_attribute = $attribute2[$value]."、"; } $attribute_list=rtrim($select_attribute, "、"); } echo <<<EOD <div id="choice"> <input type="checkbox" name="attribute[]" value="1" id="1" {$check_attribute[1]}><label for="1">1</label><br><br> <input type="checkbox" name="attribute[]" value="2" id="2" {$check_attribute[2]}><label for="2">2</label><br><br> <input type="checkbox" name="attribute[]" value="3" id="3" {$check_attribute[3]}><label for="3">3</label><br><br> </label><br><br> //同じようなソースの繰り返し <input type="checkbox" name="attribute[]" value="23" id="other" {$check_attribute[23]}><label for="other">その他</label><br><br> <input type="text" name="attribute_custom" placeholder="カスタム" value="" id="attribute-custom"><br> </p> EOD; ?> //関係ないので中略 <?php if ((isset($_POST['attribute_custom'])) && ($attribute == "23") && (isset($attribute))) { $attribute_custom = htmlspecialchars($_POST['attribute_custom'], ENT_QUOTES); echo "その他\($attribute_custom\)"; } elseif (($attribute !== "23") && (isset($attribute))) { echo $attribute_list; } ?>
###補足情報(言語/FW/ツール等のバージョンなど)
環境
XAMPP最新版、Windows10、Atom1.18.0
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。