douyue9704 2016-10-14 14:00
浏览 64
已采纳

PHP创建的HTML是按顺序创建的

I have a PHP file which outputs some HTML.

For ease of configuration I have a second PHP file that is included in the first PHP file. This included file simply contains an array definition as follows:

<?
$myArray = array("key1"=>"value 1","key2"=>"value 2","key3"=>"value 3");
?>

In my first PHP file I have the following

<? include("second.php"); ?>

<label>Select here:
<select id='mySelect'>
<? foreach ($myArray as $keyString=>$valueString) { ?>
    <option value='<?= $keyString; ?>'><?= $valueString; ?></option>
<? } ?>
</select>
</label>

However the HTML output is generated as follows:

<label>Select here:
<select id='mySelect'>
</select>
<option value='key1'>value 1</option>
<option value='key2'>value 2</option>
<option value='key3'>value 3</option>
</label>

which renders as an empty select dropdown with the text of the options shown below it!

I'm sure this is something stupid and simple as I'm sure I have done this many times before without problems...


[Update]

After investigation it turns out that this is only happening when I display the first PHP page within FancyBox - when I load it directly the options are included in the <select> as you would expect.

The FancyBox is opened via JavaScript via the onClick event of an image as follows (the image is on the background page that the FancyBox opens on, it is not part of first.php or second.php):

<img src="myImage.png" alt="my image" onClick="openFirstPHP();">

in my JavaScript I have (in first.js which is included in first.php with the usual <SCRIPT> tags):

function openFirstPHP() {
    $.fancybox({
        href: "/first.php",
        type: "ajax",
        afterShow: function() {
            // do some setting of some radio buttons' states - which works fine
        }
    });
}

For info, the reason I call first.php this way rather than in an iFrame or whatever is that it interacts with the calling page as well and so avoid me having to reference a different document object (I think).

Basically everything else works without problems in first.php - the reading and settings of cookies, the interaction with the calling PHP page variables etc. The only thing that is not working is the insertion of these <options> tags out of sequence with the <select> tags.

  • 写回答

4条回答 默认 最新

  • dongpiaozhao6836 2016-12-08 10:53
    关注

    This turned out to be an odd one caused by a typo in my HTML markup rather than a problem with PHP or out-of-sequence markup generation.

    My opening <select> statement had a stray / in it. i.e.

    <select id='mySelect' />.
    

    Because browsers do their best to make sense of malformed HTML, looking in the inspectors the <select> was immediately followed by a </select> then my options and the closing </select> from my code was being dropped entirely so the <option> statements were outside of the select and so being ignored.

    Oddly, though, this only happened when called inside FancBoxy, when calling the page directly it worked without problems!

    Removing the mis-placed / in the openeing <select> statement completely resolved the issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器