按下“提交”按钮后,表格消失

时间:2016-01-22 10:39:53

标签: php html regex validation

我是html,php,mysql的新手,我不得不喜欢学习基础知识@我的新工作场所。 我的表单验证存在恼人的问题。我将ubuntu服务器与PuTTY结合使用

我的问题是:我的验证'和空场'检查没有正常工作。

因此,当我进入浏览器时,我的表格(表格)会显示出来。当我点击“提交”按钮而没有在字段中写入任何内容时,表单会停留在页面上并显示我的错误:("姓名是必需的,电子邮件,Nachname和#34;)到目前为止这是正确的。

但是当我在字段中填写任何内容,然后点击“提交”按钮时,表单就会消失,我就像一个空白页面(但仍然有我的CSS背景资料)。

无论是否符合要求,

我试图在我的工作场所每天9小时后整天发现错误。 所以希望你们中的任何人都可以帮助我最终让这件事工作。

我现在发布的所有内容与我在PuTTy中的顺序相同 (纳米)

我的脚本开头是这样的:

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验证:

<?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>';
    }


}

回声表:

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表单:

<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>

0 个答案:

没有答案