Help get this topic noticed by sharing it on Twitter, Facebook, or email.
I’m tired.

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

So I wanted to implement a contact form.

I patched my html.inc.php 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
@content
endif
foreach $PHP do
@content
endforeach


that includes a .php file ("content/contact/form.php") that checks for
isset($_POST)
, 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 :)
2 people have
this problem
+1
Reply
  • 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')
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • 1
    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":


    <?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":

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

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


    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.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • That's a brilliant solution.
    A perfect workaround for the caching issues stacey raises.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited