Jump to content

Sliding Banner with AJAX


negatron
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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/

  • Like 2
Link to comment
Share on other sites

  • 4 years later...

I think a have a problem related to this.
Iam running PW 3.0 and loading content from a php file, that is located at my site root, into one of my template files (under site/templates).

So in my PHP File I have this (like Ryan suggested):

include('./index.php');  // bootstrap ProcessWire

So this works now:

<img src="<?php echo $config->urls->templates?>img/image.png">

But when I try to save pages in an array like this for example:

$wohnungen = wire("pages")->find("parent=/wohnen/, template=wohnungen, sort=sort");

I get this Error:

Error: Call to undefined function wire() (line 8 of /www/htdocs/w00dd152/projekte/fabricius/wohnungen.php) 

Any Ideas?

I figured it out. I had to use

$wohnungen = $wire->pages->find("parent=/wohnen/, template=wohnungen, sort=sort");

 

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