Input name with dynamic name

1

I want to create a form with a variable number of inputs, according to the amount of data coming from a DB and assign them to the "name" unique names.

Then I need to submit this form and retrieve the data via POST.

I thought I'd do something like:

<input name="nomePopular.'.$id.'"  type="text">

But I do not know how to retrieve this value via POST.

I'm using PHP and MySQL.

Thank you!

    
asked by anonymous 03.11.2015 / 03:13

2 answers

3

Set the field name to array

Instead:

<input name="nomePopular.'.$id.'"  type="text">

Switch to this mode:

<input name="nomePopular[<?php echo $id;?>]"  type="text">

An observation, your original code

<input name="nomePopular.'.$id.'"  type="text">

There seems to be a syntax error in concatenation.

I do not know how the rest of the code is, but this piece of code does not make sense.

If you are "giving an echo", it would be something like this

echo '<input name="nomePopular['.$id.']"  type="text">';

In the script that receives $_POST , request it:

if (isset($_POST['nomePopular']))
{
    foreach ($_POST['nomePopular'] as $id => $v)
    {
        /**
        Isso aqui é um teste com finalidade didática, ok?
        Vai imprimnir na tela o ID e o respectiva valor do campo "nomePopular".
        */
        echo 'id: '.$id.'<br />
        valor: '.$v.'<br /><br />';
    }
}
    
03.11.2015 / 07:31
1

I imagine that when you say "amount of data", you mean number of columns coming from the bank, correct? If so, maybe this will work:

Connecting to the bank:

<?php
   define('DB_NAME', 'insira_aqui_o_nome_do_banco');
   //as informações abaixo são padrão, mas podem variar conforme você configurar seu banco
   define('DB_USER', 'root');
   define('DB_HOST', 'localhost');
   define('DB_PASS', 'root');
   define('DB_PORT', '3306');

  $conexao = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

  if (!$conexao) {
     dir('Erro ao conectar no banco: ' . mysql_error());    
  }
?>

After that, you count how many columns the bank (in this case, any table you choose) is bringing:

<?php 

INCLUDE "conexao.php";

$sql = "DESCRIBE [nome_da_tabela]";  //usando DESCRIBE, o banco vai trazer uma linha para cada coluna da tabela, assim você pode contá-las com mysqli_num_row
$result = mysqli_query($conexao, $sql);
$numColunas = mysqli_num_rows($result); 
?>

Now going to the form:

<?php
for ($i = 1; $i <=$numColunas; $i++) {
?>
<form class="form-group" method="POST" action="cadastro.php">
<label> nomePopular<? echo $i ?></label> /aconselho usar bootstrap para melhorar a aparência 
<input name="nomePopular<? echo $i ?>" type="text">
<?php
}
?>
<button name="submit" id="submit">Submit</button>
</form>

Receiving the inputs on the registration page.php:

<?php
if (isset($_POST["submit"])) {
for ($i = 1; $i <=$numColunas; $i++) {
?>
        $nomePopular . $i = $_POST['nomePopular . $i'];

}

I'm just not sure if this last code to get the POST values is correct because I did not test, maybe syntax error, but already gave an idea. I hope you have helped.

    
03.11.2015 / 06:19