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.

查看全部
douyue9704
douyue9704
2016/10/14 14:00
  • fancybox
  • php
  • html
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

4个回复