admin管理员组

文章数量:1314292

How i can load this jquery get response to middle iframe in my page? The php script loads some data from mysql db? in this case i am not passing any data to php so do i need to make get or post request ? my code display the jquery response in alert but doesn't load it into iframe!

    setInterval(function() {

      $.ajax(
    {
        type: 'GET',
        url: './getDataFromMySQLdb.php',
        //data: $("#myform").serialize(),

         data: {
         title: 'test',
          // wrapper: 'testing'
         },
                success: function (good)
                {
                  //handle success

                      alert(good);
                    $("#middle").attr('src',+good); //change url
                },
                failure: function (bad)
                {
                   //handle any errors

                  alert(bad)

                }


    });
    }, 20000);

<iframe name="top" src="blank.php" id="top"  scrolling="no" noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="79"></iframe>
<iframe NAME="middle" src="blank.php" id="middle" noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="238" scrolling="auto"></iframe>
<iframe NAME="foot" src="blank.php" id="foot"  scrolling="no"  noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="200"></iframe>

How i can load this jquery get response to middle iframe in my page? The php script loads some data from mysql db? in this case i am not passing any data to php so do i need to make get or post request ? my code display the jquery response in alert but doesn't load it into iframe!

    setInterval(function() {

      $.ajax(
    {
        type: 'GET',
        url: './getDataFromMySQLdb.php',
        //data: $("#myform").serialize(),

         data: {
         title: 'test',
          // wrapper: 'testing'
         },
                success: function (good)
                {
                  //handle success

                      alert(good);
                    $("#middle").attr('src',+good); //change url
                },
                failure: function (bad)
                {
                   //handle any errors

                  alert(bad)

                }


    });
    }, 20000);

<iframe name="top" src="blank.php" id="top"  scrolling="no" noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="79"></iframe>
<iframe NAME="middle" src="blank.php" id="middle" noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="238" scrolling="auto"></iframe>
<iframe NAME="foot" src="blank.php" id="foot"  scrolling="no"  noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="200"></iframe>
Share Improve this question asked Feb 14, 2013 at 0:08 user1788736user1788736 2,84521 gold badges68 silver badges118 bronze badges 5
  • +good should just be good – Explosion Pills Commented Feb 14, 2013 at 0:14
  • Thanks for reply . I tried good too before i post it doesn't work. I redirect me to a wrong page! – user1788736 Commented Feb 14, 2013 at 0:20
  • What page does it redirect you to? – Explosion Pills Commented Feb 14, 2013 at 0:22
  • it takes me to some advertizement page from my web hoster! – user1788736 Commented Feb 14, 2013 at 0:40
  • That sounds like a 404, invalid domain, or the like. Maybe you should use an absolute path in the URL – Explosion Pills Commented Feb 14, 2013 at 0:41
Add a ment  | 

2 Answers 2

Reset to default 3

You can simply reload directly the iframe every 20 seconds, no need for $.ajax() :

setInterval(function(){
   $("#middle").attr('src', './getDataFromMySQLdb.php');
}, 20000);

However, I think that it will be better for you to not use an iframe at all. Instead, just use a div with the same id='middle', and load the result directly into it:

setInterval(function(){
   $("#middle").load('./getDataFromMySQLdb.php');
}, 20000);

There seems to be some errors in your AJAX script, but you would be right in using a GET request. Please see below for something that might be what you're looking for. Also I can't see why you are loading this information into an iFrame? I would load it into a div instead. I might be pletely misunderstood in what you're trying to achieve so the below might be of no help.

setInterval(function() {
      $.ajax({
        type: 'GET',
        url: '../getDataFromMySQLdb.php',
        data: '',
        success: function (data){
           $("#middleDiv").html(data);
        },
        failure: function (){
           alert('Error message');
        }
    });
}, 20000);

<div id="middleDiv"></div>

本文标签: javascriptHow to load ajax (jquery) response to iframe every 20 secondsStack Overflow