I'm currently working on a program where I have to validate a form on the server-side using PHP. I've been able to write working validation code for and fields but I'm a bit stuck on some parts. I'm pretty new to coding and I'm not sure how to validate that a textbox contains a certain string using PHP. I've tried Googling but nothing seems to be working. Also, I need to verify a checkbox, where certain options from a dropdown only become active when a user checks the checkbox. I was sort of able to do this, but only after the user clicks the submit button were the changes applied. I need the changes to apply as soon as they click the checkbox.
I tried this for the textbox:
$a = "June";
$b = "July";
if (empty($_POST[""])) {
$textErr = "Text is required.";
} else if (!(preg_match("/{$a}/i", $text)) && (!preg_match("/{$b}/i", $text))){
$textErr = "Text must contain either 'June' or 'July'.";
}
else {
$text= test_input($_POST["text"]);
}
and also this:
$checkjune = stripos($_POST["text"], $a);
$checkjuly = stripos($_POST["text"], $b);
(empty($_POST["text"])) {
$textErr = "Text is required.";
} else if ($checkfl === false) {
$textErr = "Text must contain either 'June' or 'July'.";
} else {
$text = test_input($_POST["text"]);
}
This is kinda what I have currently for the checkbox:
(in HTML code)
<div class="row">
<input id="student" name="student" type="checkbox">I am a student</input>
</div>
<div class="row">
<label for="subject">Favorite Subject: </label>
<select id="subject" name="subject" >
<option value="0" enabled>Not Applicable (I am not a student) </option>
<option value="1" disabled>Math</option>
<option value="2" disabled>Science</option>
<option value="3" disabled>English</option>
<?php
if(!empty($_POST['student'])) {
echo "<script>document.getElementById('subject').disabled = ''; </script>";
}
?>
</select>
</div>