1

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>
7
  • Immediate changes cannot be done with PHP. You'd have to utilize Javascript for that. And when you say that your textarea validation doesn't work, how exactly does it not work? Commented Dec 2, 2020 at 20:20
  • When I run it, the verification does not happen. Commented Dec 2, 2020 at 20:22
  • What does it mean "doesn't happen"? That's too vague. What do you expect to happen? To show the user a message? Is the message not showing? Commented Dec 2, 2020 at 20:23
  • Actually, I rechecked my code and there was a typo in that section. It appears to be working now. How would I implement that Javascript for the checkboxes? I'm not 100% sure. What I meant was the expected error message did not appear, but it's fine now. Commented Dec 2, 2020 at 20:24
  • Give this a shot. @PetrFořtFru-Fru I'm not sure AJAX is needed here. I understood the OP as simply having to disable/enable a dropdown based on a checkbox. Commented Dec 2, 2020 at 20:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.