0

I'm pretty new to html, php, mysql and i have to like learn the basics @ my new workplace. I'm having an annoying problem with my Form Validation. I'm using ubuntu server in combination with PuTTY

My problem is: that my 'Validation' and 'empty Field' check is not working propperly.

So when i go into my browser, my Form (Table) shows up as it should. When I hit the Submit button WITHOUT writing any stuff into the fields, the Form stays on the page and my Errors appear: ("Name is required, email, Nachname") That's right so far.

But when i fill in anything into the field(s), and then hit the Submit button, the form just disappears and i get like a blank page (but still having my CSS background n stuff).

No matter if comes up to the requirements, or not.

I'm trying to find out whats wrong since 3 whole days 9hrs/day @ my workplace. So hopefully anyone of you can help me finally get this thing work.

everything i post now is in the same order as i have it in my PuTTy (nano)

My script starts like this:

CSS:

<html>
  <head>
    <title> Formular FINAL </title>

<style>
body {
    background-image: url("http://fewpict.com/images/background-pictures/background-pictures-01.jpg");
}

.db_table {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    overflow: hidden;
    overflow-y: auto;
    position: fixed;
    top: 80%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 50%;
    height: 100px;
}

.db_table td, tr {
    color: white;
    text-align: center;
}
.center_div {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
.center_div td {
    font-family: "Comic Sans", Comic Sans MS, cursive;
    color: white;
    text-align: left;
}
.error {color: #FF0000;}



</style>


</head>
<body>

PHP-Form Validation:

<?php


    $VornameErr = "";
    $emailErr = "";
    $NachnameErr = "";
    $Vorname = $_POST['Vorname'];
    $email = $_POST['email'];
    $Nachname = $_POST['Nachname'];
    $allesok = "";

     //input type hidden
if(isset($_POST['action'])){

    //ÜBERPRÜFUNGSVARIABLE
$allesok = 1;


$errors = array();

if (empty($_POST) === false) {
    $required_fields = array('Vorname', 'Nachname', 'email');
    foreach($_POST as $key=>$value) {
            if (empty($value) && in_array($key, $required_fields) === true ){
                    $errors[] = 'Fields marked with an asterisk are required';
                    break 1;
            }
    }
}

    //Vorname Überprüfen
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Vorname"])) {
            $allesok = 0;$VornameErr = "Name is required";
    } else {
            $Vorname = test_input($_POST["Vorname"]);
            if (!preg_match("/[a-zA-Z]{3,}/",$Vorname)) {
                    $allesok = 0;$VornameErr = "Only letters and atleast 3  alpha characters Allowed";
            }
    }
}
    if (empty($_POST["email"])) {
            $allesok = 0;$emailErr = "Email is required";
            } else {
                    $email = test_input($_POST["email"]);
                                    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                                    $allesok = 0;$emailErr = "Invalid email format";
            }
    }


    //Nachname Überprüfen
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Nachname"])) {
           $allesok = 0; $NachnameErr = "Nachname is required";
    } else {
            $Nachname = test_input($_POST["Nachname"]);
            if (!preg_match("/[a-zA-Z]{3,}/",$Nachname)) {
                    $allesok = 0;$NachameErr = "Only letters and atleast 3 alpha characters Allowed";

            }
    }
}

function check_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
    }
}

MySQL:

if ($allesok) {

define('DB_NAME', 'formular');
define('DB_USER', 'David');
define('DB_PASSWORD', '****');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected)  {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}


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



    $value1 = $_POST['Vorname'];
    $value2 = $_POST['Nachname'];
    $value3 = $_POST['email'];

    $sql = "INSERT INTO formular (Vorname, Nachname, email) VALUES ('$value1', '$value2', '$value3')";

     if (!mysql_query($sql))  {
            die('Error: ' . mysql_error());
    } else {
            $msg1='<p> Your information was submitted successfully.</p>';
    }


}

Echo Form:

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


?>
<div class="center_div">
<table>
<tr>
     <td style="width: 200px;">Vorname: </td>
     <td style="border-bottom: 1px solid black;"><?php echo $_POST['Vorname']; ?> </br></td>
</tr>
<tr>
     <td style="width: 200px;">Nachname: </td>
     <td style="border-bottom: 1px solid black;"><?php echo $_POST['Nachname']; ?> </br></td>
</tr>
<tr>
     <td style="width: 200px;">E-Mail: </td>
     <td style="border-bottom: 1px solid black;"><?php echo $_POST['email']; ?> </br> </td>
</tr>
</table>
            <input type="button" value="Zur&uuml;ck" onClick="history.back();">
