i have a feedback form in a website (created for school). but input are coming bad words and bad links. what will do next (website page in php). i need like when anyone entering the bad words then error message should be displayed else success message and data pass to database.if any one can help please help me and i am attaching my code here with this question.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="" method="POST">
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Name</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="name" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Email ID</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="email" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Mobile Number</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="mobile" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Resident</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="resident" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Enquiry</label>
</div>
<div class="col-lg-5 col-md-5">
<textarea type="text" rows="5" placeholder="Hit Your Enquiry" minlength="3" maxlength="150" name="enquirymessage" class="form-control form-control-sm" required></textarea>
</div>
</div>
<button name="submitedenquiry" name="submitedenquiry"class="btn btn-success mb-2 " >Submit</button>
</form>
<?php
if (isset($_POST['submitedenquiry']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$resident=$_POST['resident'];
$enquirymessage=$_POST['enquirymessage'];
$date = date('m/d/Y h:i:s a', time());
$abc=mysqli_query($conn,"insert into enquiry(name,email,mobile,resident,enquiry,date)
values('$name','$email','$mobile','$resident','$enquirymessage','$date')");
if($abc>0)
echo '<script>swal({
title: "Successfull",
text: "We will contact you soon.",
icon: "success",
}).then(function(){window.location="contact-us.php";});</script>';
else
echo '<script>swal({
title: "Ooops....",
text: "Error.Please Try Again",
icon: "error",
}).then(function(){window.location="contact-us.php";});</script>';
}
?>
</body>
</html>
i tried this code but not worked
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="" method="POST">
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Name</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="name" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Email ID</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="email" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Mobile Number</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="mobile" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Resident</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="resident" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Enquiry</label>
</div>
<div class="col-lg-5 col-md-5">
<textarea type="text" rows="5" placeholder="Hit Your Enquiry" minlength="3" maxlength="150" name="enquirymessage" class="form-control form-control-sm" required></textarea>
</div>
</div>
<button name="submitedenquiry" name="submitedenquiry"class="btn btn-success mb-2 " >Submit</button>
</form>
<?php
if (isset($_POST['submitedenquiry']))
{
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$resident=$_POST['resident'];
$enquirymessage=$_POST['enquirymessage'];
$date = date('m/d/Y h:i:s a', time());
function contains($string, $array, $caseSensitive = false) {
$stripedString = $caseSensitive ? str_replace($array, '', $string) : str_ireplace($array, '', $string);
return strlen($stripedString) !== strlen($string);
}
$badwords=array("fuxk","rxpe","porn");
$badwordcount=0;
if ( contains($name, $badwords) ||contains($mobile, $badwords) ||contains($resident, $badwords) || contains($enquirymessage, $badwords) ) {
$badwordcount++;
}
if ($badwordcount!=0) {
$abc=0;
} else {
$sql="insert into enquiry(name,mobile,resident,enquiry,date) values(?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssss",$name,$mobile,$resident,$enquirymessage,$date);
$stmt->execute();
$abc=$stmt->affected_rows;
}
if($sql>0)
echo '<script>swal({
title: "Successfull",
text: "We will contact you soon.",
icon: "success",
}).then(function(){window.location="contact-us.php";});</script>';
else
echo '<script>swal({
title: "Ooops....",
text: "Something Went Wrong. Please Try Again",
icon: "error",
}).then(function(){window.location="contact-us.php";});</script>';
}
?>
</body>
</html>
but it worked here. is there any problem while code like this
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="" method="POST">
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Name</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="name" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Email ID</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="email" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Mobile Number</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="mobile" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Resident</label>
</div>
<div class="col-lg-5 col-md-5">
<input type="text" name="resident" class="form-control form-control-sm" required>
</div>
</div>
<div class="form-row row">
<div class="col-lg-4 col-md-5">
<label style="font-size: 14px;font-weight: bold;" for="">Enquiry</label>
</div>
<div class="col-lg-5 col-md-5">
<textarea type="text" rows="5" placeholder="Hit Your Enquiry" minlength="3" maxlength="150" name="enquirymessage" class="form-control form-control-sm" required></textarea>
</div>
</div>
<button name="submitedenquiry" name="submitedenquiry"class="btn btn-success mb-2 " >Submit</button>
</form>
<?php
if (isset($_POST['submitedenquiry']))
{
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$resident=$_POST['resident'];
$enquirymessage=$_POST['enquirymessage'];
$date = date('m/d/Y h:i:s a', time());
function contains($string, $array, $caseSensitive = false) {
$stripedString = $caseSensitive ? str_replace($array, '', $string) : str_ireplace($array, '', $string);
return strlen($stripedString) !== strlen($string);
}
$badwords=array("fuxk","rxpe","porn");
$badwordcount=0;
if ( contains($name, $badwords) ||contains($mobile, $badwords) ||contains($resident, $badwords) || contains($enquirymessage, $badwords) ) {
$badwordcount++;
}
if ($badwordcount!=0) {
$abc=0;
} else
$abc=mysqli_query($conn,"insert into enquiry(name,mobile,resident,enquiry,date)
values('$name','$mobile','$resident','$enquirymessage','$date')");
if($abc>0)
echo '<script>swal({
title: "Successfull",
text: "We will contact you soon.",
icon: "success",
}).then(function(){window.location="contact-us.php";});</script>';
else
echo '<script>swal({
title: "Ooops....",
text: "Something Went Wrong. Please Try Again",
icon: "error",
}).then(function(){window.location="contact-us.php";});</script>';
}
?>
</body>
</html>