表单提交后,将数据保存为html表单 [英] Keep data in html form after form submitted

查看:270
本文介绍了表单提交后,将数据保存为html表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在提交后保留html表单域中的输入。所以,我用过像

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

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

如果输入类型为 text 。它不适用于< textarea> & < select>

its working nicely if input type is text. its not working with <textarea> & <select>.

推荐答案



For textarea:

<textarea><?php echo isset($_POST['myField']) ? $_POST['myField'] : 'column_name' ?></textarea>

对于选择:

<select>
   <option value='xx' <?php echo isset($_POST['myField'])&&$_POST['myField']=='xx'?'selected="selected"':''?>>XX</option>
   <option value='yy' <?php echo isset($_POST['myField'])&&$_POST['myField']=='yy'?'selected="selected"':''?>>YY</option>
</select>

如果您使用的是jQuery,我更喜欢使用javascript填充选择内容:

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>
<?php if(isset($_POST['myField'])):?>
<script type='text/javascript'>
    $("select[name=myField] option[value='<?php echo $_POST['myField']';?>']").attr("selected","selected");
</script>
<?php endif; ?>

这篇关于表单提交后,将数据保存为html表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