-1

I have an HTML page and I want to display some information from a MySQL database. I tried putting <iframe src="xxx.php"></iframe> in the HTML source code. When I open the HTML in Firefox and Chrome, the information is displayed correctly (the xxx.php file contains a simple mysql query), but it does not work with IE. After some search, I found that this is a consequence of an IE security problem.

So, my question is: is there an alternative way to display content from a MySQL database into a completely static HTML file? I almost don't know anything about Javascript, Ajax and jQuery, so if they must be used, please guide me in as much detail as possible so that I can follow it.

Thanks for any help!

Don
  • 863
  • 1
  • 8
  • 22
Pam Apple
  • 111
  • 2
  • 10
  • 1
    Why not just provide a URL to xxx.php? In any case you are not displaying anything in a "completely static html" file. – jman Aug 08 '12 at 17:01
  • I am pretty sure iframe works fine in all editions of IE – somnath Aug 08 '12 at 17:04
  • -1 I don't think that question is eligible for upvotes. – fdomig Aug 08 '12 at 17:07
  • @skjaidev: The php file contains a very simple query to mysql database, so i didn't think it is relevant to the issue.
    @somnath: In IE, up to version 8 i still got `the webpage cannot be found...` error
    – Pam Apple Aug 08 '12 at 17:43
  • What I mean is that showing it through an iframe doesn't make it a "static" HTML file. – jman Aug 08 '12 at 17:50
  • @PamApple In IE Internet options> Security> Internet Zone> Custom Level what's this setting? "Launching programs and files in a IFRAME." Does choosing Prompt or Enable make a difference? – somnath Aug 09 '12 at 10:53
  • OK, i will check it some hours later when i go back. My current computer does not have IE – Pam Apple Aug 11 '12 at 03:48

3 Answers3

2

If you have no option but to deliver static html, try consider using an AJAX call via JavaScript.

For example, in jQuery you could do something like this:

<html>
.
.
.
<body>
    <!-- every other piece of html -->
    <div id="your_php_results"></div>
    <script type="text/javascript">
         $(function() {
             $.get('xxx.php', function(data) {
                 $('#your_php_results').html(data);
             });
         });
    </script>
</body>
</html>

xxx.php is the relative path or absolute URL to xxx.php.

In this implementation, xxx.php must generate HTML which is then placed in the div. What the jQuery $.get-call does is that it requests the location you specified in the first parameter and it gets all the output generated by the resource. For example if this was your php-File:

<?php
    //xxx.php
    $data = array('Apfel', 'Birne', 'Banane'); //this is just example data
?>
<ul>
<?php foreach($data as $fruit) : ?>
    <li><?php echo $fruit ?></li>
<?php endforeach; ?>
</ul>

Then the generated output would be:

<ul>
   <li>Apfel</li>
   <li>Birne</li>
   <li>Banane</li>
</ul>

This would be fetched by $.get and the resulting HTML-File after the execution would be:

<html>
.
.
.
<body>
    <!-- every other piece of html -->
    <div id="your_php_results">
    <ul>
        <li>Apfel</li>
        <li>Birne</li>
        <li>Banane</li>
    </ul>
    </div>
    <script type="text/javascript">
         // ...
    </script>
</body>
</html>

If JS is not an option, you are left with the iFrame variant, which also should work in all browsers, including IE.

EDIT: finer example.

Florian
  • 3,366
  • 1
  • 29
  • 35
  • Hi Florian, thank you very much for your detail explanation. The error i got (in the iframe section) is `The webpage cannot be found HTTP 404 ...`. I'm not sure where to putthw `$(function()...` part. my `xxx.php` file echo only one number, is it ok with the requirement "xxx.php must generate HTML"?. Also, how to dealt with the ` – Pam Apple Aug 08 '12 at 17:37
  • i edited to explain it further. If you get a `404` in IE, i suspect issues with you paths, can you try using the absolute URL of `xxx.php`? – Florian Aug 08 '12 at 17:47
  • Hi Florian, i'm reading your edited answer. The path is absolute path (full url). I read some similar issue but they got blank error. – Pam Apple Aug 08 '12 at 17:53
  • could you cross check with Rafael's answer? – Florian Aug 08 '12 at 17:54
  • I create the file `http://2.todaytravels.info/xxx.php` and put in the example content. I created `http://2.todaytravels.info/test.html` and put in your example content. But when i open `http://2.todaytravels.info/test.html` it shows a blank page. Could you tell me what i did wrongly? Thanks! – Pam Apple Aug 08 '12 at 18:24
  • ok, i have resolved. Actually i needed a js file for it to run (Which i didn't know about). So happy! Thank you, Florian – Pam Apple Aug 08 '12 at 19:09
0

You can use ajax call instead and append the information you received from mysql to some page element (say div with id "results"):

$(document).ready(function(){
    $.ajax({
      url: "script.php",
      dataType: "html"
    }).done(function( html ) {
      $("#results").append(html);
    });
});
tijs
  • 566
  • 4
  • 12
0

I think, your problem is related to the same origin policy on iframes of Internet Explorer, and this thread may help you: Blank iFrame in IE

Community
  • 1
  • 1
Rafael
  • 2,827
  • 1
  • 16
  • 17