php - 如何让 php 验证适用于表单?
问题描述
我需要在表单发送到会话数组之前对其进行验证,这就是我目前所拥有的。
使用PHP_SELF
表单被发送到需要验证输入的输入是否正确和有效的索引。
在提交表单之前会回显错误,并且每次提交错误输入时,其他消息都不会回显。页面刷新
任何帮助都会很棒,我已经禁用了我的 javascript 验证来测试它,并且功能只有一半
PHP代码:
<?php
// define variables and set to empty values
$nameErr = $emailErr = $mobileErr = $creditCardErr = $expiryErr = "";
$name = $email = $mobile = $creditCard = $expiry = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
function validate($str) {
return trim(htmlspecialchars($str));
}
//Valikdating User name
if (empty($_POST["cust[name]"])) {
$nameErr = "Name is required";
} else {
$name = validate($_POST["cust[name]"]);
if (!preg_match("/\b([A-Z]{1}[a-z]{1,30}[- ]{0,1}|[A-Z]{1}[- \']{1}[A-Z]{0,1}
[a-z]{1,30}[- ]{0,1}|[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}){2,5}/",$name)) {
$nameErr = "Only Western names, letters and white spaces allowed";
}
}
if (empty($_POST["cust[email]"])) {
$emailErr = "Email is required";
} else {
$email = validate($_POST["cust[email]"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["cust[mobile]"])) {
$mobileErr = "Mobile Number is required";
} else {
$mobile = validate($_POST["cust[mobile]"]);
//check if mobile phone is inputted correctly
if (!preg_match("/^(\(04\)|04|\+614)( ?\d){8}$/",$mobile)) {
$mobilelErr = "Invalid Mobile Number";
}
}
if (empty($_POST["cust[card]"])) {
$creditCardErr = "Credit Card is required";
} else {
$creditCard = validate($_POST["cust[card]"]);
//check if all credit card types are valid
if (!preg_match("/^(\d){16}$/",$creditCard)) {
}else if(!preg_match("/^(\d){4}\s{1}(\d){4}\s{1}(\d){4}\s{1}(\d){4}$/",$creditCard)) {
}else if(!preg_match("/^(\d){4}\-{1}(\d){4}\-{1}(\d){4}\-{1}(\d){4}$/",$creditCard)) {
$creditCardErr = "Invalid Credit Card";
}
}
//This needs to be fixed
if (empty($_POST["cust[expiry]"])) {
$expiryErr = "Expiry is required";
} else {
$expiry = validate($_POST["cust[expiry]"]);
//check if expiry is valid and a month ahead
if (!preg_match("",$expiry)) {
$expiryErr = "Invalid date, select month onwards";
}
}
}
?>
HTML 表单代码:
<div id="bookingsCard">
<form id="seatform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<!-- Customer Details -->
<div class="CustomerDetails">
<label for="CustomerDetails"><b>CUSTOMER DETAILS</b></label>
<br><br>
<label for="Name"><b>Name</b></label>
<input type="text" name="cust[name]" id="cust-name" value="<?php echo $name; ?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<label for="Email"><b>Email</b></label>
<input type="email" name="cust[email]" id='cust-email' value="<?php echo $email; ?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<label for="Mobile"><b>Mobile</b></label>
<input type="tel" name="cust[mobile]" id='cust-mobile' value="<?php echo $mobile; ?>">
<span class="error">* <?php echo $mobileErr;?></span>
<br><br>
<label for="Credit Card"><b>Credit Card</b></label>
<input type="text" name="cust[card]" id='cust-card' value="<?php echo $creditCard; ?>">
<span class="error">* <?php echo $creditCardErr;?></span>
<br><br>
<label for="Expiry"><b>Expiry</b></label>
<input type="month" name="cust[expiry]" id='cust-expiry'
pattern="[0-9]{4}-[0-9]{2}" value="<?php echo $expiry; ?>">
<span class="error">* <?php echo $expiryErr;?></span>
<br><br>
</div>
<hr>
<label><b>Total Amount</b></label>
<input name="total" id="total" type="text" readonly>
<br><br>
<button type="submit" class="bookbtn" name="order" value='order' id='bookbtn' >Book Now!</button>
</form>
</div>
解决方案
你可以做这样的事情:对于 html: <input type="text" required name="custname" id="cust-name" value="<?php echo $name; ?>"><span class="error">* <?php echo $nameErr;?></span>
不知道你为什么使用 cust[name] 但我使用了简化的名称,并添加了 html 5 验证required
,这让生活更轻松。现在提交表单 php 后:
if ( isset($_POST["name"]) && !empty($_POST["name"])) {
$name = $_POST["name"];
//your other validation
} else {
$nameErr = "Name is required/ not set";
}
//希望它可以帮助您快乐编码
推荐阅读
- python - 在 VS 代码 Jupyter Notebook 中创建目录
- c++ - 在数组 C++ 中交换字符
- sql - 如何选择2021年1月1日到上周的记录
- nlog - NLog 致命消息从未记录?
- javascript - 链接到页面上的部分
- python - Python CSV 字典读取,每次都输入同一行(最后一行)
- python - 获取创建指定类的对象的类的名称
- python - Numpy:根据条件有效地将数组相乘
- json - “应为项目列表,但输入 \"str\"。” 在 django 休息框架中
- javascript - 承诺 {
} with PromiseState "fulfilled" with forEach 和 async