PHP inclusion (patched via github) doesn't read $_POST/$_GET

  • Problem
  • Updated 5 years ago
So I wanted to implement a contact form.

I patched my with the current one on github to gain php parsing and it works just fine, except one problem. I can't handle any data in $_POST or $_GET.

Example: I have a folder "content/contact" and a page.txt with some contact info; the according page.html template has something like

if @content do
foreach $PHP do

that includes a .php file ("content/contact/form.php") that checks for
, if not, it displays a form using
<form action"" />
. If $_POST is set it handles the form data and displays a thank-you-note.

At least in theory. I guess it has something to do with mod-rewrite and $_SERVER["PHP_SELF"] being /index.php ... I'm not sure though.

Any ideas? Would be very helpful... thanks :)
Photo of Horst Horstensen

Horst Horstensen

  • 12 Posts
  • 1 Reply Like
  • tired.

Posted 5 years ago

  • 2
Photo of Anthony Kolber

Anthony Kolber, Official Rep

  • 811 Posts
  • 157 Reply Likes
I imagine this is due to Stacey's caching mechanism. It only dumps the page cache when a file has been updated, so its probably serving outdated POST data.

Have tried it with the cache turned off? (chmod 755 '/app/_cache')
Photo of Horst Horstensen

Horst Horstensen

  • 12 Posts
  • 1 Reply Like
Sorry I didn't answer for so long, I was on vacation.

You are correct, this is a caching problem and it can be resolved with turning off the cache. But this is not a pretty solution, since the cache is part of what makes stacey so great.

One solution would be to prevent stacey from caching any page, that includes a PHP script; or you could mark pages that should not be cached with a file in it (".nocache") or something like that. Not really pretty either.

Then I thought "Why not use AJAX to circumvent the cache?", so here's my solution.

This example application performs a, presumably easy, task: You enter a name in a field, click 'submit' and the page displays a greeting. That could be done by jQuery alone but we want to parse in through a PHP script first.

I created a folder called "php" in the stacey root folder, so this is "/php/application.php":

echo "Hello, " . $_POST['field'] . "!";

For this to work I had to move the template's jQuery include to the <head> of the html, I don't know why I had to, but it wouldn't work otherwise. The template calls the :html partial so we can have some basic html and JS.

This is "/content/1.myPageName/application.html":

<label for="field">Name: </label>
<input type="text" id="field" name="field" value="" />
<input type="submit" id="submit" name="submit" /><br /><br />
<div id="response"></div>

<script type="text/javascript">
$(document).ready(function() {
$("#submit").click(function(e) {
$("#response").load("http://@base_url/php/application.php", {field:$("#field").val()});

As you can see, the jQuery JS overrides the default behaviour of the submit button, reads the value of the input field and passes this as a POST-parameter to the AJAX request now performed on the .php.

The .php now parses the value of "field" any way we want (in this case "Hello, <name>!") and echoes it; this is the AJAX response that is now loaded into the response-<div> ... This circumvents the caching and produces good results without a page reload.

You could use this technique to POST data to any kind of script you like, as well as display anything a PHP script echoes.

When I find the time, I might write a simple contact form using this.
Photo of Anthony Kolber

Anthony Kolber, Official Rep

  • 811 Posts
  • 157 Reply Likes
That's a brilliant solution.
A perfect workaround for the caching issues stacey raises.