MCoyle
MCoyle

Reputation: 35

PHP - Session error messages not showing

I have a page with a contact form. I'm trying to make some error messages appear next to the fields if the fields are empty/invalid. If all fields are OK the field's contents are sent to a database.

The validation itself works. If the fields are empty or invalid it does not submit them into the database. However no error messages are shown next to the fields.

Here are the codes

Page with the contact form named palaute3.php:

<?php
session_start();



$_SESSION["nimiVirhe"]="";
$_SESSION["spostiVirhe"]="";
$_SESSION["palauteVirhe"]="";


$servername = "localhost";
$username = "username";
$password = "passwd";
$dbname = "dbname";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Game Over</title>
                <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
                <link rel="stylesheet" type="text/css" href="tyyli1.css"/>
                <style type="text/css">
                @import url('https://fonts.googleapis.com/css?family=Press+Start+2P');
                </style> 
    </head>
    <body>



        <div id="container">

            <div id="header">
            </div>
            <div id="menuBG">
            </div>

            <ul class="navlist">
                <li><a href="index.html">Etusivu</a>
                </li>

                <li><a href="leffat.html">Leffat</a>
                </li>

                <li><a href="pelit.html">Pelit</a>
                </li>

                <li><a href="palaute.html" class="active">Ota yhteyttä</a>
                </li>

            </ul>




                <div id="content">



                <h3>Palaute</h3>
                    <p>Anna Palautetta sivujen ulkonäöstä tai vinkkejä uusiksi arvosteluiksi!</p>

                    <p><span class="error">Kaikki kentät ovat pakollisia</span></p>
                    <form method="post" action="validointi.php">
                    <table width="450px">
                        <tr>
                        <td valign="top">
                            <label for="nimi">Nimi</label>
                        </td>
                        <td valign="top"> 

                            <input type="text" name="nimi" /><span class="error"><?php echo $_SESSION["nimiVirhe"];?></span>
                        </td>
                        </tr>

                        <tr>
                        <td valign="top">
                            <label for="sposti">Sähköposti</label>

                        </td>
                        <td valign="top">
                            <input type="text" name="sposti" /><span class="error"><?php echo $_SESSION["spostiVirhe"];?></span>


                        </td>
                        </tr>

                        <tr>
                        <td valign="top">
                            <label for="palaute">Palaute</label>
                        </td>
                        <td valign="top">


                            <textarea  name="palaute" maxlength="1000" cols="30" rows="6"></textarea><span class="error"><?php echo $_SESSION["palauteVirhe"];?> </span>
                        </td>
                        </tr>

                        <tr>
                        <td colspan="2" style="text-align:center">
                        <input type="submit" value="Lähetä" name="submit">
                        </td>
                        </tr>


                        </table>

                    </form>
                    <?php
                    session_unset();
                    ?>







                    <p>
                    Palautteet tähän
                    </p>


<?php


$sql = "SELECT id, palaute FROM dbtable";


$result = mysqli_query($conn, $sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Palaute: " . $row["palaute"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();



 ?>






                </div>


                <div id="footer">
                    <p>© Game Over 2018 All rights reserved</p>
                </div>
        </div>
        </body>
</html>

Here is the validation:

<?php
session_start();
 $_SESSION["nimiVirhe"]="";
 $_SESSION["spostiVirhe"]="";
 $_SESSION["palauteVirhe"]="";

if (isset($_POST['submit'])) {

   if (empty($_POST["nimi"])) {
     $_SESSION["nimiVirhe"] = " Nimi on pakollinen.";
   }
   else {
     $nimi = $_POST["nimi"];
     $_SESSION["nimi"]=$nimi;
     if (!preg_match("/^[a-zA-Z ]*$/",$nimi)) {
       $_SESSION["nimiVirhe"] = " Nimi on väärässä muodossa.";
     }
   }

   if (empty($_POST["sposti"])) {
        $sposti = $_SESSION["sposti"];
        $_SESSION["spostiVirhe"] = " Sähköposti on pakollinen.";
   } 
   else {
        $sposti = $_POST["sposti"];
        $_SESSION["sposti"]=$sposti;
        if (!filter_var($sposti, FILTER_VALIDATE_EMAIL)) {
            $_SESSION["spostiVirhe"] = " Sähköpostiosoite on väärässä muodossa.";
        }
   }

   if (empty($_POST["palaute"])) {
        $_SESSION["palauteVirhe"] = "<br>Palaute on pakollinen.";
   } 
   else {
        $palaute = $_POST["palaute"];
        $_SESSION["palaute"]=$palaute;
   }



    if($_SESSION["nimiVirhe"] == "" && $_SESSION["spostiVirhe"] == "" && $_SESSION["palauteVirhe"] == ""){
        header("Location: yhteystesti.php");
        return;
    } else {
        header("Location: palaute3.php");
        return;
    }



   }


?>

The database insertion works fine so I'm not going to include it here.

Any idea what am I doing wrong here?

PS: This is a school assignment and the teacher tried to make it work but couldn't.

Upvotes: 0

Views: 382

Answers (1)

Thomas G
Thomas G

Reputation: 10206

Ath the top of your palaute3.php file you have this

$_SESSION["nimiVirhe"]="";
$_SESSION["spostiVirhe"]="";
$_SESSION["palauteVirhe"]="";

So you re-init your session error messages before using this them.

Those 3 lines should be in the validation page, but not in your error page !

Remove that and it will work.

Upvotes: 1

Related Questions