Elaine Byene Elaine Byene - 3 months ago 8
PHP Question

Hide DIV when successfully submitted

I have this form which is working perfectly. However, I want to hide the form when it is submitted. Basically, I would like to hide the entire div starting with this:

<div class="HideSuccess">
when it is successfully submitted or when the form is submitted with/without error.

How do I go about this?

<?php
if (isset($_POST['submit'])) {
$Fname = $_POST['fname'];
$Email = $_POST['email'];
$fp = fopen("data.txt","a");
if (!$fp) {
echo 'Error!';
exit;
}
if (@fwrite($fp, $Fname.'|'.$Email."\n")) {
echo '<p class="thanks">Success<p>';
} else {
echo '<p>Error!</p>';
}
fclose($fp);
}
?>
<div class="HideSuccess">
<form name="input" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<input type="text" name="fname" id="fname" placeholder="Full Name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Full Name'" required />
<input type="email" name="email" id="email" placeholder="Your Email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Your Email'" required />
<input class="button" type="submit" name="submit" value="Submit" /></p>
</form>
</div>

Answer

If you are submitting the form on the same page, this might work for you. This will hide the div if there's a POST data on page load

<?php 
if (isset($_POST['submit'])):
    $Fname = $_POST['fname'];
    $Email = $_POST['email'];
    $fp = fopen("data.txt","a");
    if(!$fp) {
        echo 'Error!';
        exit;
    }
    if(@fwrite($fp, $Fname.'|'.$Email."\n")){
        echo '<p class="thanks">Success<p>';
    }else{
        echo '<p>Error!</p>';
    }
    fclose($fp);
else: ?>
<div class="HideSuccess">
    <form name="input" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
        <input type="text" name="fname" id="fname" placeholder="Full Name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Full Name'" required />
        <input type="email" name="email" id="email" placeholder="Your Email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Your Email'" required />
        <input class="button" type="submit" name="submit" value="Submit" /></p>
    </form>
</div>
<?php endif; ?>

EDIT: Adding full code as requested

Comments