php - 在表单中创建输入组并通过 PHP 中的 POST 访问它们

标签 php html mysql

是否可以创建输入组并通过 $_POST 以关联数组方式使用 PHP 访问它们?我有一个表格,用户可以在其中输入有关产品的信息。每个产品都有名称和描述。

简单的解决方案

在典型的形式中,我会创建一个 HTML 结构,如:

<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>

并通过 PHP 访问数据:

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


棘手(但更难)的伪解决方案

我想创建的是一个 HTML 伪代码,其中产品输入以带有标题和描述的结构分组,如下所示:

<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 伪代码:

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

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

可行吗?提前致谢。

最佳答案

是的,您可以为该属性使用分组名称,以便在提交时将其分组。该 div 中的组 name="" 不正确,它必须在输入元素上。示例:

<?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 基础的插入示例(这只是一个示例,你可以用也可以不用):

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();
}

关于php - 在表单中创建输入组并通过 PHP 中的 POST 访问它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25724890/

相关文章:

MySQL 5.6.19 TIMESTAMP 不能接受很多正确的值

mysql - 如果索引字段也是外键,我应该使用多索引方法吗?

php - CodeIgniter:在登录系统中输入正确的用户名和密码时,Google Chrome 中出现空白页面(Mozilla Firefox 中没有任何事件)

html - 这个 HTML/CSS 元素叫什么?

HTML & CSS : Sticky as overlay

带有按钮的 jQuery 移动标题位置

mysql - 如何从两个不相关的表中进行选择

php - 未定义的属性:Code Igniter 中的 stdClass::$contents

php - 子文件夹中的 .htaccess

php - MYSQL 查询和 Chart.js 和 PHP