skip to Main Content

I have a problem. It is my full honor if anyone helps.

First, let me explain the workflow I want. My CMS is WordPress. I have a webpage (views.php). In this page, I want to show a download button (id=” download-button”) just to users who has the role subscriber. In default, no one has the role subscriber. So, the button is hidden in default. When a user buys a specific product he gains the role subscriber. Now, suppose a user has opened views.php page as a tab in his browser. In this step, the button is hidden. After that, he opens another tab and buys that specific product and he gains the role subscriber. Now, if he refresh the view.php page, the download button is seen. But, I want the user to see the download button without refreshing the page. In this regard, I wrote button.php file to be called in ajax. However, it does not work.

My codes:
html code (written in view.php which is the place of download button):

<div id="div1"></div>

my javascript code (which is put inside view.php file):

<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("button.php");
  });
});
</script>

my button.php code:

<?php
if (check_user_role(array('subscriber'))) {
echo ('<button id="download-button">Download</button>');                        
}               
?>

I should note that I have written check_user_role php function in views.php.
It would be my honor if you help.

Thanks in advance.
Milad

4

Answers


  1. Hey you have to use Window setInterval() Method, what this method does it will fire in background at your time interval set.

    You can call your ajax code to set/show your button

     setInterval(function(){ 
       $("#div1").load("button.php"); 
     }, 3000);
    

    Make sure once you do add this button put return false so it wont execute again and again not to increase load on webpage.

    Login or Signup to reply.
  2. Add an iframe to your view.php that doesn’t need to contain anyting nor be visible.

    <iframe name="download" id="if_download" src="blank.html"></iframe>
    

    Target the download-action to the iframe. With some JS:

    function download(href) {
      window.frames['download'].location = 'download.php?file=' + href;
      return false;
    }
    

    You may need to wrap the download-action through a php-file to modify its header

    download.php:

    $file_name = $_GET['file'];
    //validate file exists and *remove any ../ - simple:
    if (strpos($file_name, '/') !== false) die('yeah right..');
    header("Content-Disposition: attachment;filename="$file_name"");
    echo file_get_contents($file_name);
    die();
    
    Login or Signup to reply.
  3. $(document).ready event runs only once after the DOM is loaded. So this event will not fire unless page is reloaded.

    If the user is buying a subscription in another browser tab and then returns to the original tab, windows.onfocus event will fire.

    So you can use window.onfocus event to check for subscription every time view.php tab becomes active, and then show the button when necessary. So you can use something like the following, in your view.php

    $(document).ready(function(){
      window.onfocus = function () {
            $("#div1").load("button.php"); 
      }
    });
    
    Login or Signup to reply.
  4. As stated by smartdroid in one of the answers above, you can subscribe an event listener function to the window.onfocus event. Try following:

    document.addEventListener("DOMContentLoaded", function (event) {
        window.onfocus = function () {
            $("#div1").load("button.php"); 
        }
    });
    

    I highly recomment you to read further into javascript events.
    For plain javascript:
    https://www.w3schools.com/js/js_events.asp
    For jQuery:
    https://api.jquery.com/category/events/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search