Hello,
I’m trying to make a form but I’m little confused.
I use “User sign-up” and “Uploading files from frontend” cookbook to start.
I would like to make form to store simply a name and a score.
Later, I would like to return all the name with their score in table.
So i create a snippet score.php (because it’s reuse in differents page)
snippet/score.php
<form action="" method="post" enctype="multipart/form-data">
<div class="honeypot">
<label for="website">Website <abbr title="required">*</abbr></label>
<input type="website" id="website" name="website">
</div>
<div class="form-field">
<label for="name">Nom</label>
<input required name="name" type="text" maxlength="3">
<label for="score">Score</label>
<input required name="score" type="number" >
</div>
<input type="submit" name="submit" value="Submit" class="button">
</form>
A controller
controllers/score.php
<?php
return function ($kirby) {
var_dump($kirby);
$errors = [];
$success = '';
if ($kirby->request()->is('post') === true && get('submit')) {
// check the honeypot
if (empty(get('website')) === false) {
go($page->url());
exit();
}
$data = [
'name' => get('name'),
'score' => get('score'),
];
$rules = [
'name' => ['required', 'maxLength' => 3],
'score' => ['required', 'integer'],
];
$messages = [
'name' => 'Your name must have at max 3 characters',
'score' => 'Should be a number',
];
if ($invalid = invalid($data, $rules, $messages)) {
$errors = $invalid;
} else {
try {
$score = page('score')->update([
'name' => $data['name'],
'score' => $data['score'],
]);
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
}
}
return compact('alerts', 'success');
};
And a new content folder “score” and a new txt file “score.txt”
content/score/score.txt
Title: Score
Nothing append, it seem I don’t go to my controller.
Also, I think that my try in my controller isn’t correct, but I didn’t find the best method to update the file.
Can you give me some trails
Thank you have a nice day