Bianca Bianca -4 years ago 126
PHP Question

Show a div after submitting a form and keep the information of the form

I need to show a div after receiving info from a form. I got a good code to show the div after submitting the code, but for this code work, I need to use

return false
so the page doesn't reload.

Here is the code:



function showHide() {
document.getElementById("hidden").style.display = "block";
}

#hidden {
display: none;
}

<html>

<body>

<form action="" method="post" class="generator-form" onsubmit="showHide(); return false">
<label for="name">
<input type="text" name="nome" placeholder="Insira seu nome" required>
</label>
<input type="submit" value="go">

<div id="hidden">hello</div>

</body>

</html>





PS: I don't know why the js is not working in here, in codepen it's.

So, here's the thing:


  1. I can't use
    return: false
    because I do need to return these values; and

  2. I don't want to use
    onclick()
    because when the user clicks on the
    button it'll show a blank box.



To explain better, I'll get the infos I got in the form and show them in the previously hidden div. It's a signature generator for email.

There's someway I can do this with javascript or php?

Answer Source

Try this:

<?php
$show ="none";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$show = "block";
}

?>
<html>

<body>

<form action="" method="post" class="generator-form" onsubmit="showHide();  return false">
            <label for="name">
                <input type="text" name="nome" placeholder="Insira seu nome" required>
            </label>
  <input type="submit" value="go">

  <div style="display:<?php echo $show;?>">hello</div>

</body>

</html>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download