phpでフォームを作成しています。フォーム画面でフォームに入力、送信し、フォーム内容が空白などの不具合がある場合には、ページ移動する前にエラーとエラーの内容を出すようにしたいのですが、どのような記述を書けばいいのかがよくわからないのでアドバイスなどいただけたら幸いです。
現在の記述です。if(isset($_POST['send])){}で判定するのかな?とは思っているのですが全く効いていない模様で、ページ遷移してしまいます。
php
1<?php 2if(isset($_POST['send'])){ 3 $err = array(); 4 if(!$_POST['name_form']) { 5 $err['name_form'] = '名前を入力して下さい'; 6 } 7} 8?> 9<!DOCTYPE html> 10<html lang="ja"> 11<head> 12 <meta charset="UTF-8"> 13 <title>フォームPHP&mysql</title> 14 <link rel="stylesheet" type="text/css" href="css/task_ex1.css"> 15</head> 16<body> 17 <legend class="form form_width"><h1 class="title_form">フォーム</h1> 18 <form name="form" class="page" id="page" action="php/task_ex1_list.php" method="post"> 19 <div> 20 <label for="textForm">名前:</label> 21 <input type="text" placeholder="名前入力してください" size="50" maxlength="50" name="name_form" id="textForm"> 22 </div> 23 <div> 24 <label for="radioForm" class="gender">性別:</label> 25 <label for="gender1">男性</label><input type="radio" name="gender" value="男性" id="gender1"> 26 <label for="gender2">女性</label><input type="radio" name="gender" value="女性" id="gender2"> 27 <label for="gender3">その他</label><input type="radio" name="gender" value="その他" id="gender3" checked="checked"> 28 </div> 29 <div class="age"> 30 年齢:<select name="age" id="selbox"> 31 <option value="~20代">~20代</option> 32 <option value="30代~40代">30代~40代</option> 33 <option value="50代~60代">50代~60代</option> 34 <option value="70代~">70代~</option> 35 </select> 36 </div> 37 <div class="questionnaire"> 38 アンケート:好きな色(複数選択可) 39 <input type="checkbox" name="q1[]" value="赤" id="color1" ><label for="color1">赤</label> 40 <input type="checkbox" name="q1[]" value="青" id="color2" ><label for="color2">青</label> 41 <input type="checkbox" name="q1[]" value="黄" id="color3" ><label for="color3">黄</label> 42 <input type="checkbox" name="q1[]" value="緑" id="color4" ><label for="color4">緑</label> 43 <input type="checkbox" name="q1[]" value="紫" id="color5" ><label for="color5">紫</label> 44 </div> 45 <div class="free_space"> 46 自由記入欄:<br> 47 <textarea id="freeSpace" name="free_space" cols="50" rows="6" maxlength="250" placeholder="ご自由にご記入して下さい" ></textarea> 48 </div> 49 <div class="btn"> 50 <input class="button" type="submit" id="button" value="登録する" name="send" > 51 <input class="button" type="reset" value="クリア"> 52 <input class="button" type="button" value="一覧画面" onClick="location.href='php/task_ex1_list.php'"> 53 54 </div> 55 </form> 56 </legend> 57</body> 58</html>
回答3件
あなたの回答
tips
プレビュー