Updating / creating pages from the front end

I have been following the cookbook on updating from the front end. It works great, but I want to have one form that creates a new page and one form that updates the current page

I have this so far, both forms work fine if I remove the other.

    <?php

     return function ($kirby, $page) {

    // convert to invoice
    if ($kirby->request()->is('POST') && get('convert')) {

        // check the honeypot and exit if is has been filled in
        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

            // authenticate as almighty
            $kirby->impersonate('kirby');

            try {
                // create new invoice

                $newPage = $page->slug().'-inv';

                $invoice = page($page->parent())->createChild([
                 'slug'     =>   $newPage,
                 'template' => 'invoice',
                 'content' => [
                 'title'  =>$page->title(),
                 'total'  => $page->total(),
               ]
             ]);

                // go to new invoice
                if ($invoice) {
                    // store referer and name in session
                    $kirby->session()->set([
                        'referer' => $page->uri()
                    ]);
                    go($invoice->uri());
                }

            } catch (Exception $e) {
                $alert = ['Update failed: ' . $e->getMessage()];
            }
        }

    return [
        'alert' => $alert ?? null,

    ];





    // mark as sent
    if ($kirby->request()->is('POST') && get('sent')) {

        // check the honeypot and exit if is has been filled in
        if(empty(get('name')) === false) {
            go($page->url());
            exit;
        }

            // authenticate as almighty
            $kirby->impersonate('kirby');

            try {
                // update page to sent
                $page->changeStatus('listed');




            } catch (Exception $e) {
                $alert = ['Update failed: ' . $e->getMessage()];
            }
        }

    return [
        'alert' => $alert ?? null,

    ];
};

You return statements are not within the if-statements, so if the first if is not true, it return the alert and that’s it.

If I put my return statements inside the if statements, $kirby is no longer defined …

Then remove the first return statement and only leave the one at the end.

Once you use a return statement, the rest of your code is ignored.

Ah got it, many thanks for being so patient !