PHPの登録フォームを作っています。
ユーザーが入力ミスをした時にデータをキープしたままエラーメッセージが表示されるようにしたいです。
現在のプログラムですと、3項目のうち、1つや2つだけブランクにした場合、他の入力データをキープしたままvalidation エラーが表示されます。
しかし、validation errorにひっかかるデータを入力した場合、全て入力データが消えてしまいます。
またusername とemailのvalidationは、
"/^[A-Za-z0-9' -]{1,15}$/"
とセットしているのですが、15文字以上入力できてしまいます。
もちろんDBの方は15文字でセットしてあるので、16文字以降はただカットされてます。
15文字以上入力された場合、エラーを出すにはどうしたらいいでしょうか。
php
1<?php 2/* Program name: register.php 3 * Description: Program displays the blank form and checks 4 * all the form fields for blank fields. 5 */ 6ini_set("display_errors","on"); 7error_reporting(E_ALL | E_STRICT); 8ini_set("include_path","./includes"); 9include("dbinfo.inc"); 10 11if(isset($_POST['submitted']) and $_POST['submitted'] == "yes") 12{ 13 foreach($_POST as $field => $value) 14 { 15 if(empty($value)) 16 { 17 if($field != "middle_name") 18 { 19 $blank_data[] = $field; 20 } 21 } 22 else 23 { 24 $good_data[$field] = strip_tags(trim($value)); 25 } 26 } 27 if(@sizeof($blank_data) > 0) 28 { 29 $message = "<p style='color: red; margin-bottom: 0; 30 font-weight: bold'> 31 You didn't fill in one or more required fields. 32 You must enter: 33 <ul style='color: red; margin-top: 0; 34 list-style: none' >"; 35 /* display list of missing information */ 36 foreach($blank_data as $value) 37 { 38 $message .= "<li>$value</li>"; 39 } 40 $message .= "</ul>"; 41 echo $message; 42 extract($good_data); 43 include("form.inc"); 44 exit(); 45 } 46 foreach($_POST as $field => $value) 47 { 48 if(!empty($value)) 49 { 50 //validations 51 $name_patt = "/^[A-Za-z0-9' -]{1,15}$/"; 52 $email_patt = "/^.+@.+\\..+$/"; 53 $passwd_patt = "/^[A-Za-z0-9' -]{1,15}$/"; 54 55 if(preg_match("/username/i",$field)) 56 { 57 if(!preg_match($name_patt,$value)) 58 { 59 $error_array[] = "$value is not a valid Username"; 60 } 61 } 62 if(preg_match("/email/i",$field)) 63 { 64 if(!preg_match($email_patt,$value)) 65 { 66 $error_array[] = "$value is not a valid Email"; 67 } 68 } 69 70 if(preg_match("/password/i",$field)) 71 { 72 if(!preg_match($passwd_patt,$value)) 73 { 74 $error_array[] = "$value is not a valid password"; 75 } 76 } 77 } 78 } 79 if(@sizeof($error_array) > 0) 80 { 81 $message = "<ul style='color: red; list-style: none' >"; 82 foreach($error_array as $value) 83 { 84 $message .= "<li>$value</li>"; 85 } 86 $message .= "</ul>"; 87 echo $message; 88 @extract($clean_data); 89 include("form.inc"); 90 exit(); 91 } 92 else 93 { 94 $cxn = mysqli_connect($host,$user,$passwd,$dbname) 95 or die("Couldn't connect to server"); 96 //foreach((array)$clean_data as $field => $value) 97 $clean_data = array(); 98 foreach($_POST as $field => $value) 99 { 100 $clean_data[$field] = mysqli_real_escape_string($cxn,$value); 101 } 102 $date = date("Y-m-d H:i:s"); 103 //echo date("Y/m/d H:i:s",$date); 104 //$sql = "INSERT INTO CustomerPhone (last_name,first_name,middle_name,phone) 105 //VALUES ('$clean_data[last_name]','$clean_data[first_name]', 106 // '$clean_data[middle_name]','$clean_data[phone]')"; 107 ?> 108 <form method="post"> 109 <input type="hidden" name="username" value="<?php echo htmlspecialchars($_POST['username']); ?>"> 110 <input type="hidden" name="email" value="<?php echo htmlspecialchars($_POST['email']); ?>"> 111 <input type="hidden" name="password" value="<?php echo htmlspecialchars($_POST['password']); ?>"> 112 <input type="hidden" name="date" value="<?php echo $date; ?>"> 113 </form> 114 115 <?php 116 117 /* loop that displays the form */ 118 $sql = "INSERT INTO login (username,email,password,date) 119 VALUES ('".$clean_data['username']."','".$clean_data['email']."', 120 '".$clean_data['password']."','$date')"; 121//echo $clean_data['username']; 122 $result = mysqli_query($cxn,$sql) 123 or die("Couldn't execute query"); 124 $signUpMessage = 'Registered. Your Username is '.$clean_data['username'].' Password is'.$clean_data['password'].' '; 125 echo $signUpMessage; 126 include("stored.inc"); 127 } 128} 129else 130{ 131 include("form.inc"); 132} 133?>
回答2件
あなたの回答
tips
プレビュー