【发布时间】:2017-08-22 14:08:02
【问题描述】:
必须有大量与此相关的主题和帮助,但我无法弄清楚如何实现任何与我的设置一起使用的东西。
HTML:
<!-- SHOW/HIDE TEXT FIELD -->
<!-- IF BITTEN ANIMAL="YES" or BITTEN "BOTH" Show Field -->
<div id="hidden_textfield10" style="display:none;">
<p class="data_bold">What kind of animal</p><div class="data_small">What kind of animal has your pet bitten? Check all that apply:</div>
<label for="Wild"><input class="form_style" type="checkbox" value="Wild" id="Wild" name="whatKindofAnimal[5]" />Wild</label><br />
<label for="Bird"><input class="form_style" type="checkbox" value="Bird" id="Bird" name="whatKindofAnimal[4]" />Bird</label><br />
<label for="Cat"><input class="form_style" type="checkbox" value="Cat" id="Cat" name="whatKindofAnimal[3]" />Cat</label><br />
<label for="Dog"><input class="form_style" type="checkbox" value="Dog" id="Dog" name="whatKindofAnimal[2]" />Dog</label><br />
<label for="Other-Animal"><input class="form_style" type="checkbox" id="Other-Animal" name="whatKindofAnimal[1]" onclick="if(!this.checked) document.getElementById('otherKindofAnimal').value='' " />Other</label>
<!-- IF BITTEN ANIMAL="Other" Show Field -->
<div class="Other-Animal" style="display:none;">
<input class="form_style" type="text" placeholder="Please Specify" id="otherKindofAnimal" name="whatKindofAnimal[1]" />
</div>
<!-- END "Other" Field -->
PHP:
<strong>What Kind of Animal:</strong> <?php $name =
$whatKindofAnimal = implode(", ", $_POST['whatKindofAnimal']); {echo
htmlspecialchars($whatKindofAnimal);} ?></p>
我知道最后一个单选按钮和文本框的名称相同,但这适用于我的设置,与我的困境无关。忽略 HTML。
如何更正 php 以删除最后一个逗号?
谢谢!!! 特蕾西
【问题讨论】:
-
内爆前过滤掉数组的空元素:
implode(', ', array_filter($_POST['whatKindofAnimal'])). -
你太棒了!谢谢!是的,我已经阅读了有关 array_filter 以及其他一千条建议,但就像我说的,我不知道如何或在哪里添加它;并正确添加。谢谢你!我希望这是我的最后一个小困境,这种形式可以正确产生结果。再次,谢谢你!!!
-
不是上面链接的页面的完整副本。我以前读过那篇文章,但对我的情况没有帮助。 deceze 上面给出的答案很容易用 2 个单词和几个字符解决了我的问题。希望它对其他人也有帮助!