Home > OS >  Using if(isset($_POST['submit'])) to display message but it is not working
Using if(isset($_POST['submit'])) to display message but it is not working

Time:10-22

I tried to write simple Form validation with PHP. When click submit button if the name is empty then the website will ask the user to fill in. But when i click the submit button nothing appear on the screen. Can you help me how to fix this problem? Thank you. Here is my code :

<?php
if (isset($_POST["submit"])){
    if(!empty($_POST["Ename"])){

    }else{
        echo"Please enter your name";
    }
}
?>
<html>
<form class="" action="index.php" method="post">
Tutor name: <input type="text" name="Ename" value="">
<input type="button" class="submit" value="Submit your record" name="submit">
</form>
</html>

CodePudding user response:

write tag in tag and set input type="submit"

<?php
if (isset($_POST["submit"])) {
    if (!empty($_POST["Ename"])) {

    } else {
        echo "Please enter your name";
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>

<body>
    <form action="" method="POST">
        Tutor name: <input type="text" name="Ename" value="">
        <input type="submit" class="submit" value="Submit your record" name="submit">
    </form>
</body>

</html>

CodePudding user response:

You need to wrap your form inputs and submit button in a <form> tag, like this:

<html>
    <form>
        Tutor name: <input type="text" name="Ename" value="">
        <input type="button" class="submit" value="Submit your record" name="submit">
    </form>
</html>
  • Related