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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类