如何在PHP中动态地向变量追加内容?
我有一张这样的表格:如何在PHP中动态地向变量追加内容?,php,variables,Php,Variables,我有一张这样的表格: <?php if (isset($_POST['artist'])) { // do something } ?> <form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST"> <p class="artist-negative"> <label for="artist"&
<?php if (isset($_POST['artist'])) {
// do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
<p class="artist-negative">
<label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
</p>
<button type="submit">Update</button>
</form>
您可以将生成前端表单标记的foreach
与处理表单提交的foreach
配对。比如:
<?php
$regex = '/^artist_([0-9]+)$/'
foreach (array_keys($_POST) as $key) {
if (preg_match($regex,$key,$matches)) {
$artistId = (int)$matches[1];
// do something with $_POST[$key] according to $artistId
}
}
这个问题的解决方案比我一开始能掌握的要简单得多,但基本上我只需要这么做,这与我最初的问题的关键区别在于前两行:
<?php $artist_form_id = 'artist_'.$artist->ID;
if (isset($_POST[$artist_form_id])) {
// do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
<p class="artist-negative">
<label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
</p>
<button type="submit">Update</button>
</form>
一个id为的隐藏表单输入怎么样?