PHP Build a Basic PHP Website (2018) Enhancing a Form Re-Displaying the Submission

What's wrong with this code?

I wrote this code but I cannot display category in the email. Please help me.

<?php 
if($_SERVER["REQUEST_METHOD"]=="POST"){
    $name=trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
    $email=trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL));
    $category=trim(filter_input(INPUT_POST, "category", FILTER_SANITIZE_STRING));
    $format=trim(filter_input(INPUT_POST, "format", FILTER_SANITIZE_STRING));
    $year=trim(filter_input(INPUT_POST, "year", FILTER_SANITIZE_STRING));
    $details=trim(filter_input(INPUT_POST, "details", FILTER_SANITIZE_SPECIAL_CHARS));


    if($name==""||$email==""||$year==""||$category=""){
        $error_message = "Please fill in the requested fields: Name, Email, Category and Year";
    }

    if(!isset($error_message)&& $_POST["address"]!=""){
        $error_message="Bad form input";
    }

    require("inc/phpmailer/class.phpmailer.php");
    $mail=new PHPMailer;
    if(!isset($error_message)&& !$mail->ValidateAddress($email)){
            $error_message="Invalid Email Address";
        }

    if(!isset ($error_message)){
        $email_body="";
        $email_body.= "お名前 " .$name. "\n";
        $email_body.= "電子メール " .$email. "\n";
        $email_body.= "カテゴリー " .$category. "\n";
        $email_body.= "タイプ " .$format. "\n";
        $email_body.= "築年数 " .$year. "\n";
        $email_body.= "備考欄 " .$details. "\n";

        $mail->setFrom($email, $name);
        $mail->addAddress('koh@gmail.com', 'GangstaCat');     // Add a recipient


        $mail->isHTML(false);                                  // Set email format to HTML
        $mail->Subject = 'Personal Media Library Suggestion from ' . $name;
        $mail->Body = $email_body;
        if($mail->send()) {
            header("location:suggest.php?status=thanks");
            exit;
        }

        $error_message = 'Message could not be sent.';
        $error_message .= 'Mailer Error: ' . $mail->ErrorInfo;

    }


}

$pageTitle="お問い合わせ";
  $section="suggest";

 ?>
 <?php
include("inc/header.php"); ?>
<div class="section page">
    <div class="wrapper">
        <h1>お問い合わせ</h1>
        <?php if (isset($_GET["status"]) && $_GET["status"] =="thanks"){
            echo "<p>有難うございました。後程こちらからお返事致します。</p>";
        }else { 

        if (isset ($error_message)){
        echo "<p class='message'>" .$error_message . "</p>";
        } else{
            echo "<p> なんでもお気軽にご相談ください。</p>";} ?>

        <form method="post" action="suggest.php">
            <table>
            <tr>
                <th><label for="name">お名前 (必須)</label></th>
                <td><input type="text" id="name" name="name" value="<?php if(isset($name)){echo $name;}?>" />
                </td>
            </tr>
            <tr>
                <th><label for="email">電子メール (必須)</label></th>
                <td><input type="text" id="email" name="email" value="<?php if(isset($email)){echo $email;}?>" />
                </td>
            </tr>

            <tr>
                <th><label for="category">カテゴリー</label></th>
                <td>

                <select id="category" name="category" >
                <option value=" ">一つ選んでください</option>
                <optgroup label="物件の種類">
                    <option value="募集中">募集中</option>
                    <option value="入居中">入居中</option>
                </optgroup>
                </select>
                </td>
            </tr>
            <tr>
                <th><label for="format">物件のタイプ</label></th>
                <td><select id="format" name="format">
                <option value=" ">一つ選んでください</option>
                <optgroup label="物件の種類">
                    <option value="アパート">アパート</option>
                    <option value="マンション">マンション</option>
                    <option value="戸建て">戸建て</option>
                    <option value="オフィス">オフィス</option>
                </optgroup>
                </select>
                </td>
            </tr>
            <tr>
                <th><label for="year">築年数 (必須)</label></th>
                <td><input type="text" id="year" name="year" value="<?php if(isset($year)){echo $year;}?>" /></td>
            </tr>
            <tr>
                <th><label for="name">備考欄</label></th>
                <td><textarea name="details" id="details"><?php if(isset($details)){echo htmlspecialchars($_POST["details"]);}?></textarea></td>
            </tr>
            <tr style="display:none">
                <th><label for="address">Address</label></th>
                <td><input type="text" id="address" name="address"  />/>
                <p> Please leave this field blank </p>
                </td>
            </tr>
            </table>
            <input type="submit" value="Send" />
        </form>
        <?php } ?>
    </div>
