Honesty Rocks! truth rules.

Making A Value In A Textbox Stay

HOME      >>       Programming


Hey!I know it is possible, but I don't know how to do it.Anyone who know how I can make the following:When a user type a message, and press Send, he will come to a page that will check the message. If an Error occur, the user must press a button, that will take him back to the message, and fix the error. but The problem is, that when the user press the button, his message is gone, and he must write it again. bad luck, if the message was long. Hope You undertsand what I want :)Thanks //Feelay


In Javascript, there's history.back and history.go(N), eg -1.


While History.back should do it, on some ocasions the values of Textarea controls get reset. You can use cookies to save those data. When the user is re-directed back to the original page, load the contents from the cookies.Say you have a form (form.php) with two fields Name & Message & the form submits the data to your submit.php file. This file should validate the data and on error store the data as cookies to the browser along with a status flag that denotes whether the action was completed successfully or not. Now, when the user clicks on the Back button, user is re-directed to the form.php file which checks the value of the status flag and loads the contents from the cookies accordingly.Cookies could be avoided by using a different mechanism. The submit.php file should output the page with Back button as form. This form would contain the previously typed messages as hidden fields. On submit via HTTP POST (when user clicks on BACK button), these contents would be supplied to form.php which would load the values into the fields.


Google "ullman redux sticky forms" for a php script and tutorial that retains the data in a form.


You can do it without using cookies, just using the superglobal array $_POST, try this and let me know if it is what you need.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://forums.xisto.com/no_longer_exists/ Values Stay</title><script type="text/javascript">function verifica(){ if (document.a.text1.value=="") { alert("Error... Insert a text"); document.a.text1.focus(); return false; } return true;}</script></head><body ><?php$text1=(isset($_POST["text1"])) ? (string) trim($_POST["text1"]) : "";$textarea1=(isset($_POST["textarea1"])) ? (string) trim($_POST["textarea1"]) : "";if(isset($_POST["accept"])) { $text1=(string) trim($_POST["text1"]); $textarea1=(string) trim($_POST["textarea1"]); if(empty($textarea1)) { die("The textarea field was blank! <br /><br /><a href=\"java script:history.back();\">Press here to try again</a>"); } else { echo "submit successful"; exit(); }}else {?><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td>TEST FORM</td></tr></table><form name="a" method="post" onsubmit="return verifica();"><table width="100%" border="0" cellpadding="2" cellspacing="4"> <tr> <td width="20%">TEXT VALUE</td> <td width="80%"><input type="text" name="text1" value="<?php echo $text1;?>" size="40" maxlength="80" onfocus="this.select()" /></td> </tr> <tr> <td width="20%">TEXT AREA VALUE</td> <td width="80%"><textarea name="textarea1" cols="50" rows="5"><?php echo $textarea1;?></textarea></td> </tr> <tr> <td width="100%" align="center" style="padding:10px 0px" colspan="2"> <input type="submit" name="accept" value="Save" > <input type="reset" name="cancel" value="Cancel" > </td> </tr></table></form></body></html><?php }?>
Only for testing purposes i don't validate the submited data, so, remember to validate it before you insert to a database for example, one way to do this is by using the mysql_real_escape() php funciton.

View it in action at Making values stay.

Tested with Internet Explorer 6, Firefox and Opera 9.26.

Best regards,


heh.. Long time ago I made this, but I figured it out yesterday XD

The easiset thing that you can do, is..

<form><?php echo "<input type=\"textbox\" name=\"text\" value=\"{$_POST['text']}>"; ?><input type="submit" name="submit"></form>

if you make the script check on the same page, and something goes wrong, the input of the text area/ box will be saved in a vvariable, and the variable will be the value of the box :mellow:


You have to be careful, though. In case Magic Quotes is turned off (which is should be, a completely useless feature), all the data inside $_POST and $_GET arrays will not have escape quotes, apostrophes etc. If someone enters a value with quotes, you would get something like this:


<input type="textbox" name="text" value=""value"with quotes">


Needless to say, this is valid HTML :mellow: That is why it is necessary to use addslashes() - oh, and also, the input tag need to be closed:


<input type="textbox" name="text" value=""value\"with escaped quotes" />


Thanks for the warning =DAnd I didn't know input tags had to be closed ._. I thought it was enuogh just to close the form tag. Anyways, Thanks =D//Feelay


Yes, input tags has to be closed as all the other empty xhtml tags like IMG or HR tags for example, especially if you want to validate your code.


Now, the code showed by Feelay and pyost are INCORRECT, because as far as i know, the HTML and the XHTML specifications do not include an input tag of type TEXTBOX, so, the correct codes to use are:

FOR HTML 2, 3.2, 4, 4.01:


<input type="text" name="text" value="">

FOR XHTML 1.0, 1.1, Basic:


<input type="text" name="textbox" id="textbox" value="" />



<input type="text" name="textbox" id="textbox" value=""></input>

BTW, you should look into other ways for escaping strings if you will work with databases, because addslashes does NOT make your input safe for use in a database query! It only escapes according to what PHP defines, not what your database driver defines.


For example, if you work with a MySql database it's recommended to use the mysql_real_escape_string() instead of using the addslashes() function. Therefore relying on addslashes is not a good idea at all and may make your code vulnerable to security risks like SQL Injection Attacks.


Best regards,


As far as I know, textbox is the type that right =S and I use it, and it works. And as far as I know, type="text" doesn't work =Sand when handeling database data, I do use mysql_real_escape_string ((=But when making.. for example a contact script, that doesn't have anything to do with a database, I don' think it works :mellow: therefor, I use addslashes()But thanks for the warnings =) people not aware of it, may use your help ^^//Feelay


the right input type for the text field for html forms is <input type="text" name="{name}" value="" />

here is a simple reference: http://www.w3schools.com/TAGS/tag_input.asp


Just continuing with pyost.

To retain the form fields is:

<input id="text" name="text" value="<?php if(isset($_POST['text'])) { echo htmlentities($_POST['text']); } ?>" />

This will convert any HTML elements like quotes and less-than/great-than signs to HTML representable code so it will still display properly on the page without messing it up, could possibly get away with htmlspecialchars but htmlentities covers a larger range of characters.

Isset is to check if the variable exists to avoid warnings, which you would see if you had error_reporting(E_ALL | E_STRICT);

If using htmlentities, you'll also need to reverse the effects too when using the data unless you can handle the conversions (which I don't recommend as it can make it harder and cause more problems).




Haha, sorry for reviving such an old thread.. I'm just browsing through my old threads and I just realised how much I've developed since this thread was created!Somehow shocking how much you can develop in two years, haha!