当我填写表格并单击“提交”按钮时,我的验证不显示或不起作用,并且如果验证失败,则应该显示错误。它只是重定向到同一页面(index.php),仅此而已。同样,当我对类进行一些更改时,我的自定义css也不会更改。这是我的代码:
代码结构:
Project
|
+-- includes
| |
| +-- footer.php
| +-- form_process.php
| +-- header.php
+-- public
| |
| +-- main.js
| +-- style.css
+-- vendor
|
+-- composer.json
+-- index.php
style.css
.error
{
color:red;
}
.success
{
color:#ff9966;
text-align:center;
font-weight:bold;
font-size:15px;
}
index.php:
<?php include 'includes/header.php'; ?>
<?php include 'includes/form_process.php'; ?>
<div class="container">
<form id="contact" action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<h2>Contact</h2>
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter company name:" value="<?php $company_name; ?>" name="company_name">
<span class="error"> <?php $company_name_error; ?> </span>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter your name:" value="<?php $user_name; ?>" name="user_name">
<span class="error"> <?php $user_name_error; ?> </span>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter your surname:" value="<?php $user_surname; ?>" name="user_surname">
<span class="error"> <?php $user_surname_error; ?> </span>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter your email:" value="<?php $email; ?>" name="email">
<span class="error"> <?php $email_error; ?> </span>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter your number:" value="<?php $number; ?>" name="number">
<span class="error"> <?php $number_error; ?> </span>
</div>
<div class="form-group">
<textarea type="text" class="form-control" placeholder="Type your message here..." <?php $message; ?> rows="6" name="message"></textarea>
</div>
<div class="form-group">
<button class="btn btn-success" type="submit" name="submit" id="contact-submit">Submit</button>
</div>
<div class="success"> <?php $success; ?> </div>
</form>
</div>
<?php include 'includes/footer.php'; ?>
form_process.php:
<?php
require_once 'vendor/autoload.php';
$company_name_error = $user_name_error = $user_surname_error = $email_error = $number_error = "";
$company_name = $user_name = $user_surname = $email = $number = $message = $success = "";
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["company_name"])) {
$company_name_error = "Company name is required!!!";
} else {
$company_name = test_input($_POST["company_name"]);
if (!preg_match("/^[a-zA-Z ]*$/", $company_name)) {
$company_name_error = "Only letters and white spaces are allowed!!!";
}
}
if (empty($_POST["user_name"])) {
$user_name_error = "User name is required!!!";
} else {
$user_name = test_input($_POST["user_name"]);
if (!preg_match("/^[a-zA-Z ]*$/", $user_name)) {
$user_name_error = "Only letters and white spaces are allowed!!!";
}
}
if (empty($_POST["user_surname"])) {
$user_surname_error = "User surname is required!!!";
} else {
$user_surname = test_input($_POST["user_surname"]);
if (!preg_match("/^[a-zA-Z ]*$/", $user_surname)) {
$user_surname_error = "Only letters and white spaces are allowed!!!";
}
}
if (empty($_POST["email"])) {
$email_error = "Email is required!!!";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email format!!!";
}
}
if (empty($_POST["number"])) {
$number_error = "Number is required!!!";
} else {
$number = test_input($_POST["number"]);
if (!preg_match("/^[0-9+\-\.\/\(\) ]{6,30}$/", $number)) {
$number_error = "Enter phone number from 6 to 30 characters!!!";
}
}
if (empty($_POST["message"])) {
$message = "";
} else {
$message = test_input($_POST['message']);
}
}
答案 0 :(得分:3)
似乎您从未echo
中的任何变量index.php
例如,您在<?php $number_error; ?>
内的<span class="error">
必须是<?= $number_error; ?>
或<?php echo $number_error; ?>