Flashmaster82 Posted December 20, 2022 Share Posted December 20, 2022 I need some help to specify path to website root. templates/assets/auction-page/javascripts.php fetch("../../auction-longpolling.php", { method:"POST", body:data }) the file is located in website/auction-longpolling.php It´s working in localhost but not on live server? Link to comment Share on other sites More sharing options...
Jan Romero Posted December 21, 2022 Share Posted December 21, 2022 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) 1 Link to comment Share on other sites More sharing options...
Flashmaster82 Posted December 21, 2022 Author Share Posted December 21, 2022 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 More sharing options...
3fingers Posted December 21, 2022 Share Posted December 21, 2022 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 1 Link to comment Share on other sites More sharing options...
Flashmaster82 Posted December 21, 2022 Author Share Posted December 21, 2022 Thanks for the reply.. I´m not good at js so i don´t know how to implement that code ? Link to comment Share on other sites More sharing options...
Jan Romero Posted December 21, 2022 Share Posted December 21, 2022 This is roughly what your directory tree looks like: 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. 1 Link to comment Share on other sites More sharing options...
Flashmaster82 Posted December 21, 2022 Author Share Posted December 21, 2022 Thanks for the reply @Jan Romero 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 More sharing options...
Jan Romero Posted December 22, 2022 Share Posted December 22, 2022 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. 1 Link to comment Share on other sites More sharing options...
Flashmaster82 Posted December 22, 2022 Author Share Posted December 22, 2022 thanks for the reply once again @Jan Romero if i do ../../auction_longpolling.php i get this fetch run loop running in the console 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 More sharing options...
zoeck Posted December 22, 2022 Share Posted December 22, 2022 Config() is your Problem ? (it’s only available when the functions api is activated) <?php echo $config->urls->httpRoot; ?>auction_longpolling.php 2 Link to comment Share on other sites More sharing options...
Jan Romero Posted December 23, 2022 Share Posted December 23, 2022 Whoops, my bad! I always default to the functions api because of the benefits laid our here https://processwire.com/docs/start/api-access/ 1 Link to comment Share on other sites More sharing options...
Flashmaster82 Posted December 23, 2022 Author Share Posted December 23, 2022 thanks all for the replies. How to solve this in the code? Sorry for being such a beginner.. ? Link to comment Share on other sites More sharing options...
Jan Romero Posted December 23, 2022 Share Posted December 23, 2022 Zoeck just told you: 17 hours ago, zoeck said: <?php echo $config->urls->httpRoot; ?>auction_longpolling.php See if this makes a difference. Post the console output again if you have problems, but make sure to activate requests: Link to comment Share on other sites More sharing options...
Flashmaster82 Posted December 23, 2022 Author Share Posted December 23, 2022 @Jan Romero My bad ? I think it finding the file now, but endless loop still och fetch etc... Console Network Link to comment Share on other sites More sharing options...
Jan Romero Posted December 23, 2022 Share Posted December 23, 2022 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()); 1 Link to comment Share on other sites More sharing options...
Flashmaster82 Posted December 23, 2022 Author Share Posted December 23, 2022 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now