hsammak Posted November 2, 2019 Share Posted November 2, 2019 Hi, I'm just trying to create a button which when you click it will run a php file that adds a page (without page refresh): This is the jquery/ajax: <script> $(document).ready(function(){ $("button").click(function(){ $.post("/site/templates/func_addpage.php/", function(data, status){ alert("Data: " + data + "\nStatus: " + status); }); }); }); </script> <button id="but_add">Add to wishlist</button> This is the php file I'm trying to run "func_addpage.php" <?php namespace ProcessWire; /* add to a page object with parent path and new pages template name */ $newPage = $pages->add('prod_ratings', '/wishlists', $randTitle, array( 'title' => $sanitizer->text($randTitle), 'api_test_hash' => $randHash ))->setOutputFormatting(true); ?> This code works if I just run it to the page on it's own, without a button click. The issue is when I try to run the php file by using a button click, I just receive an error. I've been reading up on this in the documentation and forum, and I can't seem to find a solution. I've tried making the php file a template, and then trying to call it. I've tried moving the php file into a folder in the root directory, instead of the templates folder. Any guidance would be appreciated. Thanks. Link to comment Share on other sites More sharing options...
hsammak Posted November 2, 2019 Author Share Posted November 2, 2019 I should also state, I'm only getting errors when trying to run processwire API commands on button click. The button click script works if I run a basic php script like: echo "hello world" Link to comment Share on other sites More sharing options...
hsammak Posted November 2, 2019 Author Share Posted November 2, 2019 (edited) From reading the forums, I think I might need to take make the php file a template? I have created the template in processwire backendv (func_addpage) and created a page of the same name "func_addpage". What is the best way to refer to the page url? Like this? $.post("<?= $pages->get(1243)->url ?>" <script> $(document).ready(function(){ $("button").click(function(){ $.post("<?= $pages->get(1243)->url ?>", function(data, status){ alert("Data: " + data + "\nStatus: " + status); }); }); }); </script> Doesn't seem to do anything.... Edited November 2, 2019 by hsammak Link to comment Share on other sites More sharing options...
dragan Posted November 2, 2019 Share Posted November 2, 2019 Just very quickly a few pointers, since it's getting late over here... Definitely create a PW-tpl and matching page for such tasks. Most people use one such special page for all kinds of API/AJAX stuff. Inside the page, you can check with a simple switch/case statement which action should be run. In that "AJAX endpoint" page (or whatever you want to call it), you can e.g. use if($config->ajax) and therefore (in the else clause) also re-use it for other purposes (classic GET or POST, i.e. data not sent via AJAX). (more examples on use-cases of $config->ajax) 3 hours ago, hsammak said: $newPage = $pages->add('prod_ratings', '/wishlists', $randTitle, array( 'title' => $sanitizer->text($randTitle), 'api_test_hash' => $randHash ))->setOutputFormatting(true); ?> I guess this looks OK (though personally I prefer the more verbose form), but in your case I would definitely add input validation (sanitize / maybe add a hidden input field etc.). Also, I don't know where $randTitle or $randHas comes from - I guess it's there in your original code but you omitted to post it here for brevity? It looks like it comes from this code snippet. Just make sure you have initalized those two variables. Link to comment Share on other sites More sharing options...
hsammak Posted November 3, 2019 Author Share Posted November 3, 2019 Using if($config->ajax) has got it working now! 13 hours ago, dragan said: I guess this looks OK (though personally I prefer the more verbose form), but in your case I would definitely add input validation (sanitize / maybe add a hidden input field etc.). I need to read about input validation, santize etc. Do you know of any good guides/posts to get me started? 13 hours ago, dragan said: Also, I don't know where $randTitle or $randHas comes from - I guess it's there in your original code but you omitted to post it here for brevity? It looks like it comes from this code snippet. Just make sure you have initalized those two variables. The code snippet with those variables I used was just an example I took to get the concept working, I will change all that up. Thank you for your help Link to comment Share on other sites More sharing options...
elabx Posted November 3, 2019 Share Posted November 3, 2019 6 hours ago, hsammak said: I need to read about input validation, santize etc. Do you know of any good guides/posts to get me started? https://processwire.com/api/ref/sanitizer/ I think this docs page documents it pretty well! 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