Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:12:35+00:00 2026-05-31T22:12:35+00:00

Hello i am having trouble with passing a php return value to the innerHTML

  • 0

Hello i am having trouble with passing a php return value to the innerHTML method to change my div’s value on a fixed interval.

this is my code, i inclue the php file containing the method, and then create an object of the class containing the method, then i want to echo the return value to the innerHTML function. but is does not work.

var t = setInterval(function() {
  document.getElementById("content").innerHTML = "\
    <? include_once("php/functions.php");         \
       $cont = new Content();                     \
       echo ($cont->getPosts());                  \
    ?>";
}, 1000);

However this works so a php value can be echod to the innerHTML function.

var t = setInterval(function() {
  document.getElementById("content").innerHTML = "<? echo "Some Text"; ?>";
}, 1000);

there is noting wrong with the <? include_once("php/functions.php"); $cont = new Content(); echo ($cont->getPosts());?> statement, because when i put it outside of the <script> tags it does what it needs to, however it does not refrech every few seconds

thanks in advance
i tried to make the question as clear as possible, if you have questions however, just ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:12:36+00:00Added an answer on May 31, 2026 at 10:12 pm

    I suspect what you mean by “does not work” is that it only puts the same thing out every second. This is because PHP is run on the server-side BEFORE the browser gets any HTML, and this makes the code look something like this when it arrives to the browser:

    var t = setInterval(function(){document. getElementById("content"). innerHTML = "whatever php produced";}, 1000);
    

    This means that your browser will just put the static text “whatever php produced” into your div every time the interval fires.

    To solve this you will need a slightly different approach. Use AJAX to request an updated version from a PHP script that return just what is suppoesd to be inside the div. jQuery has a function to do this for you if you are not familiar with AJAX yourself.

    var t = setInterval( function(){$( '#content' ).load( '/url/to/serverside/script.php' )}, 1000 );
    

    Also be aware that AJAX is asynchronous, meaning static timing is a bad idea. Instead you should use a on-complete function to fire the next load.

    function loadContent() {
        $('#content').load( '/url/to/serverside/script.php', function( r, s, x ) {
              setTimeout( loadContent, 1000 );
        });
    }
    
    loadContent();
    

    The server-side script can be something like what you originally had:

    <?
    include_once("php/functions.php");
    $cont = new Content();
    echo ($cont->getPosts());
    ?>
    

    Reference:
    http://api.jquery.com/load/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble understanding why in this code snippet the Hello statement is
Hello I have been having trouble with this for a while now. I have
Hello everyone I'm currently having 2 issues with the code below: Upon return of
Hello I'm 17 and trying to learn how to code. I am having trouble
Hello I'm having trouble figuring this out. I have these structs and classes. struct
Hello I am having trouble regarding div reloading when a new record has been
Hello i'm having trouble with making like a simple waiting time between 2 php
I'm having trouble understanding the value of this in the following example: struct A
Hello I am having a hard time making this UI element look the way
Hello I'm having problems sending arrays, structs and arrays of structs from PHP to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.