使用SESSION的多条消息

时间:2017-03-24 13:41:37

标签: php html arrays session

我正在开发一个小项目,我需要验证(服务器端)表单。使用以下代码,它可以很好地显示消息“有问题”或“没有问题”。我现在想要在名称缺失时显示“名称丢失”或“名字无效”等消息。如果发生许多错误,它将显示多条消息。每条消息都必须显示在与表单相同的页面中。我不知道我的解释是否清楚。

<?php
    echo $_SESSION["errorMsg"];
    session_unset("errorMsg");
?>
<form method="post" action="test1.php">
    <table class="contactInformation">
        <tbody>
        <tr>
            <td>
                <label for="">Nom</label>
            </td>
            <td>
                <input type="text" name="lastName"/>
            </td>
        </tr>
        <tr>
            <td>
                <label for="">Prénom</label>
            </td>
            <td>
                <input type="text" name="firstName"/>
            </td>
        </tr>
        </tbody>
    </table>
    <input type="submit" value="Envoyer la commande">
</form>

<?php

if (!empty($_POST["lastName"])) {
    $lastName = trim($_POST["lastName"]);
    if (strlen($lastName) < 2 || 50 < strlen($lastName)) {
        $errors = "The name is not valid";
    }
}
else {
    $errors = "The name is missing";
}

if (!empty($_POST["firstName"])) {
    $firstName = trim($_POST["firstName"]);
    if (strlen($firstName) < 2 || 50 < strlen($firstName)) {
        $errors = "The firstname is not valid";
    }
}
else {
    $errors = "the firstname is missing";
}

if ($errors != "") {
    $_SESSION["errorMsg"] = "There is A problem";
    header("Location: test2.php");
    exit();
}
else {
    $_SESSION["errorMsg"] = "There is NO problem";
    header("Location: test2.php");
    exit();
}
?>

1 个答案:

答案 0 :(得分:1)

如果您只在一个地方显示所有错误,那么解决方案可以是:

SELECT *,
       avg(isup::integer)
          OVER (PARTITION BY itemid,
                             date_trunc('hour', logged AT TIME ZONE 'UTC')
               ) average,
       date_trunc('hour', logged AT TIME ZONE 'UTC') avg_interval
FROM uplog
ORDER BY logged;

┌────┬────────┬──────┬────────────────────────┬────────────────────────┬─────────────────────┐
│ id │ itemid │ isup │         logged         │        average         │    avg_interval     │
├────┼────────┼──────┼────────────────────────┼────────────────────────┼─────────────────────┤
│  6 │      2 │ f    │ 2017-03-23 12:55:00+01 │ 0.00000000000000000000 │ 2017-03-23 11:00:00 │
│  1 │      1 │ t    │ 2017-03-23 12:55:00+01 │ 0.50000000000000000000 │ 2017-03-23 11:00:00 │
│  2 │      1 │ f    │ 2017-03-23 12:57:00+01 │ 0.50000000000000000000 │ 2017-03-23 11:00:00 │
│  3 │      1 │ t    │ 2017-03-23 13:07:00+01 │ 0.66666666666666666667 │ 2017-03-23 12:00:00 │
│  4 │      1 │ f    │ 2017-03-23 13:09:00+01 │ 0.66666666666666666667 │ 2017-03-23 12:00:00 │
│  5 │      1 │ t    │ 2017-03-23 13:50:00+01 │ 0.66666666666666666667 │ 2017-03-23 12:00:00 │
│  7 │      1 │ t    │ 2017-03-23 14:00:00+01 │ 0.50000000000000000000 │ 2017-03-23 13:00:00 │
│  8 │      1 │ f    │ 2017-03-23 14:03:00+01 │ 0.50000000000000000000 │ 2017-03-23 13:00:00 │
└────┴────────┴──────┴────────────────────────┴────────────────────────┴─────────────────────┘
(8 rows)

否则,你可以在数组中犯错:

<?php

$errors = '';
if (!empty($_POST["lastName"])) {
    $lastName = trim($_POST["lastName"]);
    if (strlen($lastName) < 2 || 50 < strlen($lastName)) {
        $errors .= "-The name is not valid. <br/>";
    }
}
else {
    $errors .= "-The name is missing. <br/>";
}

if (!empty($_POST["firstName"])) {
    $firstName = trim($_POST["firstName"]);
    if (strlen($firstName) < 2 || 50 < strlen($firstName)) {
        $errors .= "-The firstname is not valid. <br/>";
    }
}
else {
    $errors .= "-the firstname is missing. <br/>";
}

if ($errors != "") {
    $_SESSION["errorMsg"] = "There is A problem : <br/>".$errors;
    header("Location: test2.php");
    exit();
}
else {
    $_SESSION["errorMsg"] = "There is NO problem";
    header("Location: test2.php");
    exit();
}
?>