1
I’m trying to create a concatenated list of client names using only one php entry, but ultimately, the result was not what I expected.
External Part (Interface):
At the beginning, to each name inserted in the input
, the user decides whether to add another name or already display the list with the names already inserted by two buttons called with the names add (+)
and show (Exibir)
respectively. This way, to each button add (+)
clicked, the system will print a new empty entry below the filled one to put another name, and another name, and so on until the user registers all the necessary names and decides to print a list of all the customers that were registered in the system by pressing the button Enviar
.
Internal part (processing code):
1º) In this system will have a counter called $i
which will control the number of names that will be registered in the list, which will also define the number of rows that the column will have;
2º) I created an array called $nome
to store all the names the user registers;
3º) I created a new variable called $conteudo
which will store all the names that the user is entering in a concatenated form;
4º) A function called get_post_action
which has as parameter the variable $nome
, that will return the names of all buttons in the form;
5) I created a switch-case
to define the different actions of buttons add
and show
;
How it would be possible to create a concatenated list of names with the lines inserted through inputs running in PHP?
<?php
$i = 0;
$nome = array("", "", "", "", "", "");
$conteudo = "";
function get_post_action($name){
$params = func_get_args();
foreach ($params as $name) {
if (isset($_POST[$name])) {
return $name;
}
}
}
switch (get_post_action('add', 'show')) {
case 'add':
$i++;
$nome[$i] = $_POST['nome'];
$conteudo += "$nome[$i]<br>";
echo "<input type='text' name='nome'><input type='submit' value='+' name='add'><hr>";
break;
case 'show':
echo "$nome";
break;
default:
$conteudo = "";
break;
}
?>
<form action="teste.php" method="POST">
<input type="text" name="nome">
<input type="submit" value="+" name="add"><hr>
<input type="submit" value="Enviar" name="show">
</form>
It worked right @Vinicius.Silva. Thanks for the guidance! ;-) Now I’ll just make some adjustments to add one more button to remove row by row, for when every time I go to update the script in PHP there are those lines exaggerations. Gratitude! -D
– pe.Math