Jump to content

Need some help to specify path to website root.


Flashmaster82
 Share

Recommended Posts

Mh? Doesn’t fetch("/auction-longpolling.php" work? Starting with a slash is always relative to the “origin”.

You could also put the full address:

fetch("<?=config()->urls->httpRoot?>auction-longpolling.php" etc.

(note httpRoot already comes with a trailing slash)

  • Like 1
Link to comment
Share on other sites

Thanks for the reply @Jan Romero But it didn´t work? hmm.. i wonder why it works on localhost and not on live server.. just to be clear the file is outside the templates folder, root of the website folder. I have tried to put the file inside the template folder in my assets/auction-page/ folder but then it won´t work. So therefor i put it in the root folder.

<script>
// (B) LAST UPDATED TIMESTAMP
var last = 0;
 
// (C) AJAX LONG POLL
function poll () {
  // (C1) FORM DATA
  let data = new FormData();
  data.append("last", last);
  console.log("Fetch run", last);
 
  // (C2) FETCH UPDATE ON SERVER RESPONSE
  fetch("<?=config()->urls->httpRoot?>auction-longpolling.php", { method:"POST", body:data })
  .then(res => res.json())
  .then(data => {
    // (C2-1) UPDATE HTML DISPLAY
    
    document.getElementById("bid").innerHTML = data.bid;
 
    // (C2-2) NEXT ROUND
    last = data.unix;
    poll();
  }) 
 
  // (C3) CATCH ERROR - LOOP ON TIMEOUT
  .catch(err => poll());
}
 
// (D) GO!
window.onload = poll;
</script>

 

Link to comment
Share on other sites

Quote

just to be clear the file is outside the templates folder, root of the website folder.

The root of the site is one more level up from that location though.....

Usually when I need to pass some php var to js I do this:

    $jsconfig = array(
        'subscribe' => $urls->root . "myPhpFile.php" // the key "subscribe" can be whatever you want of course.
		// it points to the root of the site, close to "site" folder, one level up the "template" folder.
    );
    echo "<script>var pwUrls = " . json_encode($jsconfig) . ";</script>";
    ?>

Then in js I reference that var like this:

  pwUrls.subscribe
  • Like 1
Link to comment
Share on other sites

This is roughly what your directory tree looks like:

image.png.163e2615850f23259f6118d69af75d18.png

Your file auction-longpolling.php should be in the same directory as index.php and favicon.ico. Then it should be accessible as example.com/auction-longpolling.php.

You can test if the file is accessible by just typing the URL into your browser.

This isn’t a very processwirey way of doing things per se. You might want to make a special Template and Page for this polling or use a Path Hook or a UrlSegment.

  • Sad 1
Link to comment
Share on other sites

Thanks for the reply @Jan Romero

 

fileexplorer.jpg.41fc48587f8c8d991b5225099f0d791a.jpg

templates/assets/auction-page/javascripts.php

<script>    
    
// (B) LAST UPDATED TIMESTAMP
var last = 0;
 
// (C) AJAX LONG POLL
function poll () {
  // (C1) FORM DATA
  let data = new FormData();
  data.append("last", last);
  console.log("Fetch run", last);
    
  // (C2) FETCH UPDATE ON SERVER RESPONSE
  fetch("<?=config()->urls->httpRoot?>auction-longpolling.php", { method:"POST", body:data })
  .then(res => res.json())
  .then(data => {
    // (C2-1) UPDATE HTML DISPLAY
    
    document.getElementById("bid").innerHTML = data.bid;
 
    // (C2-2) NEXT ROUND
    last = data.unix;
    poll();
  }) 
 
  // (C3) CATCH ERROR - LOOP ON TIMEOUT
  .catch(err => poll());
}
 
// (D) GO!
window.onload = poll;
</script>

 

auction-longpolling.php

<?php

class auctionbids {
  // (A) CONSTRUCTOR - CONNECT TO DATABASE
  protected $pdo = null;
  protected $stmt = null;
  function __construct () { try {
    $this->pdo = new PDO(
      "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
      DB_USER, DB_PASSWORD, [
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);
  } catch (Exception $ex) { exit($ex->getMessage()); }}

  // (B) DESTRUCTOR - CLOSE CONNECTION
  function __destruct () {
    if ($this->stmt !== null) { $this->stmt = null; }
    if ($this->pdo !== null) { $this->pdo = null; }
  }

  // (C) GET LATEST 
  function getauctionbids () {
    $this->stmt = $this->pdo->prepare(
      "SELECT *, UNIX_TIMESTAMP(`time`) AS `unix`
       FROM `auctionbidx` ORDER BY `time` DESC LIMIT 1"
    );
    $this->stmt->execute();
    return $this->stmt->fetch();
  }
}

// (D) DATABASE SETTINGS - CHANGE THESE TO YOUR OWN!
define("DB_HOST", "xxx");
define("DB_NAME", "xxx");
define("DB_USER", "xxx");
define("DB_PASSWORD", "xxx");
define("DB_CHARSET", "utf8");

// (E) CHECK FOR auctionbids UPDATES
if (isset($_POST["last"])) {
  // (E1) SET TIME LIMIT
  set_time_limit(30); // set an appropriate time limit
  ignore_user_abort(false); // stop when long polling breaks

  // (E2) LOOP UNTIL THERE ARE UPDATES OR TIMEOUT
  $_auctionbids = new auctionbids();
  while (true) {
    $auctionbids = $_auctionbids->getauctionbids();
    if (isset($auctionbids["unix"]) && $auctionbids["unix"] > $_POST["last"]) {
      echo json_encode($auctionbids);
      break;
    }
    sleep(1); // short pause to not break server
  }
}
?>

You can try this out and see if you can make the path work...

Link to comment
Share on other sites

As it turns out, I don’t get a notification when you change your reaction to one of my posts, only when you add a new reaction, but I noticed anyway ?

You have to get an understanding of how to debug what’s happening. Are you familiar with your browser’s web development tools? There will be a place that shows all requests it sends to your site and their results. You can use this to figure out what is going wrong.

On 12/21/2022 at 1:53 PM, Flashmaster82 said:
  fetch("<?=config()->urls->httpRoot?>auction-longpolling.php", { method:"POST", body:data })

This line will initiate a POST request to auction-longpolling.php. Check your browser console to see the response status and its contents. It might be 404 or 403 or something, in which case you’re probably hitting the wrong URL or the server doesn’t allow the PHP file to be accessed that way. If it’s in the 500s there’s probably an issue with the PHP code.

  • Confused 1
Link to comment
Share on other sites

thanks for the reply once again @Jan Romero

if i do

../../auction_longpolling.php

i get this fetch run loop running in the console

fetch.jpg

if i do

<?php echo config()->urls->httpRoot;?>auction_longpolling.php

i get Error

Call to undefined function config()
86:      fetch("<?php echo config()->urls->httpRoot;?>auction_longpolling.php", { method:"POST", body:data })

 

Link to comment
Share on other sites

You can set breakpoints in your javascript using Firefox’s Debugger tab. You can also look at the response bodies in both places you screenshotted, to see if they are what you expect. Likely something in your javascript is going wrong and this happens:

On 12/21/2022 at 1:53 PM, Flashmaster82 said:
.catch(err => poll());

 

  • Like 1
Link to comment
Share on other sites

I am a total noob at javascript so it´s pretty difficult for me this ? How would you @Jan Romero do if i wanted to poll another field or multiple inside a template? Lets say that i wanted to do poll field: auction_bid that is on my auction_bid template, that is a child of auction_page. Right now its polling from a test table auctionbidx that is in the root of the database.

 

tree.jpg

auctionbid.jpg

BiddingDashboard2.jpg

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...