[英]How to show success message if there is no empty field after form submission in php
I have made an html form and validate it with php or more precisely part of validation (just check rquired field) Here is the html codes: 我已经制作了一个html表单,并使用php或更确切地说是验证的一部分进行了验证(只需检查必填字段),这是html代码:
<div class="container">
<div class="row">
<div class="col-md-6">
<h3>Registration Form</h3><br>
<form action="#" method="post">
<div class="form-group">
<label for="name">Name : * </label><span class="error"><?php echo $nameErr; ?></span>
<input id="name" type="text" name="name" class="form-control" placeholder="Type your name here">
</div>
<div class="form-group">
<label for="username">Username : * </label><span class="error"><?php echo $usernameErr; ?></span>
<input id="username" type="text" name="username" class="form-control" placeholder="Type your username here">
</div>
<div class="form-group">
<label for="password">Password : * </label><span class="error"><?php echo $passwordErr; ?></span>
<input id="password" type="password" name="password" class="form-control" placeholder="Type password here">
</div>
<div class="form-group">
<label for="rpassword">Retype Password : * </label><span class="error"><?php echo $rpasswordErr; ?></span>
<input id="rpassword" type="password" name="rpassword" class="form-control" placeholder="Retype password here">
</div>
<div class="form-group">
<label for="email">Email : * </label><span class="error"><?php echo $emailErr; ?></span>
<input id="email" type="email" name="email" class="form-control" placeholder="Type your email here">
</div>
<div class="form-group">
<label for="phone">Phone No : * </label><span class="error"><?php echo $phoneErr; ?></span>
<input id="phone" type="text" name="phone" class="form-control" placeholder="Type your phone here">
</div>
<div class="form-group">
<label for="dob">Date of Birth : * </label><span class="error"><?php echo $dobErr; ?></span>
<input id="dob" type="date" name="dob" class="form-control">
</div>
<div class="form-group">
<label>Select A Country : * </label><span class="error"><?php echo $countryErr; ?></span>
<input id="country" type="text" name="country" class="form-control not-me">
</div>
<div class="form-group">
<label for="gender">Please Select Your Gender : *</label><span class="error"><?php echo $genderErr; ?></span>
<div class="form-control gender">
<input type="radio" id="male" name="gender">
<label for="male" class="exclude">Male</label>
<input type="radio" id="female" name="gender">
<label for="female" class="exclude">Female</label>
</div>
</div>
<input type="submit" name="submit" class="custom-btn" value="Submit" name="reg-form">
</form>
</div>
</div>
</div>
And here is the php code to check if any of the field is empty: 这是用于检查任何字段是否为空的php代码:
<?php
$nameErr = $usernameErr = $passwordErr = $rpasswordErr = $emailErr = $phoneErr = $dobErr = $countryErr = $genderErr = "";
$name = $username = $password = $rpassword = $email = $phone = $dob = $country = $gender = "";
if(isset($_POST["submit"])) {
if (empty($_POST["name"])) {
$nameErr = " This field is required";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["username"])) {
$usernameErr = " You must enter a username";
}
else {
$username = $_POST["username"];
}
if (empty($_POST["password"])) {
$passwordErr = " You must type a strong password";
}
else {
$password = $_POST["password"];
}
if (empty($_POST["rpassword"])) {
$rpasswordErr = " Your password didn't match";
}
else {
$rpassword = $_POST["rpassword"];
}
if (empty($_POST["email"])) {
$emailErr = " This is not a valid email address";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["phone"])) {
$phoneErr = " Please enter your contact number";
}
else {
$phone = $_POST["phone"];
}
if (empty($_POST["dob"])) {
$dobErr = " You must fill your Date of Birth";
}
else {
$dob = $_POST["dob"];
}
if (empty($_POST["country"])) {
$countryErr = " You didn't select a country";
}
else {
$country = $_POST["country"];
}
if (empty($_POST["gender"])) {
$genderErr = " You can not leave this field empty";
}
else {
$gender = $_POST["gender"];
}
}
?>
Now I simply want to show a success message after submission the form if there is no empty field. 现在,如果没有空字段,我只想在提交表单后显示成功消息。
NB: This is my first php work. 注意:这是我的第一个php工作。 Thanks in advance!
提前致谢!
simplest solution would be to use simple flag variable, proof of concept using your code: 最简单的解决方案是使用简单的标志变量,并使用您的代码进行概念验证:
<?php
$isFullyFilled = true; //flag
$nameErr = $usernameErr = $passwordErr = $rpasswordErr = $emailErr = $phoneErr = $dobErr = $countryErr = $genderErr = "";
$name = $username = $password = $rpassword = $email = $phone = $dob = $country = $gender = "";
if(isset($_POST["submit"])) {
if (empty($_POST["name"])) {
$nameErr = " This field is required";
$isFullyFilled = false; //this is line which changes flag
}
else {
$name = $_POST["name"];
}
.... //many other ifs modified like that
if($isFullyFilled){
showMessage("Form is fully filled");
}
add the following lines to the bottom of your php code: 将以下行添加到您的php代码的底部:
echo '<script type="text/javascript">';
echo 'alert("the form has been completed")';
echo '</script>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.