How to continuously update a part of the page

后端 未结 3 2025
礼貌的吻别
礼貌的吻别 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:06

    This is pretty simple. To access elements using Jquery you use css selectors, for example, to get value of an input field with name "foo" you do the following:

    var fooVal = $("input[name=foo]").val();
    

    To send it over to the server you are to append an event listener (for example, click) to the submit button/any other element

    var data = { varName : fooVal };
    var url = "http://example.com";
    var responseDataType = "json";
    function parseResponse(JSON)
    {
       // your code handling server response here, it's called asynchronously, so you might want to add some indicator for the user, that your request is being processed
    }
    
    $("input[type=submit]").on('click', function(e){
      e.preventDefault();
        $(this).val("query processing");
        $.post(url,data, parseResponse, responseDataType);
     return false;
    });
    

    If you want to do constant updates, you can, of course, add timers or some other logic. But I hope you get the idea of how to proceed to such cases;

    0 讨论(0)
  • 2020-12-30 12:17

    To answer part of your question, you can use ajax.

    <html><head></head><body>
    <div id="feed"></div>
    <script type="text/javascript">
    var refreshtime=10;
    function tc()
    {
    asyncAjax("GET","upload.php",Math.random(),display,{});
    setTimeout(tc,refreshtime);
    }
    function display(xhr,cdat)
    {
     if(xhr.readyState==4 && xhr.status==200)
     {
       document.getElementById("feed").innerHTML=xhr.responseText;
     }
    }
    function asyncAjax(method,url,qs,callback,callbackData)
    {
        var xmlhttp=new XMLHttpRequest();
        //xmlhttp.cdat=callbackData;
        if(method=="GET")
        {
            url+="?"+qs;
        }
        var cb=callback;
        callback=function()
        {
            var xhr=xmlhttp;
            //xhr.cdat=callbackData;
            var cdat2=callbackData;
            cb(xhr,cdat2);
            return;
        }
        xmlhttp.open(method,url,true);
        xmlhttp.onreadystatechange=callback;
        if(method=="POST"){
                xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xmlhttp.send(qs);
        }
        else
        {
                xmlhttp.send(null);
        }
    }
    tc();
    </script>
    </body></html>
    
    0 讨论(0)
  • 2020-12-30 12:21

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

    form.php:

    <form action='profile.php' method='post' class='ajaxform'>
     <input type='text' name='txt' value='Test Text'>
     <input type='submit' value='submit'>
    </form>
    
    <div id='result'>Result comes here..</div>
    

    profile.php:

    <?php
          // All form data is in $_POST
    
          // Now perform actions on form data here and 
          // create an result array something like this
          $arr = array( 'result' => '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.

    0 讨论(0)
提交回复
热议问题