Jump to content

Run PHP on button click using jquery/ajax


hsammak
 Share

Recommended Posts

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

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 by hsammak
Link to comment
Share on other sites

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

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

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...