</div>
<?php

    echo $msg1."<br /><br /><br />";

    //Liste anzeigen
} elseif(isset($_POST['show_table'])) {


            //fake formular <-----was made for still having the possibility to fill out stuff when i view the LIST
    echo "<div class='center_div'>";
    echo "<form action='toto2.php' method='POST'/>";
            echo"<table>";
                   echo "<tr>";
                           echo "<th></th>";
                           echo "<th></th>";
                           echo "<th>span class='error'>* required field.</span></th>";

                   echo "<tr>";
                           echo "<td style= 'width: 200px;' > Vorname:* </td>";
                           echo "<td> <input type='text' name='Vorname' placeholder='Your Vorname...' /></td>";
                           echo "<td><span class='error'>*$VornameErr </span></td>";
                    echo "</tr>";
                    echo "<tr>";
                            echo "<td style='width: 200px;'> Nachname:* </td>";
                            echo "<td> <input type='text' name='Nachname' placeholder='Your Nachname...' /></td>";
                            echo "<td><span class='error'>*$NachnameErr</span></td>";
                    echo "</tr>";
                    echo "<tr>";
                            echo "<td style='width: 200px;'> E-Mail:* </td>";
                            echo "<td><input type='email' name='email' placeholder='Your E-Mail address...' /></td>";
                            echo "<td><span class='error'>*$emailErr</span></td>";
                   echo "</tr>";
            echo "</table>";
            echo "<input type='submit' value='SEND' name='sent' />";
            echo "<input type='submit' value='Eintr&auml;ge anzeigen' name='show_table' />";
            echo "<input type='button' value='Eintr&auml;ge ausblenden' onClick='history.back();'>";
    echo "</div>";
    echo "</form>";


            //DB Tabelle
    $query = "SELECT * FROM formular;";
    $result = mysql_query($query);

    echo '<div class="db_table">';
    echo '<table>';
    echo '<tr>';
    echo '<th>ID</th>';
    echo '<th>Vorname</th>';
    echo '<th>Nachname</th>';
    echo '<th>email</th>';
    echo '</tr>';
    while($row = mysql_fetch_row($result)) {

            echo "<tr>";
            echo "<td>".$row[0]."</td>";
            echo "<td>".$row[1]."</td>";
            echo "<td>".$row[2]."</td>";
            echo "<td>".$row[3]."</td>";
            echo "</tr>";
    }
    echo '<tr>';
    echo '<td>';
    echo '<input type="button" value="Zur&uuml;ck" onClick="history.back();">';
    echo '</td>';
    echo '</tr>';
    echo '</table>';
    echo '</div>';



    }
}  else {

?>

HTML Form:

<div class="center_div">
    <span class="error"></span>
    <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <table>
                    <tr>
                            <th></th>
                            <th></th>
                            <th><span class="error">* required field.</span></th>

                    <tr>
                            <td style= "width: 200px;" > Vorname:* </td>
                            <td> <input type="text" name="Vorname" placeholder="Your Vorname..." /></td>
                            <td><span class="error">* <?php echo $VornameErr;?></span></td>
                    </tr>
                    <tr>
                            <td style="width: 200px;"> Nachname:* </td>
                            <td> <input type="text" name="Nachname" placeholder="Your Nachname..." /></td>
                            <td><span class="error">* <?php echo $NachnameErr;?></span></td>
                    </tr>
                    <tr>
                            <td style="width: 200px;"> E-Mail:* </td>
                            <td><input type="text" name="email" placeholder="Your E-Mail address..." /></td>
                            <td><span class="error">* <?php echo $emailErr;?></span></td>
                    </tr>
            </table>
            <input type="hidden" name="action" value="1">
            <input type="submit" value="SEND" name="sent" />
            <input type="submit" value="Eintr&auml;ge anzeigen" name="show_table" />
            <input type="button" value="Eintr&auml;ge ausblenden" onClick="history.back();">
    </form>
</div>
<?php
}

mysql_close();
?>

</body>
</html>
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
SoliaK
  • 9
  • 2
  • That is a lot of code to expect us all to PLOUGH through. Can you not produce a simple example that demonstrates the problem – RiggsFolly Jan 22 '16 at 10:45
  • I think your problem can be solved by making an verification client side too. By doing this you won't have to redirect, which result in you leaving the page on incorrect information. – Oliver Nybroe Jan 22 '16 at 10:46
  • Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Jan 22 '16 at 10:46
  • sorry for these 1000 lines.. but i need to get this done today. its my last day of my internship.. so it has to work. its very important for me – SoliaK Jan 22 '16 at 10:51
  • In HTML form in your form, your action can be empty, as of HTML5 the field is not required to have any data in it. If the action field is empty it will redirect to the current site. – Oliver Nybroe Jan 22 '16 at 10:51
  • when i check out: tail -f /var/log/apache2/error.log: its says to me: undefined function test_input (fatal error) undefined index: Vorname (php notice) " " Email " " Nachname mysql_close(): no MySQL-Link resource supplied – SoliaK Jan 22 '16 at 10:56
  • *...i get like a blank page* means something is broken. Add these lines `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top of your PHP scripts and see if it yields any error or not. – Rajdeep Paul Jan 22 '16 at 10:57
  • it displays the same errors i alrdy mentioned – SoliaK Jan 22 '16 at 11:01
  • something is wrong with the: $Vorname = test_input($_POST["Vorname"]); in the PHP-Form Validation part – SoliaK Jan 22 '16 at 11:04
  • ok i changed the function check_input($data) { to: function test_input($data) { – SoliaK Jan 22 '16 at 11:07

0 Answers0