skip to Main Content

I want to show the information I flush in php before the response is complete.
I searched and wrote the following code
I get respone after progress compleated but i want to show result when php script process and user look What is being done .
How can I do this?

Thanks

My Code :

<!doctype html>
<html lang="">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form>
    <input type="text" id="ip" value="127.0.0.1">
    <button type="button" onclick="save()">send</button>
</form>

<iframe id="loadarea"></iframe>


<script src="jquery-3.5.1.min.js"></script>
<script type="text/javascript">
    function save() {
        let ip = $('#ip').val();
        let req = $.ajax({
            type: 'POST',
            url: 'xample.php',
            data: {
                ip: ip
            }
        });

       **************************************************************
       *                                                            *
       *  req.progress(function (res) {                             *
       *      document.getElementById('loadarea').src = 'nmap.php'; *
       *  });                                                       *
       *                                                            *
       **************************************************************
        req.done(function (res) {
            if (res == "cant") {
                 console.log('fail');
            }else
                 console.log('ok' );
        });
    }
</script>
</body>
</html>

2

Answers


  1. Chosen as BEST ANSWER

    I solved this problem with this way :

    <script src="jquery.min.js"></script>
    <script type="text/javascript">
        function save() {
            var ip = $('#ip').val();
            var data = new FormData();
            data.append('ip', ip);
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'example.php', true);
    
            xhr.send(data);
            xhr.onreadystatechange = function () {
                if (xhr.status === 200) {
                    if (xhr.readyState === XMLHttpRequest.LOADING) {
                        $('#loadarea').html(xhr.response);
                    }
                    if (xhr.readyState === XMLHttpRequest.DONE) {
                        $('#loadarea').append("finished!!!");
                    }
                }
            }
        }
    </script>
    

  2. you for show progress can set an interval that check status of your process.like this:

      <script type="text/javascript">
        function save() {
          let ip = $('#ip').val();
          let req = $.ajax({
            type: 'POST',
            url: 'xample.php',
            data: {
              ip: ip
            }
          });
    
          req.done(function (res) {
    
    
    //================================
            //  set interval for check status of progress every 1 sec
            setInterval(() => {
              let reqStatus = $.ajax({
                type: 'POST',
                url: 'xampleStatus.php',
                data: {
                  ip: ip
                }
              });
              req.done(function (resStatus) {
                console.log(resStatus);
              });
    
            }, 1000);
    
    
    //================================
    
            if (res == "cant") {
              console.log('fail');
            } else
              console.log('Start progress');
          });
    
    
    
        }
      </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search