I have a textbox that is in a for
loop. I want to validate this textbox if no data is entered then it should display an error message.
<form method="post" action="test.php">
<?php for($i = 0; $i < 2; $i++) { ?>
<input type='text' name='txtName[]' required />
<?php } ?>
<input type="submit" name="btnConfirm" value="Proceed" class="button" />
</form>
<?php
$name = $_POST['txtName'];
if(isset($_POST['btnConfirm'])) {
if(!empty($name)) { echo "it worked!"; }
else { echo "Please enter your name."; }
}
?>
It works when I use this code:
$name = $_POST['txtName[]'];
but after I enter data in textbox
, it doesn't go to my else
statement.