acr
acr

Reputation: 1746

Keep form data inside the field after submission using php

I am using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.

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="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>


        <div class="input">Your Place</div>
        <div class="response"><span><input  class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>

        <div class="input-quest">Graduation Status</div>
        <div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></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']) || !empty($_POST['output_textarea'] ) )
{

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

?>


<form  id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">


<?php

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

?>

</textarea>

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

<input  id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input  id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input  id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</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>

For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea

My name is John and I am from UK, and I am Graduate

I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2

My name is John and I am from UK, and I am

Here,

  1. How can I keep the the dropdown selection also with the output text area
  2. Will I able to show only the output_textarea content after second form submission without keeping the first form data ?

PHP FIDDLE

Upvotes: 4

Views: 21278

Answers (1)

You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:

<input  id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input  id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input  id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />

instead of that, this code should work:

<input  id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input  id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input  id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />

Upvotes: 6

Related Questions