问题
I have problem, when I press submit button in my contact form all input values refreshing, it's good if all fields are filled correct, but if are some warning users must see previaus values, but all are cleared. My Javascript skills are low, maye are solution to solv this problem with php and html ?
回答1:
a simpler way is use html5 storage . store value on onkeyup event and destroy if submit successful
for an example
<input id="title" type="text" value="" name="article_name">
script
to check support of html5
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
script to store
if(supports_html5_storage()){
$("#title").keyup(function(){
var articel_title = $("#title").val();
localStorage.setItem("articel_title",articel_title);
localStorage.getItem("articel_title");
});
to clear storage ,you can fire this if form successfully submit it will clear storage
<script type=text/javascript>
localStorage.removeItem("articel_title");
</script>
you can also make ajax call on change or keyup event and store value to database so that if html5 is not supported by browser it will still work but it will cause more load on server
回答2:
return values to client:
<input type="text" name="val1" value="<?echo $_POST['val1']?>" />
回答3:
Post your values to an iframe within the page and if the entries are correct then redirect to another page.
So if the entries are wrong you would not have to do anything
回答4:
You will need ajax with jquery. The follwoing tutorials will help you:
- http://www.queness.com/post/160/create-a-ajax-based-form-submission-with-jquery
- http://www.tuttoaster.com/create-an-ajaxjqueryphp-contact-form/
来源:https://stackoverflow.com/questions/13669385/php-how-to-not-refresh-input-values-after-press-submit