0

Does anyone know how to make inputs in a textbox and selectbox remain after page refreshed?

I want to make my inputs remain afer page refreshed if the user unable to fill all the required filleds in a registration page.

user3352395
  • 205
  • 1
  • 3
  • 9

3 Answers3

1

you should validate your form before sending it to server, in client side with javascript

RayanFar
  • 539
  • 11
  • 28
0

set the value of the field to the posted value eg:

<form method="POST">
<input name="username" value="<?php echo $_POST['username']; ?>"/>
<input type="submit"/>
</form>
andrew
  • 9,313
  • 7
  • 30
  • 61
  • Don't forget to escape said value. In it's current form, one could inject javascript events onto that input (or simply break the input, causing loss of the previous data) – Kevin B Mar 04 '14 at 18:48
  • @KevinB interesting point. could you recommend a php built in for escaping? – andrew Mar 04 '14 at 18:57
  • I'm sure there's some simple method for it, i'm not a php coder so i would have to google for it. in ColdFusion it's called `HTMLEditFormat`. – Kevin B Mar 04 '14 at 18:59
  • It's contained within one of the answers here: http://stackoverflow.com/questions/4015345/how-to-properly-escape-quotes-inside-html-attributes – Kevin B Mar 04 '14 at 19:00
  • seems `filter_input()` would do the trick, thanks for the heads up – andrew Mar 04 '14 at 19:05
0

I assume you meant 'form submit' by 'page refresh'. Here how you do it.

For text fields:

<input type="text" name="text_field" value="<?php echo @$_POST['text_field']"; ?>/>

For select fields:

<select name="select_field">
    <option value="1" <?php echo @$_POST['select_field'] == "1" ? "selected" : '' ?>>an Option</option>
    <option value="2" <?php echo @$_POST['select_field'] == "2" ? "selected" : '' ?>>another Option</option>
</select>

NOTE: @ symbols is to suppress the error when you load the page for the first time.

Hope this helps :)

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54