1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHPのメールフォームの作成について SESSION

Posted at

PHPでメールフォームを作成しているのですが、セレクトボタンやラジオボタンの時にSSESIONを使用して情報を保存したいのですが、どうしても上手くいきません。
セレクトボタン=応募職種
ラジオボタン=性別
どのようなコードを書くべきでしょうか?
*他は全てSESSIONで情報が保存されます。

index.php
<?php
    session_start();

    $work = "";
    $furigana = "";
    $name = "";
    $sex = "";
    $age = "";
    $tel = "";
    $mail = "";
    $content = "";
    $skill = "";
    $performance = "";
    $motive = "";
    $errorWork = "";
    $errorName = "";
    $errorFurigana = "";
    $errorSex = "";
    $errorAge = "";
    $errorTel = "";
    $errorMail = "";
    $errorContent = "";
    $errorSkill = "";
    $errorPerformance = "";
    $errorMotive = "";

    if(isset($_SESSION['work']) && isset($_SESSION['name']) && isset($_SESSION['furigana']) && isset($_SESSION['sex'])
    && isset($_SESSION['age']) && isset($_SESSION['tel']) && isset($_SESSION['mail']) 
    && isset($_SESSION['content']) && isset($_SESSION['skill']) && isset($_SESSION['performance']) 
    && isset($_SESSION['motive'])){
        $work = !is_null($_SESSION['work']) ? $_SESSION['work'] : "";
        $name = !is_null($_SESSION['name']) ? $_SESSION['name'] : "";
        $furigana = !is_null($_SESSION['furigana']) ? $_SESSION['furigana'] : "";
        $sex = !is_null($_SESSION['sex']) ? $_SESSION['sex'] : "";
        $age = !is_null($_SESSION['age']) ? $_SESSION['age'] : "";
        $tel = !is_null($_SESSION['tel']) ? $_SESSION['tel'] : "";
        $mail = !is_null($_SESSION['mail']) ? $_SESSION['mail'] : "";
        $content = !is_null($_SESSION['content']) ? $_SESSION['content'] : "";
        $skill = !is_null($_SESSION['skill']) ? $_SESSION['skill'] : "";
        $performance = !is_null($_SESSION['performance']) ? $_SESSION['performance'] : "";
        $motive = !is_null($_SESSION['motive']) ? $_SESSION['motive'] : "";
    }

    if(isset($_SESSION['error_work']) && isset($_SESSION['error_name']) && isset($_SESSION['error_furigana']) && isset($_SESSION['error_sex'])
    && isset($_SESSION['error_age']) && isset($_SESSION['error_tel']) && isset($_SESSION['error_mail']) 
    && isset($_SESSION['error_content']) && isset($_SESSION['error_skill']) && isset($_SESSION['error_performance'])
    && isset($_SESSION['error_motive'])){
        $errorWork = $_SESSION['error_work'] ? 1 : 0;
        $errorName = $_SESSION['error_name'] ? 1 : 0;
        $errorFurigana = $_SESSION['error_furigana'] ? 1 : 0;
        $errorSex = $_SESSION['error_sex'] ? 1 : 0;
        $errorAge = $_SESSION['error_age'] ? 1 : 0;
        $errorTel = $_SESSION['error_tel'] ? 1 : 0;
        $errorMail = $_SESSION['error_mail'] ? 1 : 0;
        $errorContent = $_SESSION['error_content'] ? 1 : 0;
        $errorSkill = $_SESSION['error_skill'] ? 1 : 0;
        $errorPerformance = $_SESSION['error_performance'] ? 1 : 0;
        $errorMotive = $_SESSION['error_motive'] ? 1 : 0;
    }
?>
    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>phpでフォーム送信</title>
