简体   繁体   中英

how to insert php on html5

I wanted to insert php on html5

i trying copy code from 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 在此处输入图片说明

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. NOTE :: it should be .php extension file

    <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

for easy solution that will fit your situation, ill assume this is your PHP code:

//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)

<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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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