I have a validation for my contact form that adds a red text font under required inputs. But what I really want is to add a red border-bottom to the bottom of the input to show it needs to be required. I have an already working php switch case but can not find the answer anywhere to add css classes to different cases. I hope someone knows more about this than I do.
Here is my php
<?php
session_start();
// define variables and set to empty values
$nameErr = $emailErr = $phoneErr = $humanErr = "";
$Name = $Email = $Phone = $Human = "";
$hasError = false;
$sent = false;
if(isset($_POST['submit'])) {
$Name = trim(htmlspecialchars($_POST['Name'], ENT_QUOTES));
$FName = trim($_POST['FiancesName']);
$Email = trim($_POST['Email']);
$DesiredWedDate = trim($_POST['DesiredWedDate']);
$WeddingSize = trim($_POST['WeddingSize']);
$Phone = trim($_POST['Phone']);
$IndoorCeremony = trim($_POST['IndoorCeremony']);
$OutdoorCeremony = trim($_POST['OutdoorCeremony']);
$AlcoholYes = trim($_POST['AlcoholYes']);
$AlcoholNo = trim($_POST['AlcoholNo']);
$Human = trim($_POST['Human']);
$Number = 6;
$fieldsArray = array(
'Name' => $Name,
'Email' => $Email,
'Phone' => $Phone,
'Human' => $Human
);
$errorArray = array();
foreach($fieldsArray as $key => $val) {
switch ($key) {
case 'Name':
if(empty($val)) {
$hasError = true;
$nameErr = "Please enter your name.";
}
case 'Name':
if (!preg_match("/^[a-zA-Z ]*$/", ($val))) {
$hasError = true;
$nameErr = "The value must be alphanumeric.";
}
break;
case 'Phone':
if (!preg_match("/^[0-9]+$/", ($val))) {
$hasError = true;
$phoneErr = "Only numbers and white space allowed.";
}
case 'Phone':
if(empty($val)) {
$hasError = true;
$phoneErr = "Phone is required.";
}
break;
case 'Email':
if(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
$hasError = true;
$emailErr = "Email is required.";
} else {
$Email = filter_var($Email, FILTER_SANITIZE_EMAIL);
}
break;
case 'Human':
if (!preg_match("/[^\d]?6[^\d]?/", ($val))) {
$hasError = true;
$humanErr = "Not the right answer";
}
case 'Human':
if (!preg_match("/^[0-9]+$/", ($val))) {
$hasError = true;
$humanErr = "Must be a number";
}
case 'Human':
if(empty($val)) {
$hasError = true;
$humanErr = "Are you human?";
}
break;
}
}
//CHECK BOX WRITE UP
if (isset($_POST['IndoorCeremony'])) {
$checkBoxValue = "yes";
//is checked
} else {
$checkBoxValue = "no";
//is unchecked
}
if (isset($_POST['OutdoorCeremony'])) {
$checkBoxValue = "yes";
//is checked
} else {
$checkBoxValue = "no";
//is unchecked
}
if (isset($_POST['AlcoholYes'])) {
$checkBoxValue = "yes";
//is checked
} else {
$checkBoxValue = "no";
//is unchecked
}
if (isset($_POST['AlcoholNo'])) {
$checkBoxValue = "yes";
//is checked
} else {
$checkBoxValue = "no";
//is unchecked
}
//Validation Success!
//Do form processing like email, database etc here
if($hasError !== true) {
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
//FOR STYLING EMAIL
// $headers .= "MIME-Version: 1.0" . "\r\n";
//$headers .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
//STYLING EMAIL
/* $message = "<html>
<h1>
$Name
</h1>
<BR>
<h3>
$Email
<BR>Tel: $Phone
<BR>Company: $Compnay
<BR>Website: $Website
<BR>Subject: $Subjectmatter
<BR>Describe: $Describe
</h3>
<BR>
<BR>
<BR><h4>Web Design: $webdesign
<BR>Web Hosting: $webhosting
<BR>Wordpress Design: $wordpressdesign
<BR>Logo Design: $logodesign
<BR>Brochures: $brochures</h4>
<BR>
<BR>
<h4>
Other: $otherswitch
<BR>Describe: $OtherDescribe
</h4>
</html>";
*/
$formcontent=" From: $Name \n \n Fiance's Name: $FName \n \n Email: $Email \n \n Phone: $Phone \n \n Desired Wedding Date: $DesiredWedDate \n \n Wedding Size: $WeddingSize \n \n Describe: $Describe \n \n Indoor Ceremony: $IndoorCeremony \n \n Outdoor Ceremony: $OutdoorCeremony \n \n Alcohol Yes: $AlcoholYes \n \n Alcohol No $AlcoholNo \n \n Referral: $Referral \n ";
$recipient = "Youremail@email.com";
$subject = "Pre Book Wedding Contact Form";
$mailheader = "From: $Email \r\n";
mail($recipient, $subject, $formcontent, $mailheader /*$message, $headers*/);
header("Refresh:0; url=thanks.php");
exit();
}
}
?><!--END PHP-->
Here is my input form
<span class="input input--kaede">
<input name="FiancesName" class="input__field input__field--kaede" type="text" id="input-2" />
<label class="input__label input__label--kaede" for="input-2">
<span class="input__label-content input__label-content--kaede">Fiance's Name</span>
</label>
</span>
The css I want to add I called it
.under_text_error {
border-bottom: 2px solid red;
}
that should hopefully give someone enough to go off of. I appreciate any possible help!
Hope this helps
<?php
if(!empty($nameErr))
{
?>
<div class="under_text_error">
<?php echo $nameErr ?>
</div>
<?php
}
?>
Here is the solution
<span class="input input--kaede <?php echo $FnameErr ?>">
<?php
if(!empty($FnameErr))
{
?>
<div class="under_text_error">
</div>
<?php
}
?>
<input name="FName" class="input__field input__field--kaede " type="text" id="input-2" value="<?php echo (isset($FName) ? $FName : ""); ?>"/>
<label class="input__label input__label--kaede " for="input-2">
<span class="input__label-content input__label-content--kaede ">Fiance's Name</span>
</label>
</span>
Just echo the error class if the field has an error
Something like:
<input class="input__field input__field--kaede <?=isset($nameErr)? 'under_text_error':''?>" />
Okay here is what I tried.
<span class="input input--kaede <?php echo $nameErr ?>">
<?php
if(!empty($nameErr))
{
?>
<div class="under_text_error">
</div>
<?php
}
?>
<input name="Name" class="input__field input__field--kaede " type="text" id="input-2" />
<label class="input__label input__label--kaede " for="input-2" value="<?php echo (isset($Name) ? $Name : ""); ?>">
<span class="input__label-content input__label-content--kaede ">Fiance's Name</span>
</label>
</span>
What this ends up doing is putting the red line on the input and when i fill it in and submit it disappears like its suppose to. The problem now is that the input information (what you type in disappears) it doesn't keep it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.