Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从POST数组中获取x个数量的变量_Php_Forms_Post_Http Post - Fatal编程技术网

Php 从POST数组中获取x个数量的变量

Php 从POST数组中获取x个数量的变量,php,forms,post,http-post,Php,Forms,Post,Http Post,我想知道最好的方法是什么。 基本上,我正在准备一份课程的注册表格。用户指定要注册的人数,并在表中显示X行,以输入每个用户的详细信息 此表单提交到将处理数据的php文件。但是,如果我要从POST数组中获取信息,我需要写出100条语句来执行此操作,还是可以动态执行此操作 例如,目前这是我的表格: <form action="#" method="POST"> <table> <th></th>

我想知道最好的方法是什么。 基本上,我正在准备一份课程的注册表格。用户指定要注册的人数,并在表中显示X行,以输入每个用户的详细信息

此表单提交到将处理数据的
php
文件。但是,如果我要从
POST
数组中获取信息,我需要写出100条语句来执行此操作,还是可以动态执行此操作

例如,目前这是我的表格:

<form action="#" method="POST">
        <table>
            <th></th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <? for($i = 0; $i < $qty; $i++):?>
            <tr><td>Guest <?= $i + 1?></td>
                <td><input type="text" id="name" name="name<?= $i + 1 ?>"></td>
                <td><input type="text" id="email" name="email<?= $i + 1 ?>"/></td>
                <td><input type="text" id="phone" name="phone<?= $i + 1 ?>"/></td>
                <input type="hidden" name="course_id" value="<?= the_ID() ?>"/>
                <input type="hidden" name="course_title" value="<?= $course ?>"/>
                <input type="hidden" name="formSend2" value="1" />
                <input type="hidden" name="course_date" value="<?= $date ?>"/>
                <input type="hidden" name="course_location" value="<?= $location ?>"/>
                <input type="hidden" name="course_applicant" value="<?= $user_ID ?>"/>
            </tr>
            <? endfor; ?>
        </table>
        <input type="submit" value="Confirm Registration"/>
        </form>
如果用户选择数量为8,则表单将包含8行,并且在发布时,
var\u dump($\u POST)
会输出以下内容:

array (size=30)
  'name1' => string 'd' (length=1)
  'email1' => string 's' (length=1)
  'phone1' => string 'd' (length=1)
  'course_id' => string '1063' (length=4)
  'course_title' => string 'Energy use in Mushroom Units' (length=28)
  'formSend2' => string '1' (length=1)
  'course_date' => string '23-07-2014' (length=10)
  'course_location' => string 'Teagasc, Thurles' (length=16)
  'course_applicant' => string '1' (length=1)
  'name2' => string '' (length=0)
  'email2' => string '' (length=0)
  'phone2' => string '' (length=0)
  'name3' => string '' (length=0)
  'email3' => string '' (length=0)
  'phone3' => string '' (length=0)
  'name4' => string '' (length=0)
  'email4' => string '' (length=0)
  'phone4' => string '' (length=0)
  'name5' => string '' (length=0)
  'email5' => string '' (length=0)
  'phone5' => string '' (length=0)
  'name6' => string '' (length=0)
  'email6' => string '' (length=0)
  'phone6' => string '' (length=0)
  'name7' => string '' (length=0)
  'email7' => string '' (length=0)
  'phone7' => string '' (length=0)
  'name8' => string '' (length=0)
  'email8' => string '' (length=0)
  'phone8' => string '' (length=0)

我是否必须写入x个变量来检查它们是否在
POST
数组中设置?

就像您在html代码段中生成行一样,您可以从
$\u POST
收集数据

for ($i = 1; $i <= X; $i++) {
  if (isset($_POST['name'.$i]) && $_POST['name'.$i] != '') {
    // user entered something in this row
    // collect all data like that:
    $email = $_POST['email'.$i];
  }
}
用于($i=1;$i如果使用

<input type="text" id="name" name="name<?= $j + 1 ?>">
但我建议您删除for循环并使用HTML中的数组:

<input type="text" id="name" name="name[]">

然后在PHP中,您可以执行以下操作:

for ($j = 1; $j <= count($_POST['name']); $j++) {
    $name = $_POST['name' . $j];
    $email = $_POST['email' . $j];
    $phone = $_POST['phone' . $j];
    // ...
}
for($j=1;$jNah,PHP可以为您提供

试试这个代码

表格编号:

<form action="#" method="POST">
    <table>
        <th></th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
        <? for($i = 0; $i < $qty; $i++):?>
        <tr><td>Guest <?= $i + 1?></td>
            <td><input type="text" id="name" name="guest[<?= $i + 1 ?>][email]"></td>
        </tr>
        <? endfor; ?>
    </table>
    <input type="submit" value="Confirm Registration"/>
    </form>

出于好奇,我为什么要在循环中使用数组选项?它提供了更清晰的HTML构建。
for ($j = 1; $j <= count($_POST['name']); $j++) {
    $name = $_POST['name' . $j];
    $email = $_POST['email' . $j];
    $phone = $_POST['phone' . $j];
    // ...
}
$test =   array(   'name1' =>  'd',
  'email1' =>  's' ,
  'phone1' =>  'd' ,
  'course_id' =>  '1063',
  'course_title' =>  'Energy use in Mushroom Units',
  'formSend2' =>  '1' ,
  'course_date' =>  '23-07-2014' ,
  'course_location' =>  'Teagasc, Thurles' ,
  'course_applicant' =>  '1' ,
  'name2' =>  '' ,
  'email2' =>  '',
  'phone2' =>  '',
  'name3' =>  '' ,
  'email3' =>  '',
  'phone3' =>  '',
  'name4' =>  '' ,
  'email4' =>  '',
  'phone4' =>  '',
  'name5' =>  '' ,
  'email5' =>  '',
  'phone5' =>  '',
  'name6' =>  '' ,
  'email6' =>  '',
  'phone6' =>  '',
  'name7' =>  '' ,
  'email7' =>  '',
  'phone7' =>  '',
  'name8' =>  '' ,
  'email8' =>  '',
  'phone8' =>  '');

  $test = array_filter($test);
  print_r($test);

  -> Array ( [name1] => d [email1] => s [phone1] => d [course_id] => 1063 [course_title] => Energy use in Mushroom Units
            [formSend2] => 1 [course_date] => 23-07-2014 
            [course_location] => Teagasc, Thurles [course_applicant] => 1 ) 
<form action="#" method="POST">
    <table>
        <th></th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
        <? for($i = 0; $i < $qty; $i++):?>
        <tr><td>Guest <?= $i + 1?></td>
            <td><input type="text" id="name" name="guest[<?= $i + 1 ?>][email]"></td>
        </tr>
        <? endfor; ?>
    </table>
    <input type="submit" value="Confirm Registration"/>
    </form>
foreach ($_POST ['guest'] as $guest)
{
     echo $guest['email']
}