MODX Community

Help with updating page TV from user input

Hi,

I would appreciate some pointers. I have a site with collections with a list of bikes with data associated and I would like to update a TV on the bike pages with some user input.
The input will be sanitized, inspected, patted down for foreign objects etc so that’s fine I just can’t work out how to get the correct page/TV object and update it.
I can get the data (pdotools is very cool) and display it, but putting some back in again via a snippet has got me confused.
it’s a modx cloud install, modx 2.8.1, php7.1

Cheers
Karl

Here is a basic example:
Put this part in the page template. It calls a snippet (uncached) and outputs a form.

[[!saveTV]]

<form method="post" action="[[~[[*id]]]]">
    <input type="text" id="myTV" name="myTV">
    <input type="submit" value="Save TV">
</form>

Then create the snippet saveTV:

<?php
if (isset($_POST['myTV'])) {
    //maybe check user permissions
    
    $my_tv = $_POST['myTV'];
    //validate or sanitize the data
    
    $res = $modx->resource; //current resource
    //$res = $modx->getObject('modResource', $resource_id); //or load a different resource
    if ($res) {
        $res->setTVValue('name_of_my_tv', $my_tv);
    }
}