簡體   English   中英

使html表單文本字段為必填

[英]Make html form text field required

我有一個關於如何將文本字段中的數據添加到數據庫中SQL表中的問題。 我希望文本字段需要某種數據,以便表中不能有空白數據。 但是,我似乎無法使其正常工作。

下面的代碼顯示了我的問題。

創建用戶的頁面:

 <html> <head> <title>Eksamensprojekt</title> <style> .outer { display: table; position: absolute; top: 0; left: 0; height: 100%; width: 100%; } .middle { display: table-cell; vertical-align: middle; } .login-form { margin-left: auto; margin-right: auto; width: 400px; font-family: Tahoma, Geneva, sans-serif; } .login-form h1 { text-align: center; color: #4d4d4d; font-size: 24px; padding: 20px 0 20px 0; } .login-form input[type="password"], .login-form input[type="text"] { width: 100%; padding: 15px; border: 1px solid #dddddd; margin-bottom: 15px; box-sizing:border-box; } .login-form input[type="submit"] { width: 100%; padding: 15px; background-color: #535b63; border: 0; box-sizing: border-box; cursor: pointer; font-weight: bold; color: #ffffff; margin: 0; } </style> </head> <body> <div class="outer"> <div class="middle"> <div class="login-form"> <h1>Associhunt</h1> <form method="get"> <input type="text" name="firstname" placeholder="First name"><br> <span class="error">* <?php echo $nameErr;?></span> <input type="text" name="email" placeholder="E-mail"><br> <input type="text" name="tlfnr" placeholder="Phone"><br> <input type="text" name="position" placeholder="Current job position"><br> <input type="text" name="username" placeholder="Username"><br> <input type="text" name="password" placeholder="Password"><br> <input type="submit" value="Opret bruger" name="opretbruger"> </form> </div> <div class="login-form"> <form method="get" action="../login/"> <input type="submit" value="Already have an account?"> </form> </div> </div> </div> <?php $conn = new mysqli($servername, $username, $password,$dbnavn); if ($conn->connect_error) die("Fejl: " . $conn->connect_error); if (isset($_GET['opretbruger'])) { $firstname=$_GET['firstname']; if ($firstname===NULL) die(); $lastname=$_GET['lastname']; if ($lastname===NULL) die(); $email=$_GET['email']; if ($email===NULL) die(); $tlfnr=$_GET['tlfnr']; if ($tlfnr===NULL) die(); $position=$_GET['position']; if ($position===NULL) die(); $username=$_GET['username']; if ($username===NULL) die(); $password=$_GET['password']; if ($password===NULL) die(); $sql="INSERT INTO UserData ( firstname, lastname, email, tlfnr, position, username, password ) VALUES ('".$firstname."','".$lastname."','".$email."','".$tlfnr."','".$position."','".$username."','".$password."')"; $conn->query($sql); if ($conn->affected_rows >0 ) { echo "Bruger oprettet!"; } else { echo "Bruger ikke oprettet!"; }; }; ?> </body> </html> 

表創建:

 <?php // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // sql to create table $sql = "CREATE TABLE UserData ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50) NOT NULL, tlfnr INT(8) NOT NULL, position VARCHAR(50), username VARCHAR(40) NOT NULL, password VARCHAR(40) NOT NULL, reg_date TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table UserData created successfully"; } else { echo "Error creating table: " . $conn->error; } $conn->close(); ?> 

您可以使用HTML輸入必填屬性-例如,更改:

<input type="text" name="email" placeholder="E-mail">

<input type="text" name="email" placeholder="E-mail" required>

您是否查看了必需的屬性?

在您的代碼下方,添加了屬性:

 <html> <head> <title>Eksamensprojekt</title> <style> .outer { display: table; position: absolute; top: 0; left: 0; height: 100%; width: 100%; } .middle { display: table-cell; vertical-align: middle; } .login-form { margin-left: auto; margin-right: auto; width: 400px; font-family: Tahoma, Geneva, sans-serif; } .login-form h1 { text-align: center; color: #4d4d4d; font-size: 24px; padding: 20px 0 20px 0; } .login-form input[type="password"], .login-form input[type="text"] { width: 100%; padding: 15px; border: 1px solid #dddddd; margin-bottom: 15px; box-sizing:border-box; } .login-form input[type="submit"] { width: 100%; padding: 15px; background-color: #535b63; border: 0; box-sizing: border-box; cursor: pointer; font-weight: bold; color: #ffffff; margin: 0; } </style> </head> <body> <div class="outer"> <div class="middle"> <div class="login-form"> <h1>Associhunt</h1> <form method="get"> <input type="text" name="firstname" required placeholder="First name"><br> <span class="error">* <?php echo $nameErr;?></span> <input type="text" name="email" required placeholder="E-mail"><br> <input type="text" name="tlfnr" required placeholder="Phone"><br> <input type="text" name="position" required placeholder="Current job position"><br> <input type="text" name="username" required placeholder="Username"><br> <input type="text" name="password" required placeholder="Password"><br> <input type="submit" value="Opret bruger" name="opretbruger"> </form> </div> <div class="login-form"> <form method="get" action="../login/"> <input type="submit" value="Already have an account?"> </form> </div> </div> </div> <?php $conn = new mysqli($servername, $username, $password,$dbnavn); if ($conn->connect_error) die("Fejl: " . $conn->connect_error); if (isset($_GET['opretbruger'])) { $firstname=$_GET['firstname']; if ($firstname===NULL) die(); $lastname=$_GET['lastname']; if ($lastname===NULL) die(); $email=$_GET['email']; if ($email===NULL) die(); $tlfnr=$_GET['tlfnr']; if ($tlfnr===NULL) die(); $position=$_GET['position']; if ($position===NULL) die(); $username=$_GET['username']; if ($username===NULL) die(); $password=$_GET['password']; if ($password===NULL) die(); $sql="INSERT INTO UserData ( firstname, lastname, email, tlfnr, position, username, password ) VALUES ('".$firstname."','".$lastname."','".$email."','".$tlfnr."','".$position."','".$username."','".$password."')"; $conn->query($sql); if ($conn->affected_rows >0 ) { echo "Bruger oprettet!"; } else { echo "Bruger ikke oprettet!"; }; }; ?> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM