【问题标题】:PHP won't echo HTML with Checkbox Array ResultsPHP 不会使用复选框数组结果回显 HTML
【发布时间】:2017-08-27 22:06:23
【问题描述】:

我被难住了!!!我得到了漂亮的结果,但是当用户选择某些内容时,我无法让相关文本“仅”显示。就像现在一样,文本是静态的,并且始终显示,无论用户是否选择了答案。

例如。 “什么样的动物:”在结果页面上,即使用户没有为它选择答案!如果没有相关答案,这就是不需要显示的内容。

我的 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:

        <p class="data_answers">
    <?php if (isset($_POST['whatKindofAnimal'])) {echo '<strong>What Kind of Animal:</strong> '; $name = $whatKindofAnimal = implode(', ', array_filter($_POST['whatKindofAnimal'])); echo htmlspecialchars($whatKindofAnimal);} ?>

我一直在整个页面中使用此 PHP 代码的第一部分作为单选按钮,它运行良好!这就是我在其他地方为单选按钮设置的方式......并且相关文本仅在用户实际选择答案时显示。

  <p class="data_answers">
    <?php if (isset($_POST['bite-reported-human'])) {echo '<strong>Bite Reported:</strong> '; echo htmlspecialchars($_POST['bite-reported-human']);} ?></p>

在这种情况下,“Bite Reported:”在结果页面上保持不可见,除非已为其选择答案。

请帮忙????

在此先感谢您, 特蕾西

【问题讨论】:

    标签: php html checkbox echo


    【解决方案1】:

    老兄的问题是输入字段

    <input class="form_style" type="text" placeholder="Please Specify" id="otherKindofAnimal" name="whatKindofAnimal[1]" />
    

    即使此字段为空,它也会通过 $_POST 发送数据,您可以通过 print_r($_POST) 对其进行测试,空字符串将在 $_POST['whatKindofAnimal'][1] 所以试试这个

    替换

    if (isset($_POST['whatKindofAnimal']))
    

    if (count($_POST['whatKindofAnimal'])>1)
    

    因为 isset($_POST['whatKindofAnimal']) 将始终为真,因为 $_POST['whatKindofAnimal'][1]

    中总会有一个值

    【讨论】:

    • 我的问题不在于复选框及其结果......它们完美地返回......直到剥离数组中的最后一个逗号。我的问题是为什么在结果中显示出什么样的动物选择零答案?是什么样的动物?
    • 对不起,我一开始没有清楚地理解这个问题,希望我编辑的答案对你有帮助
    • 您好,谢谢!我忙于做其他事情,然后又回到了这个问题上。您编辑的答案很好地修复了它!谢谢!
    • 欢迎快乐编码
    【解决方案2】:

    由于 'Other-Animal' 是一个文本字段,它即使是空的也会被提交,使得 $_POST['whatKindofAnimal'] == array([1]=>''),所以 isset 代码将运行。

    可能有很多解决方案,但一个快速的方法是更改​​ Other-Animal 字段的名称,并在选中 Other 时显示它

    /*form adjustments*/
    <label for="Other-Animal"><input class="form_style" type="checkbox" id="Other-Animal" name="whatKindofAnimal[1]" value="Other: " />Other</label>
    
    <input class="form_style" type="text" placeholder="Please Specify" id="otherKindofAnimal" name="textForOther" />
    
    /*addition to your if(isset) php*/
    if(array_key_exists(1,$_POST['whatKindofAnimal']))
            echo $_POST['textForOther'];
    

    如果选择 other,结果将显示为 Other: {input},如果未单击任何复选框,则根本不会显示。

    【讨论】:

    • 不,实际上,当字段为空时,文本字段结果不返回任何内容;并在填充时返回正确的结果(以及选中的任何复选框)。我的问题根本不在于结果......那些是完美的。我的问题是它没有回显 html 'What Kind of Animal: '
    • 对不起,我误会了。当我测试您的代码时,What Kind of Animal 确实出现了,即使没有选择任何内容,因此我提出了建议。
    • 是的,这确实解决了这个问题,但随后我的其他文本框被踢出了阵列。而不是答案,现在我正在“继续”
    • “开”来自“其他”复选框,值为空。在我的调整中,我将该复选框的值更改为“其他:”。然后我更改了用户输入其他动物可能是什么的文本字段的名称。它可能与原始名称太接近了,所以我将其更新为更加独特。它现在称为“textForOther”,但请使用对您有意义的任何内容。如果有人选中“其他”,您的 (isset($_POST['whatKindofAnimal'])) 现在将显示“其他:”。将我的 array_key_exists 代码添加到其中将显示来自“textForOther”的输入。
    猜你喜欢
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 2017-12-23
    相关资源
    最近更新 更多