Forum Moderators: coopster

Message Too Old, No Replies

retaining values in text boxes

         

JuicyScript

9:18 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



Please how do i retain values in text boxes and select box so users do not need to re-fill those fields when they hit the back button,after submiting the form.
this is my form
<form name="post" method="post" action="insert+new+personal.php ">
<fieldset>
<legend><strong class="legend">Personal Information </strong></legend>
<table width="670" align="center" class="FormInput">
<tr>
<td width="191"><p>&nbsp;</p>
<p>&nbsp;</p></td>
<td width="465">&nbsp;</td>
</tr>
<tr>
<td width="191"><span class="err">!</span>Mr/Mrs/Miss</td>
<td width="465"><select name="Mr" id="Mr" class="FormInput">
<option value="0">Please Select....</option>
<option value="MR">Mr</option>
<option value="MRS">Mrs</option>
<option value="MISS">Miss</option>
</select></td>
</tr>
<tr>
<td width="191"><span class="err">!</span>Sur Name</td>
<td width="465"><input name="surname" type="text" value="" onChange="javascript:this.value=this.value.toUpperCase();" size="20" id="surname" class="FormInput2" /> </td>
</tr>
<tr>
<td width="191"><span class="err">!</span>Other Names</td>
<td width="465"><input name="othernames" type="text" value="" onChange="javascript:this.value=this.value.toUpperCase();" size="40" id="othernames" class="FormInput2" /> </td>
</tr>
<tr>
<td width="191"><span class="err">!</span>Gender</td>
<td width="465"><input type="radio" name="gender" value="MALE" />
Male
<input type="radio" name="gender" value="FEMALE" checked="checked" />
Female </td>
</tr>
<tr>
<td width="191"><span class="err">!</span>Date of Birth</td>
<td width="465"><label>
<select name="drpdate" id="drpdate" class="FormInput2" >
<option>Select Date</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
</label>
<label>
<select name="drpmonth" id="drpmonth" class="FormInput2" >
<option>Select Month</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
</label>
<label>
<select name="drpYear" id="drpYear" class="FormInput2">
<option value=""> Select Year</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="1966">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
</select>
</label> </td>
</tr>
<tr>

jatar_k

9:51 pm on Mar 4, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> so users do not need to re-fill those fields when they hit the back button

you can't, what you can do is reload the form when you find an error and use the appropriate $_POST variable to set the form element, either echo'ing the value or using the value to set the appropriate option as selected

Matthew1980

10:10 pm on Mar 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there JuicyScript,

something like this would be right:-


<input type="text" name="name" value="<?php echo (isset($_POST['name']) ? strip_tags($_POST['name']) : '');?>">


the only input type thats different is the textarea:-


<textarea name="comments"><?php echo (isset($_POST['comments']) ? strip_tags($_POST['comments']) : '');?></textarea>


As its between the opening and closing tags..

Bearing in mind this is the simplified and I have placed the $_POST[] values directly in.

This will function after you submit & you have for arguments sake redirected the user back to the form to amend data that has already been input ie: it remembers the detail if set, if nothing is set it will display nothing.

Hope that's sort of what you were after... ;-p

Cheers,

MRb

Rune

6:12 pm on Mar 14, 2010 (gmt 0)

10+ Year Member



Sure you can. Once you submit the form, put the values into a $_SESSION and have the form pull them from there.

$formarr = Array("email"=>$email);
$_SESSION["formarr"] = $formarr;

<input type='text' name='email' value='<?=$_SESSION["formarr"]["email"];?>'>

For anything other than text/textarea/hidden it will be more complicated. You'll need to either generate the options from an array or use Javascript to make the selected one match the session value.

Matthew1980

6:51 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there,

Using sessions would in theory be a bad Idea, especially if you were having some 'sensitive' data being held in the session. Yes It would work, but you would have to make sure that the sessions would be killed afterwards.

As jatar_k mentions, this can only be done after the form has been processed so that you can grab the $_POST data and echo the values in the respective elements.

More than likely though, there are a few clever javascript or suchlike scripts out there, but for the purpose of just repopulating a textbox, submit the form, then catch the $_POST.

Please correct me if I'm wrong, that's just how I understand things to be ;-p

Cheers,
MRb

rocknbil

9:33 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Both would "work." They're just "workarounds."

If you submit a form, and have to use a back button on error, this is poor GUI design. **Normally** when you "go back" the form is still populated, but there are a variety of reasons why it won't be: no-cache, client security settings, form generated via Ajax/Javascript (shudder,) etc. Obviously the OP has a problem with "back button" in any case.

An extension of the solution posed by jatar_k, in a single script:

- if no input vars, display empty form. Submit back to this script. The form is **not** a static HTML page, it is generated by the script.

- if there are input vars, error check.

---- If errors, return to form with error message prominently displayed and fields populated. This requires a little more work for selects and radio buttons, but it needs to be done.

---- If input validates, perform the expected functions and return a response directly from this script. Use the (cleansed) input data to generate a customized response, a redirect to a "Duh-err, dank U" page is just . . . lazy.