</div>
<?php include("inc/footer.php");?>
Kai Macmaster
Kai Macmaster
14,567 Points

Hi Kohane,

It's hard to see exactly what's going wrong as you've not formatted the question using the correct markdown, but I've got some pointers that could make it easier to solve your problem.

Your code:

<div class="wrapper">
    <h1>お問い合わせ</h1>
    <?php if (isset($_GET["status"]) && $_GET["status"] =="thanks"){
        echo "<p>有難うございました。後程こちらからお返事致します。</p>";
    }else { 
    if (isset ($error_message)){
    echo &quot;&quot; .$error_message . &quot;&quot;;
    } else{
        echo &quot; なんでもお気軽にご相談ください。&quot;;} ?&gt;

    <form method="post" action="suggest.php">
        <table>
        <tr>

Solution:

<div class="wrapper">
    <h1>お問い合わせ</h1>
    <?php 
    if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
        echo "<p>有難うございました。後程こちらからお返事致します。</p>";
    } else if (isset ($error_message)) {
        echo $error_message;
    } else {
        echo "なんでもお気軽にご相談ください。";
    } 
    ?>
    <form method="post" action="suggest.php">
        <table>
        <tr>

This might not get it to display correctly, but it's certainly a good place to start. :) Also, your PHP code isn't formatted correctly:

<?php
if ($_SERVER["REQUEST_METHOD"]=="POST") { 
    $name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
    $email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL)); 
    $category = trim(filter_input(INPUT_POST, "category", FILTER_SANITIZE_STRING)); 
    $format = trim(filter_input(INPUT_POST, "format", FILTER_SANITIZE_STRING));
    $year = trim(filter_input(INPUT_POST, "year", FILTER_SANITIZE_STRING)); 
    $details = trim(filter_input(INPUT_POST, "details", FILTER_SANITIZE_SPECIAL_CHARS));
}

if ($name=="" || $email == "" || $year == "" || $category = "") {
    $error_message = "Please fill in the requested fields: Name, Email, Category and Year";
}

if (!isset($error_message) && $_POST["address"]!="") {
    $error_message = "Bad form input";
}

require("inc/phpmailer/class.phpmailer.php");

$mail = new PHPMailer;

if(!isset($error_message) && !$mail -> ValidateAddress($email)) {
    $error_message="Invalid Email Address";
}

if(!isset ($error_message)) {
    $email_body = "";
    $email_body . = "お名前 " .$name. "\n";
    $email_body . = "電子メール " .$email. "\n";
    $email_body . = "カテゴリー " .$category. "\n";
    $email_body . = "タイプ " .$format. "\n";
    $email_body . = "築年数 " .$year. "\n";
    $email_body . = "備考欄 " .$details. "\n";

    $mail->setFrom($email, $name);
    $mail->addAddress('koh@gmail.com', 'GangstaCat');     // Add a recipient

    $mail->isHTML(false);                                  // Set email format to HTML
    $mail->Subject = 'Personal Media Library Suggestion from ' . $name;
    $mail->Body = $email_body;

    if ($mail -> send()) {
        header("location:suggest.php?status=thanks");
        exit;
    }

    $error_message = 'Message could not be sent.';
    $error_message .= 'Mailer Error: ' . $mail->ErrorInfo;
}

$pageTitle="お問い合わせ"; 
$section="suggest";

include("inc/header.php"); ?>

Hopefully this will point you in the right direction? As much as I would love to understand it, I haven't got a clue how to read Kanji or any Japanese Script for that matter, so apologies if I've deleted something unbeknownst to me.

Kind regards, Kai

1 Answer

Andrew Dovganyuk
Andrew Dovganyuk
10,633 Points

You forget php code in category value! Here is the solution:

'''php <tr> <th><label for="category">カテゴリー</label></th> <td>

            <select id="category" name="category" >
            <option value=" ">一つ選んでください</option>
            <optgroup label="物件の種類">
                <option value="募集中"<?php if(isset($category) && $category == "募集中"){ echo " Selected";} ?>>募集中</option>
                <option value="入居中"<?php if(isset($category) && $category == "入居中"){ echo " Selected";} ?>入居中</option>
            </optgroup>
            </select>
            </td>
        </tr>

'''

Hope it helps