I have a basic sign up form, and when checking if a field is not set (if(!$name)) it always goes to 'else', even when the field is empty. Does anyone know why? (When i'm trying to check it in reverse (if($name)), it does show the error line. )
Thanks a lot!
<?php
$error = '';
if(isset($_POST['submit'])){
$name = trim(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));
if( !$name && preg_match("/^([\w]){2,50}$/", $name)){
$error = ' * Please enter a valid name';
}
?>
<form action="" method="post">
<label for="name">Name:</label><br>
<input type="text" name="name"> <br><br>
<input type="submit" name="submit" value="Sign up">
<span class="error"><?= $error ?></span>
</form>
Aucun commentaire:
Enregistrer un commentaire