PHP_SELF calls the same file

<?php
if(isset($_POST['submit'])) 
{ 
    $name = $_POST['name'];
    echo "User Has submitted the form and entered this name : <b> $name </b>";

}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="text" name="name"><br>
   <input type="submit" name="submit" value="Submit Form"><br>
</form>

      

I only want if the part to run after submit and display should not be displayed after clicking the submit button of the form

+3


source to share


2 answers


Write your html form code inside the else part.



<?php
if(isset($_POST['submit'])) 
{ 
    $name = $_POST['name'];
    echo "User Has submitted the form and entered this name : <b> $name </b>";

} else {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="text" name="name"><br>
   <input type="submit" name="submit" value="Submit Form"><br>
</form>

<?php } ?>

      

+6


source


Use exit .



if(isset($_POST['submit']) {
    $name = $_POST['name'];

    echo "User has submitted the form and entered this name : <b> $name </b>";

    exit;
}

      

+1


source







All Articles