How to use a Chunk and a Snippet in a Resource, but returning only the Chunk when page is loaded using MODx Revolution?

StackOverflow https://stackoverflow.com/questions/23518024

Question

I have a resource (63) that should get a value from a form (chunk) which will be later handled by a snippet that divides it by 4. Than it gives me the result on the same page as you can see below:

In the resource:

[[$divisionForm]]
[[!divisionFormHandling]]

The divisionForm Chunk:

<form action="[[~63]]" method="post">
  <input type="text" name="value"/>
  <input type="submit" name="division" value="Submit" />
</form>

The divisionFormHandling snippet:

<?php
    $var = $_POST["value"];
    $division = $var / 4;

    if ($division != 2) {
        $error_message = 'It is not the number 8!';
        $output = $error_message;
        return $output;
    }
    else {
        $playing_around = $var + 1;
    }

    working_around = $var * 2;

    $output = 'Playing: '.$playing_around.'<br />';
    $output .= 'Working: '.$working_around.'<br />';

    return $output;
?>

So, when I insert 8 in the form and submit it, I have following result on the screen:

Playing: 9 Working: 16

And when I insert any other number I have the following result, just like expected:

It is not the number 8!

But when I access the page for the first time or refresh it, I still receive the error_message. My problem is: how could I rewrite the code, so at the first time or after a refresh I see only the form (i.e. the Chunck) and not the error message (that comes from the Snippet)?

I'm quite sure that there is a cleaner way to do this, but I need help.

Many thanks!

Was it helpful?

Solution

if (isset($_POST["value"])) { // here first time checking

    $var = (int) $_POST["value"]; // (int) is for security purpose

    // all other your code here ...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top