coder
coder

Reputation: 299

PHP: HTML Form disappears after submit

I've got a simple <form> in myFunction, as you can see here:

<?php
    function myFunction()
    {
        echo "
            <form method='post'>
                <fieldset>
                    <legend>Something</legend>
                    <input type='text'>
                    <input type='submit' value='send' name='postform'>          
                </fieldset>
            </form>
        ";
            
        if (isset($_POST['postform']))
        {
            echo "I'm working!";
        }
    }
?>

When I call this function, I can see the form but, when I submit it (by clicking the submit button), it disappears. How can I solve this problem?

Here is the full code:

<?php
    echo "
            <form method='post'>
            <button name='first'>First step</button>
            </form>
        </div>
        ";
    
    if (isset($_POST['first']))
    {
        myFunction();
    }
?>
   
<?php
    function myFunction()
    {
        echo "
        <form method='post'>
            <fieldset>
                <legend>Something</legend>
                <input type='text'>
                <input type='submit' value='send' name='postform'>
            </fieldset>
        </form>
    ";
    
    if (isset($_POST['postform']))
    {
        echo "I'm working!";
    }
}
?>

Upvotes: 0

Views: 8450

Answers (7)

Alexander Mikhalchenko
Alexander Mikhalchenko

Reputation: 4565

The issue is that $_POST superglobal is purged on new request, just like when you're navigating through pages, which is quite natural.

So, if one comes to a page with, let's say, $_POST = ['first' => ''], and then he submits a post form (or any form) ['postform' => 'send'], the resulting $_POST would be ['postform' => 'send'].

So, in your case the easiest solution would be either to follow Shailesh's answer or submit the first form with method='get' and, of course, then you'll have to change $_POST['first'] to $_GET['first'].

But a better solution would be to pass some 'step' parameter in request on each step, so you'll have <input type="hidden" name="step" value="1">. And then, depending on a step variable, do some stuff.

Also check out $_SESSION.

Cheers!

Upvotes: 3

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

Move your postform form processing out of the myFunction function definition and call myFunction function from there. Here's the complete code,

<html>
<head>
    <title>Page Title</title>
</head>
<body>
<?php
    echo "
        <form method='post'>
            <button name='first'>First step</button>
        </form>
    ";

    if (isset($_POST['first'])){
        myFunction();
    }

?>

<?php
    function myFunction(){
        echo "
            <form method='post'>
                <fieldset>
                    <legend>Something</legend>
                    <input type='text'>
                    <input type='submit' value='send' name='postform'>
                </fieldset>
            </form>
        ";
    }

    if (isset($_POST['postform'])){
        myFunction();
        echo "I'm working!";
    }
?>
</body>
</html> 

Upvotes: 1

Saahon
Saahon

Reputation: 419

Change this place on this code:

<?php
echo "
<form method='post'>
<button name='first'>First step</button>
</form>
</div>
";

if (isset($_POST['first']) or isset($_POST['postform']))
{
myFunction();
}

?>

Upvotes: 0

macl
macl

Reputation: 995

It disappears because you don't get to call the function myFunction() itself. The second form does not include the field "first".

If you want it to work "as is", include this in myFunction() code:

    function myFunction()
    {
    echo "
    <form method='post'>
    <fieldset>
    <legend>Something</legend>
    <input type='text'>
    <input type='hidden' value='send' name='first'>
    <input type='submit' value='send' name='postform'>
    </fieldset>
    </form>
   ";
   if (isset($_POST['postform']))
   {
    echo "I'm working!";
   }
   }
   ?>

The only change is

    <input type='hidden' value='send' name='first'>

which makes the form visible again. Anyway, you should rethink this whole code.

Upvotes: 2

Amir Hosseinzadeh
Amir Hosseinzadeh

Reputation: 8458

<?php
if(isset($_POST['submit'])) 
{ 
    $name = $_POST['name'];
    echo "User Has submitted the form and entered this name : <b> $name </b>";
    echo "<br>You can use the following form again to enter a new name."; 
}
?>
<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>

Upvotes: 0

Shailesh Katarmal
Shailesh Katarmal

Reputation: 2785

if (isset($_POST['first']))
{
   myFunction();
}

Replace with:

if (isset($_POST['first']) || isset($_POST['postform']) )
{
   myFunction();
}

Upvotes: 1

Rushi Ayyappa
Rushi Ayyappa

Reputation: 2848

write the same as two different documents.

<html>
 <body>
 <form method="post" action="submit.php">
 <fieldset>
   <legend>Something</legend>
   <input type="text" name="input">
   <input type='submit' value='send' name="postform">
   </fieldset>
   </form>

now write the following in submit.php

 <?php 
  $input=$_POST['input'];
    if ($input!="")
     {
    echo "I'm working!";
      }

 ?>

Upvotes: 0

Related Questions