Save page from frontend

I wonder if it is possible to save a page from the frontend?
I have some user input related to content stored in a structure field. The user should select the existing content (via ) und save his entry without access to the panel.

Yes, that is possible. You can use $page->update() . Have a look at this post: Add method to append to structure field

1 Like

Works perfectly. Thank you very much!

Anyone interested in sharing the code? I mean all of it for the newbies? :wink:

Of course, no problem. Feel free to ask for details.

Template

<?php if(param('status') == "go"): ?>

<?php 

$description = get('description');
$person = get('name');
$title = get('title');

// get all current entries (where "objects" is the class defined in the blueprint)
$existing_array = $page->objects()->yaml();

// append a new entry to the array
$existing_array[] = array('title' => $title, 'person' => $person, 'description' => $description);

// update the page
$page->update(array('objects' => yaml::encode($existing_array)));

go($page->url().'/status:done');
?>


<?php elseif(param('status') == "done"): ?>

  <h2>Thank you for your entry.</h2>

<?php else: ?>

<?php
// Display the form here
?>

<?php endif ?>

Blueprint

<?php if(!defined('KIRBY')) exit ?>

title: Page
pages: true
files: true
fields:
  title:
    label: title
    type:  text
  text:
    label: text
    type:  textarea
  objects:
    label: objects
    type: structure
    entry: >
      {{title}}<br />
      {{person}}
    fields:
      title:
        label: title
        type: text
      description:
        label: Description
        type: text
      person:
        label: person
        type: text
5 Likes

Hi Oliver,

Thank you for sharing! Can you explain your implementation? The way you use this code? :stuck_out_tongue: I am sorry for the questions!

I’m using it for frontend input from users. For example: A user can report a bug on a webpage. The site prefills the form with the url of the current page. The user can then input the error she/he found and describe it. If she/he clicks the “submit” button of the form, the error is saved on a specific internal page. I could save all of it as a csv-file but the structure-field from Kirby provides more options (e.g. linking a user account to it, that should fix the bug). Feel free to ask any additional questions – that is what a support forum like this is for.

1 Like

Hey there!
Sorry to revive an old plugin (I seem to be doing that a lot lately!).

Your code starts by checking if the url contains a param status: go
Can you share with us what your form action / submit button look like to achieve this?

Also, would this work without refreshing the page?

Cheers!

One additional thing: As discussed a lot in the last time, you should check „user generated content“ for malicious code – especially (or at least) if anonymous users can submit your form. The esc() helper method is your friend :wink:

https://getkirby.com/docs/toolkit/api/helpers/esc

1 Like

There’s a tiny typo in your post :wink:
The esc() method indeed is our friend!

1 Like

Thanks, I fixed it :stuck_out_tongue_winking_eye:

1 Like

I’m currently working on a little tutorial, so we’ll have something in the docs soon.

3 Likes