Jump to content


Photo

Sliding Banner with AJAX


  • Please log in to reply
3 replies to this topic

#1 negatron

negatron

    Newbie

  • Members
  • Pip
  • 3 posts
  • 0

Posted 08 February 2012 - 08:47 PM

New to all this, and would appreicate any insight!

I am using the 'Images Extra Fieldtype' to hold some images for a jquery sliding banner. I had the banner working, but have decided I am not keen on loading all the images at once.

Instead I'm currenlty retrieving the array and using shuffle() to select a random one to display on page load. I'd like to then fetch the others after the page has loaded, and am trying to use AJAX to send a response back to my PHP script, to no avail - I'm getting 'access denied' to the script I'm trying to post to (it resides in the templates directory).

Before I spend more time trying to figure this out, I'm interested to know if my intentions seem logical, and if this is a sound approach?!

Cheers.

#2 cesar

cesar

    Starter

  • Members
  • 1 posts
  • 0

Posted 08 February 2012 - 11:04 PM

To elaborate a bit on this, our main.php (site/templates/) contains the markup with an inline ajax call. The point of this is that if javascript is available on the client, we can send additional markup to the page. In this example, we are just popping an alert box.

site/templates/main.php:
<html>
...
<body>
...
<?php
if ($page->get("title") == "Home")
{  
  echo "<script type='text/javascript'>
	var stuff = 1;
	$.ajax({
	 type: 'POST',
	 url: '" . $config->urls->templates . "somefile.php',
	 data: {'inspection': stuff},
	 dataType: 'json',
	 success: function(returnedData, status, request)
	 {
	  alert('Yay! ' + returnedData);
	 },
	 error: function()
	 {
	  alert('crap...');
	 }
	});
	 </script>";
}
?>
</body>
</head>
Where url is '/site/templates/somefile.php'

site/templates/somefile.php:
<?php
if($config->ajax)
{
json_encode("Hello");
exit;
}
?>

However, this fails with 403 (Forbidden).
Placing the contents of somefile.php at the top of main.php and replacing the ajax url with '$page->url' works just fine.

Is there any reason why we would be unable to post to another php script?

Thanks!

#3 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • LocationAtlanta, GA

Posted 09 February 2012 - 05:48 PM

ProcessWire doesn't allow direct execution of PHP files in its /site/templates/ directory for security. So what you'd want to do is place your ajax PHP file somewhere else, like in the site root or under some other directory that you've created. Then include ProcessWire's /index.php to bootstrap it:

/somefile.php

<?php
include('./index.php'); // bootstrap ProcessWire
if(wire('config')->ajax) echo json_encode("Hello");

When you bootstrap ProcessWire, it'll work the same as it would from your template file, except that you have to access the API variables through the wire() function, i.e. wire('pages'), wire('config'), etc.

More here:
http://processwire.com/api/include/

#4 negatron

negatron

    Newbie

  • Members
  • Pip
  • 3 posts
  • 0

Posted 09 February 2012 - 08:24 PM

Thanks ryan, I have some learning to do :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users