dourong8495 2010-07-26 20:08
浏览 32

我想知道当action =“any.php”提交表单时如何显示“加载”图像

I want show Loader img with jQuery when you post by PHP Simple action="search.php" Not ajax post or jquery post and Thanx for all

  • 写回答

1条回答 默认 最新

  • dounao1875 2010-09-07 03:39
    关注

    This problem is not solvable in a meaningful way without the use of AJAX. In other words, you need AJAX to show a "loading" image while the response from a form submission is handled by the server.

    If I understand you correctly, then you have a form:

    <form action="search.php" method="post">
        ...
    </form>
    

    And it is handled as a regular HTML form. The data is passed by the page to the server that uses PHP to handle it. Without AJAX, you have no good way of finding out when the response of the form, the search results, are ready.

    Generally, what is done is that the form is submitted to the server with the use of AJAX. The form is handled by the server (PHP does its stuff). During this time, a "searching" image is shown. Then when the server responds, the image is removed and the search results are shown.

    Using jQuery and AJAX on a page this could be done like this:

    jsFiddle example

      // The form has an id of "theForm".
      // This function defines what happens when it is submitted.
    $('#theForm').submit(function() {
    
          // Replace the form w the search icon.
        $("#theForm").html('Searching...<br/>' +
            '<img src="http://i.stack.imgur.com/ZK0sq.gif" />');
    
          // Make the search request to the PHP page.
          // The 3 arguments are: 
          //     1 - The url. 2 - the data sent 
          //     3 - The function called when data is sent back
        $.post("/ajax_html_echo/", $(this).serialize(), function(result){
    
              // Here we replace the search image with the data
            $("#page").html(response);
        });
    
          // Cancel the regular submission of the form.
          // You have to do this, or the page will change and things won't work.
        return false;
    });
    

    The nice thing about the above is that it degrades nicely if JS is off, since you still have the regular HTML form and its regular submit functionality to fall back on, and if JS is on, then it blocks this regular functionality with the return false and uses the AJAX.

    Jquery used:
    .submit()
    .html()
    .post()
    .serialize()

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?