0

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 enter image description here

but it doesn't show anything. what do i miss?

1
  • 2
    Where is your PHP code? Commented Jul 14, 2015 at 8:14

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.