KarlvonKarton Posted August 10, 2016 Share Posted August 10, 2016 Hello, Can anybody tell me how to access a PHP file in: templates/lib/instapast-ajax.php From a javascript file in: templates/scripts/app.js code in app.js: request = $.ajax({ url: "../lib/instapast-ajax.php", // wrong code ? type: "post" }); Thank you. Link to comment Share on other sites More sharing options...
bernhard Posted August 10, 2016 Share Posted August 10, 2016 2 Link to comment Share on other sites More sharing options...
KarlvonKarton Posted August 10, 2016 Author Share Posted August 10, 2016 @bernhard Is this the answer to the url part? (I only want to know what link should be in the url of the JS.) Link to comment Share on other sites More sharing options...
BitPoet Posted August 10, 2016 Share Posted August 10, 2016 ProcessWire by default blocks access to files with the extensions php, htm, html, inc and tpl underneath the templates directory (or, to be precise, the shipped .htaccess file does so). This is a security feature. Thus, to access third party PHP code, you have two choices: Move the relevant code outside of the templates directory or create a template with a PHP file in site/templates that includes the external library (in your case instapast-ajax.php, and that's where Ryan's quote comes in) and a page that uses this template (and that provides you with the URL to call from JS) 7 Link to comment Share on other sites More sharing options...
KarlvonKarton Posted August 10, 2016 Author Share Posted August 10, 2016 Indeed, moving the directory /lib to /site made the PHP file accessible (and was the easiest thing to do for me now): request = $.ajax({ url: "site/lib/instapast-ajax.php", type: "post" }); Thank you, BitPoet and of course bernhard. Link to comment Share on other sites More sharing options...
KarlvonKarton Posted August 11, 2016 Author Share Posted August 11, 2016 (edited) I have tried the second choice of BitPoet too: Somewhere on a page I have two pieces of HTML: <!-- trigger --> <a title=“some title” class="instapast" data-insta=“some_page_id”>linkname</a> <!-- Zurb Foundation Reveal Popup --> <div class="reveal" id="instapastPopup" data-reveal> <h1 id="instapastPopupTitle"></h1> <p id="instapastPopupContent"></p> <button class="close-button" data-close aria-label="Close modal" type="button"> <span aria-hidden="true">×</span> </button> </div> The Javascript /templates/scripts/app.js jQuery(document).ready(function($){ var request; $('a.instapast').click(function(){ var id = this.dataset.insta; request = $.ajax({ url: "instapast-ajax/?id=" + id, // instapast-ajax.php is in the directory templates type: "post" }); request.done(function (data, textStatus, jqXHR){ var json = $.parseJSON(data); $('#instapastPopupTitle').html(json.title); $('#instapastPopupContent').html(json.text); $('#instapastPopup').foundation('open'); }); }); }); In Processwire I have made a Template: instapast-ajax And a page with the same name and the template avove: instapast-ajax That gives me the URL for app.js: /instapast-ajax/ The code in instapast-ajax.php: <?php namespace ProcessWire; //$ID = $_GET['id']; // not secure $ID = $sanitizer->int($input->get->id); // updated thanks to bernhard $instagramPickTitle_ = $pages->get($ID)->title; $instagramPickContent_ = $pages->get($ID)->text; $arr = array( 'title' => $instagramPickTitle_, 'text' => $instagramPickContent_ ); $json = json_encode($arr); echo $json; ps: I hope I did not forget something. Edited August 11, 2016 by KarlvonKarton Bernhard's tip on sanitizer 2 Link to comment Share on other sites More sharing options...
bernhard Posted August 11, 2016 Share Posted August 11, 2016 what is your question? does it work or does it not work? don't forget to sanitize your input variables! $ID = $_GET['id']; // better $ID = $sanitizer->int($input->get->id); maybe also check if the page exists: if(!$pages->get($ID)->id) // return or throw error see https://processwire.com/api/variables/sanitizer/ 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted August 11, 2016 Share Posted August 11, 2016 I don't think there was a question, but it's rather a mini-tutorial. 2 Link to comment Share on other sites More sharing options...
KarlvonKarton Posted August 11, 2016 Author Share Posted August 11, 2016 1 minute ago, LostKobrakai said: I don't think there was a question, but it's rather a mini-tutorial. Exactly. There is no question anymore. It works. Just wanted to share. 2 Link to comment Share on other sites More sharing options...
shogun Posted June 24, 2020 Share Posted June 24, 2020 On 8/10/2016 at 12:49 PM, BitPoet said: ProcessWire by default blocks access to files with the extensions php, htm, html, inc and tpl underneath the templates directory (or, to be precise, the shipped .htaccess file does so). This is a security feature. Thus, to access third party PHP code, you have two choices: Move the relevant code outside of the templates directory or create a template with a PHP file in site/templates that includes the external library (in your case instapast-ajax.php, and that's where Ryan's quote comes in) and a page that uses this template (and that provides you with the URL to call from JS) For the first solution Im stilling getting the forbidden error after moving the php file to the site directory. Here's my console after ajax submission to the contact.php file. I need to access processwire page variables from this file also. Link to comment Share on other sites More sharing options...
kongondo Posted June 25, 2020 Share Posted June 25, 2020 14 hours ago, shogun said: For the first solution Im stilling getting the forbidden error after moving the php file to the site directory. See my answer to your similar question here: Link to comment Share on other sites More sharing options...
shogun Posted June 25, 2020 Share Posted June 25, 2020 Yup. I added a trailing slash to the url and it worked. Not sure why 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