简体   繁体   English

如何在html5上插入php

[英]how to insert php on html5

I wanted to insert php on html5 我想在html5上插入php

i trying copy code from w3schools 我试图从w3schools复制代码

<!DOCTYPE html>
<html lang = "en-US">
 <head>
 <meta charset = "UTF-8">
 </head>
 <body>
    <div id="result"> </div> 

  <script>
if(typeof(EventSource) !== "undefined") {
    var source = new EventSource("do_dropdown.php");
    source.onmessage = function(event) {
        document.getElementById("result").innerHTML += event.data + "<br>";
    };
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
 </body>
</html>

my do_dropdown.php just select data from drop down list 我的do_dropdown.php只是从下拉列表中选择数据 在此处输入图片说明

but it doesn't show anything. 但它什么也没显示。 what do i miss? 我想念什么?

why don't you try this ? 你为什么不试试这个呢? I think, It should not create any problem even if you're using HTML5. 我认为,即使您使用的是HTML5,它也不应该造成任何问题。 NOTE :: it should be .php extension file 注意::它应该是.php扩展文件

    <html>
    <body>
            <?php
                    ....your code
                    ....from file <do_dropdown.php>
            ?>
    </body>
    </html>

you can also create a function and call it from here. 您还可以创建一个函数并从此处调用它。

the right thing to do, for my opinion is to send your options data as JSON and to parse it with JS to required dropdown 正确的做法,我认为是将您的选项数据作为JSON发送,并使用JS解析为所需的下拉列表

for easy solution that will fit your situation, ill assume this is your PHP code: 为了适合您的情况的简单解决方案,请假设这是您的PHP代码:

//file name: do_dropdown.php
<?php
  $options = [1,2,3,4,5];
  echo '<select name="name">';
  foreach $options as $option{
     echo "<option value=$option>$option</option>";
  }
  echo '</select>';
?>

so you need to change your html file to PHP file (file.html -> file.php) and make sure you run on it PHP server (localhost as you are using right now) 因此,您需要将html文件更改为PHP文件(file.html-> file.php),并确保在PHP服务器(当前正在使用的本地主机)上运行

<div id="result">
  <?php
    require_once "do_dropdown.php";
  ?>
</div>

there are much better solutions for this, but I guess your'e new in business so good luck :) 有很多更好的解决方案,但是我想您是个新手,祝您好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM