Skip to content Skip to sidebar Skip to footer

Keep Data In Html Form After Form Submitted

i was trying to keep the input in html form field after submission occur. so, i've used like

Solution 1:

For textarea:

<textarea><?phpechoisset($_POST['myField']) ? $_POST['myField'] : 'column_name'?></textarea>

For select:

<select><optionvalue='xx'<?phpechoisset($_POST['myField'])&&$_POST['myField']=='xx'?'selected="selected"':''?>>XX</option><optionvalue='yy'<?phpechoisset($_POST['myField'])&&$_POST['myField']=='yy'?'selected="selected"':''?>>YY</option></select>

If you have are using jQuery, I prefer to populate the select using javascript:

<select name='myField'>
    <option value='xx'>XX</option>
    <option value='yy'>YY</option>
</select>
<?phpif(isset($_POST['myField'])):?>
<script type='text/javascript'>
    $("select[name=myField] option[value='<?php echo $_POST['myField']';?>']").attr("selected","selected");
</script>
<?phpendif; ?>

Solution 2:

<textarea>s don't have a value attribute. They have a body:

<textareaname="myField2"><?phpechoisset($_POST['myField2']) ? $_POST['myField2'] : 'column_name'?></textarea>

Likewise, <select>s do not have a value attribute. The value of a <select> is determined by the contained <option>s which have the selected attribute. This one is left as an exercise to the reader. Hint: you'll need to iterate over the <select>'s <option>s and conditionally add the selected attribute to one or more of the <option>s.

Post a Comment for "Keep Data In Html Form After Form Submitted"