<?php
$nameErr = "";
if(isset($_POST["name"])){ //Check form submission
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {//Check Validations
echo "Only letters allowed";
}
elseif(empty($_POST["name"])){
echo "No letter entered";//Check not empty name
}
else {
$veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "onion");
$fruits = array("Apple", "Banana", "orange", "Pineapple", "Grapes", "Watermelon");
$salad = array_merge ($veggies, $fruits);
$Object = $_POST["name"];
$search = array_filter($salad, function($list) use ($Object){
return ( stripos($list, $Object) !== FALSE );
});
print_r($search); //displays the result
}
}
function test_input($data) {
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE HTML>
<html>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter Letter: <input type="text" name="name"><span> <?php echo $nameErr;?></span><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
what if i enter "aaaaaa" and such elements is not in the array, so then it should display match not found, i used in_array in isset statement but its not working , is it proper method to do or is it wrong?