How to continuously update a part of the page

后端 未结 3 2048
礼貌的吻别
礼貌的吻别 2020-12-30 11:25

http://pastebin.com/dttyN3L6

The file that processes the form is called upload.php

I have never really used jquery/js so I am unsure how I would do this or w

3条回答
  •  青春惊慌失措
    2020-12-30 12:21

    You can submit a form without refreshing a page something like this:

    form.php:

    Result comes here..

    profile.php:

     'This is my result' );
          echo json_encode( $arr );
    ?>
    

    jQuery:

    jQuery(document).ready(function(){
    
        jQuery('.ajaxform').submit( function() {
    
            $.ajax({
                url     : $(this).attr('action'),
                type    : $(this).attr('method'),
                dataType: 'json',
                data    : $(this).serialize(),
                success : function( data ) {
                            // loop to set the result(value)
                            // in required div(key)
                            for(var id in data) {
                                jQuery('#' + id).html( data[id] );
                            }
                          }
            });
    
            return false;
        });
    
    });
    

    And If you want to call an ajax request without refreshing page after a particular time, you can try something like this:

    var timer, delay = 300000;
    
    timer = setInterval(function(){
        $.ajax({
          type    : 'POST',
          url     : 'profile.php',
          dataType: 'json',
          data    : $('.ajaxform').serialize(),
          success : function(data){
                      for(var id in data) {
                        jQuery('#' + id).html( data[id] );
                      }
                    }
        });
    }, delay);
    

    And you can stop the timer at any time like this:

    clearInterval( timer );
    

    Hope this will give you a direction to complete your task.

提交回复
热议问题