Create page in loop creates only one page at a time

hi!

i am sending an array of data through an input form to the backend and have setup a page controller to handle it:

the code works, except i am looping the array and need to create a subpage for each item in the array and only one page is created.

here the code in the controller:

// check if request is POST and input type submit has been clicked
if ($kirby->request()->is('POST') && get('post_comment')) {

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

    // we POST a list of fields all named the same
    // `comment_data[]`, the `[]` put the fields inside
    // a common array.
    // let's loop over the array and process each field,
    // which is JSON stringify value

    $body = get('comment_data');
    // file_put_contents('log.txt', json_encode($body));
    
    foreach($body as $field) {

        // get the data and check if it's all good

        $field = json_decode($field, true);
        $selection_text = yaml::encode($field['content']['selection_text'], true);

        $content = $field['content'];
        $data = [
            'user' => $content['user'],
            'timestamp' => $content['timestamp'],
            'article_slug' => $content['article_slug'],
            'block_id' => $content['block_id'],
            'text' => $content['text'],
            'selection_type' => $content['selection_type'],
            'selection_text' => $selection_text
        ];

        $rules = [
            'user' => ['required'],
            'text' => ['required'],
        ];

        $messages = [
            'user' => 'Please set a name',
            'text' => 'Please write a comment',
        ];

        // if data has some problems stop everything
        // and refresh page
        if ($invalid = invalid($data, $rules, $messages)) {
            go($page->url());
            exit;

        } else {
            // else proceed with requested operation:
            // - act as the API user
            // - create subpage for comment

            $user = $kirby->users()->filterBy('role', '==', 'api')->first();
            $kirby->impersonate($user->email());

            try {

                // make subpage under <current-article>/comments>
                $comments_subpage = $page->find('comments');
            
                $comment = $comments_subpage->createChild([
                    'slug'     => $content['timestamp'],
                    'template' => $field['template'],
                    'content'  => $data
                ]);

                // => done!
                // maybe show an <article>/comment-added subpage
                // explaining what happened?
                go($page->url());

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

    } // -- ends data loop

}

if i send the same form one more time from the frontend, the next item in the list will be created as a subpage, etc. i browsed the forum and the docs and can’t find which silly mistake i am making (:slight_smile: … any tip appreciated.

thanks!

This piece of code after creating the page redirects after the first round in the loop. Redirecting should happen outside the loop. Also, store your alerts in an array.

dang! noob.

thank you!