PHP how to not refresh input values after press submit

二次信任 提交于 2020-01-16 09:59:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!