前提・実現したいこと
PHPを始めたばっかりなのでわかりやすくおしえていただきたいです。
PHPでお問い合わせフォームとつくっているのですが
一つ目は
index.phpでtxtareaで入力したのが$_POSTにはいらなくエラーを出したときにtextareaの所に入力したのが残っていない。
二つ目は
index.php→confirm.phpに遷移し戻るを押した際値の移動が出来ずindex.phpに戻った際入力したものが消えてしまっている。
の二点です
該当のソースコード
index.php
<?php session_start(); $errors = array(); // $comment = filter_input(INPUT_POST,'comment'); // print_r($_SESSION); if(filter_input(INPUT_POST,'message')){ $message = filter_input(INPUT_POST,'message'); if((mb_strlen($message) >= 10 || mb_strlen($message) <= 5)){ $errors['num'] = "5文字以上10文字以下で記入してください。"; //echo $errors['num']; } if(preg_match('/^[a-zA-Z0-9]+$/', $message)){ $errors['abc'] ="ひらがなで入力してください。"; echo "<ul>"; foreach($errors as $com){ echo "<li>{$com}</li>"; } echo "</ul>"; } if(filter_input(INPUT_POST,'mail')){ $mail = filter_input(INPUT_POST,'mail'); if(!preg_match("/^[a-zA-Z0-9_¥.¥-]+@[a-zA-Z0-9¥-]+.[a-zA-Z0-9]{2,4}$/", $mail)){ echo "メールアドレスを入力してください"; } if(filter_input(INPUT_POST,'comment')){ $comment = filter_input(INPUT_POST,'comment'); print_r($_POST); }else{ $_SESSION['message'] = $_POST['message']; $_SESSION['mail'] = $_POST['mail']; $_SESSION['comment'] = $_POST['comment']; $upfile = $_FILES['upfile']; //header("location: confirm.php"); // print_r($_SESSION); exit; } } ?> <html> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <br> <input type="text" name ="message" value ="<?php if (isset($message)) { echo $message; } ?>" /><br> <lable>メールアドレス<br></lable> <input type="text" name ="mail" size ="40" value ="<?php if (isset($mail)) { echo $mail; } ?>" /><br> <label>お問い合わせ内容<br></lable> <textarea name = "commnt"> <?php if (isset($comment)) { echo nl2br($comment); } ?></textarea> <br></html><p><input type="file" name="upfile" required/></p> <input type="submit" value="送信" name="submit_btm"/> </form> </body>
confirm.php
<!DOCTYPE html> <?php session_start(); $message = $_SESSION['message']; $mail = $_SESSION['mail']; $comment = $_SESSION['comment']; //$comment = nl2br($comment); echo "<br>".htmlspecialchars($message)."<br>"; echo "メールアドレス<br>".htmlspecialchars($mail)."<br>"; echo "お問い合わせ内容<br>".htmlspecialchars($comment)."<br>"; echo "<br>"; print_r($_SESSION); if(isset($upfile['tmp_name'])){ $type = exif_imagetype($upfile['tmp_name']); if($upfile !== IMAGETYPE_JPEG && $upfile !==IMAGETYPE_PNG && $upfile !== IMAGETYPE_GIF){ $err['pic']='対象ファイルはPNG,JPG,GIFのみです。'; echo $err['pic']; }else { $extension = pathinfo($upfile['name'], PATHINFO_EXTENSION); $new_upfile = mb5(uniqid(mt_rand(),true)).'.'.$extension; move_uploaded_file($upfile['tmp_name'], './img'.$new_upfile); } } echo "<br> 画像をアップロードしました</p>"; if (isset($_POST["success"])) { header("location: comp.php"); exit; }elseif(isset($_POST["back"])){ header("location: index.php"); exit; } ?> <html> <head> <meta charset="UTF-8"> <title>confirm</title> </head> <body> <form action ="" method = "post"> <input type ="submit" name = "success" value = "送信"/> <input type ="submit" name = "back" value = "戻る" /> </form> </body> </html>よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー