从POST数组中获取x个变量

时间:2014-06-23 12:33:35

标签: php forms post http-post

我想知道最好的办法是什么。 基本上我正在编写课程的注册表。用户指定将注册的人数,并在表格中显示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>

这是它提交的脚本

function register_course_handler(){

 var_dump($_POST);
    die();

    $course_id = $_POST['course_id'];
    $course_title = $_POST['course_title'];
    $course_date = $_POST['course_date'];
    $course_location = $_POST['course_location'];
    $course_applicant = $_POST['course_applicant'];

如果用户选择数量为8,则表单将包含8行,并且在发布时var_dump($_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数组中设置?

4 个答案:

答案 0 :(得分:1)

如果您使用

<input type="text" id="name" name="name<?= $j + 1 ?>">

然后在PHP中你可以这样做:

$j = 0;
while (++$j) {
    if (!isset($_POST['name' . $j]))
        break;
    $name = $_POST['name' . $j];
    $email = $_POST['email' . $j];
    $phone = $_POST['phone' . $j];
    // ...
}

但是我建议你删除你的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];
    // ...
}

答案 1 :(得分:0)

就像您在html代码段中生成行一样,您可以从$_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];
  }
}

答案 2 :(得分:0)

不,PHP可以purge all of the empty values为你

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

答案 3 :(得分:0)

试试这段代码。

表格代码:

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

在PHP脚本中

foreach ($_POST ['guest'] as $guest)
{
     echo $guest['email']
}