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

Is it possible to create input groups and access them $_POST

via PHP in the form of an associative array? I have a form where a user can enter information about products. Each product has a name and description.

A simple solution

In a typical form, I would create an 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 the data through 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/>';
}

      


Difficult (but harder) pseudo solution

What I would like to create is HTML pseudo code where product inputs are grouped into structures with title and 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 pseudocode for accessing inputs:

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

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

      

Is it possible? Thanks in advance.

+3


source to share


1 answer


Yes, you can use the grouping name for this attribute so that it gets grouped when you submit it. The group name=""

in this div is wrong, 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>

      



An example of a super basic insert (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();
}

      

+3


source







All Articles