I generate text boxes in js based on a value chosen by a number type input :
function updateChannels(input)
{
const container = document.getElementById('description-container');
const count = parseInt(input.value, 10);
// Empty the container to reset the descriptions
container.innerHTML = '';
// Vérifie que l'entrée est un nombre entre 1 et 4
if (isNaN(count) || count < 1 || count > 4)
{
input.value = ''; // Reset if the value is invalid
alert('Please enter a number between 1 and 4.');
return;
}
// Add description fields based on value
for (let i = 1; i <= count; i++)
{
const label = document.createElement('label');
label.textContent = `Please describe your channel number ${i}`;
const textarea = document.createElement('textarea');
textarea.name = `channel_description_${i}`;
textarea.id = `channel_description_${i}`;
textarea.className = 'form-control';
textarea.placeholder = `Description for channel ${i}`;
// Add the label and text box to the container
container.appendChild(label);
container.appendChild(textarea);
}
}
So, if I choose 1, a text box is displayed, etc… up to 4.
I retrieve all the data from my form with a $request variable in the route, here is the code :
Route::post('/submit_before', function (Request $request)
{
// Enregistrement de la manipulation
$manipulation = Manipulation::create([
'users_id' => $request->input('user'),
'machines_id' => 1,
'manipulation_name' => $request->input('manipulation_name'),
'system_issue' => $request->input('system_free_issue'),
'system_qualified' => $request->input('system_qualified'),
'howmany_injections' => $request->input('channelCount'),
'column_id' => $request->input('column_description'),
'guard_column_id' => $request->input('guard_column_description'),
'type_samples' => $request->input('type_of_samples'),
]);
// Récupérer l'ID de l'enregistrement créé
$manipulationId = $manipulation->id;
//récupérer le nombre de description de channels :
$channelCount = $request->input('channelCount');
// Boucler pour insérer chaque description de canal
for ($i = 1; $i <= $channelCount; $i++) {
// Construire dynamiquement le nom de l'input pour chaque description de canal
$description = $request->input("channel_description_$i");
// Insérer dans la table channels_descriptions
ChannelsDescription::create([
'description' => $description,
'manipulation_id' => $manipulationId,
'channel_number' => $i, // Numéro du canal (1, 2, etc.)
]);
}
// Rediriger vers la vue `chrono` après l'enregistrement
return view('chrono');
})->name('submit_before'); // Déplacement de ->name() avant le point-virgule
The problem is that everything is retrieved except the value of my text boxes which are generated dynamically (the name of textareas begins with channel_description_);
#parameters: array:9 [▼
"_token" => "0dusC043ovYJMZGShMlsBdioLY3nR8Zyw00zrqAO"
"user" => "1"
"manipulation_name" => "eerrere"
"system_free_issue" => "yes"
"system_qualified" => "yes"
"channelCount" => "2"
"column_description" => "21"
"guard_column_description" => "17"
"type_of_samples" => "reeee"
They are NULL.
$description = $request->input("channel_description_$i");
null // routes/web.php:72
If I have two, two rows are inserted into the database, but NULL is put in description.
On the other hand, if the text boxes are present when the form loads, I get their values in the $request variable.
Do you have any idea please? Thanks in advance.