PHP form not working WHY? IT must warn user if blanks are empty | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

PHP form not working WHY? IT must warn user if blanks are empty

<?php $loginerr = $websiteerr = $messageerr = $emailerr = $mavzuerr=""; $login = $website = $message = $email = $mavzu = ""; if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (empty($_POST['login'])){ $loginerr = "Loginni kiritish majburiy"; } else { $login = test_input($_POST['login']); } if(empty($_POST['email'])){ $emailerr = "Emailni kiritish majburiy"; } else { $email = test_input($_POST['email']); } } function test_input($data) { $data = trim($data); $data = stripcslashes($data); $data = htmlspecialchars($data); return $data; } ?> <html> <form> <input type="text" name="login" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post"> <span class="error"><?php echo $loginerr;?></span> <input type="email" name="email" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post"> <span class="error"><?php echo $emailerr; ?></span> <input type="submit" value="Submit"> </form> <?php echo $login; echo $email; ?> </html>

10th Jun 2017, 11:03 AM
Nodirbek
2 Answers
+ 3
Remove commented statement <?php $loginerr = $websiteerr = $messageerr = $emailerr = $mavzuerr=""; $login = $website = $message = $email = $mavzu = ""; /* if ($_SERVER['REQUEST_METHOD'] == 'POST') { remove this if clause */ if (empty($_POST['login'])){ $loginerr = "Loginni kiritish majburiy"; } else { $login = test_input($_POST['login']); } if(empty($_POST['email'])){ $emailerr = "Emailni kiritish majburiy"; } else { $email = test_input($_POST['email']); } /* } remove this also */ function test_input($data) { $data = trim($data); $data = stripcslashes($data); $data = htmlspecialchars($data); return $data; } ?> <html> <form> <input type="text" name="login" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" required > <span class="error"><?php echo $loginerr;?></span> <input type="email" name="email" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" required > <span class="error"><?php echo $emailerr; ?></span> <input type="submit" value="Submit"> </form> <?php echo $login; echo $email; ?> </html>
10th Jun 2017, 11:33 AM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
0
Just use the "Required Attribute of INPUT tag. For example: <input type="text" name="login" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" REQUIRED> After this, if the form is empty, the user will not be able to submit it.
10th Jun 2017, 11:25 AM
Mrigank Pawagi
Mrigank Pawagi - avatar