</head>
<body>
<form action="./confirm2.php" method="post">
    <div id="contents">
        <div>
            <label>応募職種</label>
            <select name="work">
                <option value="<?php echo $errorWork; ?>">選択してください</option>
                <option value="<?php echo $work; ?>">エンジニア</option>
                <option value="<?php echo $work; ?>">デザイナー</option>
                <?php if($errorWork) echo "入力に誤りがあります"; ?>
            </select>
        </div>
        <div id="name">
          お名前 <input type="text" name="name" value="<?php echo $name; ?>">
          <?php if($errorName) echo "入力に誤りがあります"; ?>
        </div>
        <div id="furigana">
          フリガナ <input type="text" name="furigana" value="<?php echo $furigana; ?>">
          <?php if($errorFurigana) echo "入力に誤りがあります"; ?>
        </div>
        <div id="age">
          年齢 <input type="text" name="age" value="<?php echo $age; ?>">
          <?php if($errorAge) echo "入力に誤りがあります"; ?>
        </div>
        <div>
            <label>性別</labal>
            <input type="radio" name="sex" value="<?php echo $sex; ?>">男性
            <input type="radio" name="sex" value="<?php echo $sex; ?>">女性
        </div>
        <div id="tel">
          電話番号 <input type="text" name="tel" value="<?php echo $tel; ?>">
          <?php if($errorTel) echo "入力に誤りがあります"; ?>
        </div>
        <div id="mail">
          メールアドレス <input type="text" name="mail" value="<?php echo $mail; ?>">
          <?php if($errorMail) echo "入力に誤りがあります"; ?>
        </div>
        <div id="content">
          職務経歴 <textarea name="content"><?php echo $content; ?></textarea>
          <?php if($errorContent) echo "入力に誤りがあります"; ?>
        </div>
        <div id="skill">
          スキルシート <textarea name="skill"><?php echo $skill; ?></textarea>
          <?php if($errorSkill) echo "入力に誤りがあります"; ?>
        </div>
        <div id="performance">
          実績 <textarea name="performance"><?php echo $performance; ?></textarea>
          <?php if($errorPerformance) echo "入力に誤りがあります"; ?>
        </div>
        <div id="motive">
          志望動機 <textarea name="motive"><?php echo $motive; ?></textarea>
          <?php if($errorMotive) echo "入力に誤りがあります"; ?>
        </div>
        </br>
        </br>
        <input type="submit" value="確認">
    </div>
</form>
</body>
</html>
confirm2.php
<?php
    session_start();

    $work = validateData("work");
    $name = validateData("name");
    $furigana = validateData("furigana");
    $sex = validateData("sex");
    $age = validateData("age");
    $tel = validateData("tel");
    $mail = validateData("mail");
    $content = validateData("content");
    $skill = validateData("skill");
    $performance = validateData("performance");
    $motive = validateData("motive");

    if(!filter_var($mail, FILTER_VALIDATE_EMAIL)){
        $_SESSION['error_mail'] = 1;
    }

    if($_SESSION['error_work'] || $_SESSION['error_name'] || $_SESSION['error_furigana']
    || $_SESSION['error_sex'] || $_SESSION['error_age'] || $_SESSION['error_tel'] 
    || $_SESSION['error_mail'] || $_SESSION['error_content'] || $_SESSION['error_skill'] 
    || $_SESSION['error_performance'] || $_SESSION['error_motive'] ){
        header("Location: ./index.php");
        exit();
    }

    function validateData($key){
        if(is_null($_POST["$key"]) || empty($_POST["$key"]) || !is_string($_POST["$key"])){
            $_SESSION["$key"] = $_POST["$key"];
            $_SESSION["error_" . $key] = 1;
            return "";
        }else{
            $_SESSION["$key"] = $_POST["$key"];
            $_SESSION["error_" . $key] = 0;
            return $_POST["$key"];
        }
    }
    ?>
    <!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>確認画面</title>
</head>
    <body>
        希望職種:<?php echo $work; ?>
        </br>
        お名前:<?php echo $name; ?>
        </br>
        フリガナ:<?php echo $furigana; ?>
        </br>
        性別:<?php echo $sex; ?>
        </br>
        年齢:<?php echo $age; ?>
        </br>
        電話番号:<?php echo $tel; ?>
        </br>
        メールアドレス:<?php echo $mail; ?>
        </br>
        職務経歴
        </br>
        <?php echo $content; ?>
        スキルシート
        </br>
        <?php echo $skill; ?>
        実績
        </br>
        <?php echo $performance; ?>
        志望動機
        </br>
        <?php echo $motive; ?>
        </br>
        </br>
        <form action='./complete2.php' method='post'>
            <input type='button' onclick='history.back()' value='戻る'>
            <input type='submit' value='確認'>
        </form>
    </body>
</html>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?