Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create input groups within forms and access them via POST in PHP

Tags:

html

php

mysql

Is it possible to create inputs groups and access them via $_POST with PHP in a associative-array way? I have a form where user can input information about products. Each product has a name and a description.

Simple solution

In a typical form, I would create a HTML structure like:

<form method="post" id="insert" action="test.php">
  <!-- First product -->
  <input type="text" name="title1"/>
  <input type="text" name="description1"/>

  <!-- Second product -->
  <input type="text" name="title2"/>
  <input type="text" name="description2"/>

  <input type="submit" name="submit" value="Insert products" />
</form>

and access data via PHP with:

if(isset($_POST['submit']))
{
  echo 'Submitted data:<br/>';
  echo 'title='.$_POST['title1'].' description='.$_POST['description1'].'<br/>';
  echo 'title='.$_POST['title2'].' description='.$_POST['description2'].'<br/>';
}


Tricky (but harder) pseudo-solution

What I would like to create is a HTML pseudo-code where product inputs are grouped in structures with a title and a description, like this:

<form method="post" id="insert" action="test.php">
  <!-- First product -->
  <div name="products[]">
    <input type="text" name="title"/>
    <input type="text" name="description"/>
  </div>

  <!-- Second product -->
  <div name="products[]">
    <input type="text" name="title"/>
    <input type="text" name="description"/>
  </div>

  <input type="submit" name="submit" value="Insert products" />
</form>

PHP pseudo-code to access inputs:

if(isset($_POST['submit']))
{
  echo 'Submitted data:<br/>';

  foreach($_POST["products"] as $product)
  {
    echo 'title='.$product['title'].' description='.$product['description'].'<br/>';
  }
}

Is it feasible? Thanks in advance.

like image 981
Giorgio Avatar asked Sep 04 '26 14:09

Giorgio


1 Answers

Yes you can use a grouping name for that attribute so that when it is submitted it is grouped. The group name="" in that div is incorrect, it must be on the input elements. Example:

<?php

if(isset($_POST['submit'])) {
    $products = $_POST['products']; // assuming they are all filled, they will be here inside
    echo '<pre>';
    print_r($products); // check the results
}


?>

<form method="post" id="insert" action="">
  <!-- First product -->
  <!-- group them by row index -->
  Title: <input type="text" name="products[1][title]"/>
  Description: <input type="text" name="products[1][description]"/>
  <br/><br/>
  <!-- Second product -->
  Title: <input type="text" name="products[2][title]"/>
  Description: <input type="text" name="products[2][description]"/>
  <br/><br/>
  <input type="submit" name="submit" value="Insert products" />
</form>

Super basic insertion example (this is just an example, you can use it or not):

if(isset($_POST['submit'])) {
    $products = $_POST['products'];

    $db = new mysqli('localhost', 'username', 'password', 'database');
    $insert = $db->prepare('INSERT INTO `table_name` (`title`, `description`) VALUES (?, ?)');
    foreach($products as $product) {
        $insert->bind_param('ss', $product['title'], $product['description']);
        $insert->execute();
    }
    $insert->close();
}
like image 178
Kevin Avatar answered Sep 07 '26 06:09

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!