Hi,
Please look at this:

<?php
 
print '
<form>
 <td><input type="text" name="txt" maxlength="7" style="text-transform:uppercase"></td>
</form>
</html>';

if(isset($_POST['submit'])
{
  $val=$_POST['txt'];
#header();
}
 ?>

my text box values gets erased after submit button. If i use header function:
error is
Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\test.php:8) in C:\xampplite\htdocs\test.php on line 113

I want this format of my php coding. Please give me a solution to retain my text box values.

Member Avatar for diafol

You don't have a submit button from what I can see, so how are you submitting?
Also POST won't work as you don't stipulate a method attribute in the form tag. By default form method is GET. I don't understand the closing </html> tag. It doens't seem to make much sense to place it there. No DTD, No head, no BODY or is this script an include file?
The header() function although commented out, does not have any attributes so it won't do anything useful. The header() needs to be placed before any output (any html or echoed stuff from php).

All in all, you need to check some html and php tutorials on how to provide valid markup and create forms and how to process form data (it's also good practice to send form data to a different page via the action attribute).

Your code haven't got sense.
First your check must be at start of your code so your PHP to work depnding if the event is happened or not.
Other thing is that you must add method="post" to your form.
And after ($_POST) you have missed one ).
One more thing you must make a submit button if you want to use $_POST for check

Here is corrected code sample:

<?php

    if(isset($_POST['submit']))
    {
    $val=$_POST['txt'];
    #header();
	echo $val;
    }     
    print '
    <form method="post">
    <td><input type="text" name="txt" maxlength="7" style="text-transform:uppercase"></td>
    <td><input type="submit" name="submit" maxlength="7" style="text-transform:uppercase" value="submit"></td>
    </form>
    </html>';
     
    ?>

I have added one echo of the posted text to see that its working.

My previous code is the sample code.

<?php

print '<html>

<body bgcolor=#FF8C00>
<table align=center>
<tr><th>Form</th></tr>
</table>

<table align="center" border="1">
  <tr><th>Sno</th><th>Item</th></tr>
  <form method="post" name="f1">
  <tr>
  <td>1</td>
  <td><input type="text" name="txt" maxlength="7" style="text-transform:uppercase"></td>
  </tr>

<tr>
 <td colspan="3" align="center"><input type="submit" name="submit" value="submit"></td>
 </tr>
 </form>
 </table>
 
 </body>
 </html>';
 if(isset($_POST['submit']) && ($_POST['submit'])=="submit")
{
	$val=trim($_POST['txt']);
}
?>

I am not facing the error. I need text box values to be retained. I dont know.

Hi,

I need my isset function at the bottom of the page only. Because i am displaying the result on the same page at the bottom of the form.

Member Avatar for diafol
if(isset($_POST['submit']) && ($_POST['submit'])=="submit"){
   $val=trim($_POST['txt']);
}else{
   $val="";
}

that at top of file, then:

<td><input type="text" name="txt" maxlength="7" value="<?php echo $val;?>" style="text-transform:uppercase" /></td>
if(isset($_POST['submit']) && ($_POST['submit'])=="submit")
    {
    $val=trim($_POST['txt']);
	print $val;
    }

just add one print and you will get your content.
If you want to store it you will need some kind of database (mysql or other) or other type for storing your data.

hi,
thanks for your coding

<td><input type="text" name="txt" maxlength="7" value="<?php echo $val;?>" style="text-transform:uppercase" /></td>

when i try your code, again the problem,

in my text box, i am getting like this <?php echo $val;?>.

I am not getting it correctly.

try

<td><input type="text" name="txt" maxlength="7" value="'.$val.'" style="text-transform:uppercase" /></td>

he hasnt notice that you use var for your html code

No, its not working.

click on submit button "textbox value gets erased". Please check my code..what i posted.

<?php
     $val="";
    if(isset($_POST['submit']) && ($_POST['submit'])=="submit")
    {
    $val=trim($_POST['txt']);
    }
    print '<html>
     
    <body bgcolor=#FF8C00>
    <table align=center>
    <tr><th>Form</th></tr>
    </table>
     
    <table align="center" border="1">
    <tr><th>Sno</th><th>Item</th></tr>
    <form method="post" name="f1">
    <tr>
    <td>1</td>
    <td><input type="text" name="txt" maxlength="7" style="text-transform:uppercase" value="'.$val.'"></td>
    </tr>
     
    <tr>
    <td colspan="3" align="center"><input type="submit" name="submit" value="submit"></td>
    </tr>
    </form>
    </table>
     
    </body>
    </html>';
    ?>

Tested..

hi..i know it will work. but my isset() function i need at the bottom of my html tag. Because i am displaying my result set at the botton of the form text box.

if i placed isset() function at the top of the page means, my result set displayed at the top of my textbox. It won't looks good for me. So i am placing isset() function at the bottom of the page. thanks

<?php
     
    print '<html>
     
    <body bgcolor=#FF8C00>
    <table align=center>
    <tr><th>Form</th></tr>
    </table>
     
    <table align="center" border="1">
    <tr><th>Sno</th><th>Item</th></tr>
    <form method="post" name="f1">
    <tr>
    <td>1</td>
    <td><input type="text" name="txt" maxlength="7" style="text-transform:uppercase" value="'.trim($_POST['txt']).'"></td>
    </tr>
     
    <tr>
    <td colspan="3" align="center"><input type="submit" name="submit" value="submit"></td>
    </tr>
    </form>
    </table>
     
    </body>
    </html>';
    if(isset($_POST['submit']) && ($_POST['submit'])=="submit")
    {
    $val=trim($_POST['txt']);
    }
    ?>

hi, thanks a lot. Its working. Thanks for your help and your helping tendency.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.