Dynamic name input name

Asked

Viewed 761 times

1

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

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

I thought I’d do something like:

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

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

I’m using PHP and Mysql.

Thank you!

2 answers

4


Set the field name to array

Instead:

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

Switch to that way:

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

An observation, its original code

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

There seems to be syntax error in concatenation.

I don’t know what the rest of the code looks like, but this bit that you presented doesn’t make sense.

In case you’re "giving an echo," it would be something like this

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

In the script that receives the $_POST, request so:

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 />';
    }
}
  • 1

    Note that it is possible to omit id without any loss of functionality: <input name="nomePopular[]" type="text">. PHP will populate the index incrementally, provided from 0.

1

I guess when you say "amount of data," you mean amount of columns coming from the bank, right? If so, maybe it works:

Connecting in 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 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 inputs on the.php sign-up page:

<?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 values of the POST are correct because I didn’t test it, maybe I have syntax error, but I could give an idea. I hope it helped.

  • Sorry, I was not clear mt on the question. I meant q were table rows (1:N relationship) and not columns. But still I appreciate your help.

  • Good example using columns!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.