1
votes

This is the continuation of my previous issue. I made the code simple and suspecting issue is with second form submission.

This is the code structure:

  1. A form (form1) with two input field for name and place
  2. After form submission,(and ONLY if in two fields, user entered data) creating an another form (form2) with a text area to display the php echo from previous form and another two submit buttons
  3. In actual I am using the second form's submit buttons for text save and email (here I just replaced it with a simple textarea echo to make it simple)

CODE:

<html>
<body>

<div class="emcsaninfo-symcli-main">
    <form  id="form1" name="form1" action=" " method="post" > 



        <div class="input">Your Name</div>
        <div class="response"><span><input  class="textbox" id="myname" name="myname" type="text" value="" /></span> </div>


        <div class="input">Your Place</div>
        <div class="response"><span><input  class="textbox" id="myplace" name="myplace" type="text" value="" /></span> </div>


<div class="submit">
        <input  id="first_submit" type="submit"  name="first_submit" value="first_submit" />
</div>

</form>


<?php

if(!empty($_POST['myname']) && !empty($_POST['myplace']) )
{

$myname = $_POST['myname'];
$myplace = $_POST['myplace'];

?>


<form  id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly>


<?php

echo "My name is $myname and I am from $myplace";

?>

</textarea>

<input  id="submit1" type="submit"  name="name_field" value="submit1" />
<input  id="submit2" type="submit"  name="place_field" value="submit2" />
</form> 

<?php

function name()
{
    echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
   name();
} 



function place()
{
    echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
   place();
} 

}

?>


</div>      

</html>
</body>

Issue: The first form form1 submit works fine.It will create the output textarea with two other submit button submit1 and submit2. But when I submitting the second form form2 using these two buttons, the form is not submitting properly, it just refreshing the html with initial code.

My requirement is when I press the second form submit button, it has to echo the output from textarea again, after keeping the first form textarea in its position.

PHP FIDDLE:

I have setup a php fiddle to understand the issue PHP FIDDLE MAIN PHP FIDDLE execution results -

2

2 Answers

1
votes

Your if condition contain only first form. Replace your if condition with

if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea']))

Keep form1 data with hidden input like this:

<form id="form2" name="form2" action="" method="post">

   .....
   <input type="hidden" name="myname" value="$myname" />
   <input type="hidden" name="myplace" value="$myplace" />
</form>
1
votes

You need to test which form was submitted. It's probably best to do this by checking the submit fields rather than the input fields.

if(isset($_POST['first_submit'])) )
{
// Do stuff with first form
}

if (isset($_POST['submit1'], $_POST['submit2']))
{
// Do stuff with second form
}

To keep the form values filled in, you can fill them using the post values. In the first form, change the inputs to:

<input  class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname']) echo $_POST['myname']; ?>" />

In the second form, add hidden inputs that copy the values:

<input  id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname']) echo $_POST['myname']; ?>" />

Notice that I changed the ID, adding _hidden, because you can't have two elements with the same ID. I removed the class, since CSS is irrelevant for hidden fields.