skip to Main Content

Is it possible to use Ajax, Jquery or Javascript to call a specific PHP Function and refresh / reload it every 10 seconds for example inside a specific Div or areas?

Connection.php

function TerminalStatus ($IPAddress, $portStatus ) // Responsible for current terminal status
{
    $connectStatus = fsockopen($IPAddress, $portStatus, $errno, $errstr, 10); // Build cconnection to Terminal socket 25001
    if (!$connectStatus) {
        echo "$errstr ($errno)<br />n";
    } else {
       $Status =  fgets($connectStatus) ;
        echo $Status ();
    }
}

This connection is just to see the current status of a terminal.
I want to see the status of this function at the bottom of my index.php without refreshing the whole page.

I can accomplish this by putting this function in its own PHP Files (status.php) and using Javascript in the following way:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
    var auto_refresh = setInterval(
        function ()
        {
            $('#Status').load('status.php');
        }, 1000); // refresh every 1000 milliseconds
</script>

But i just want to utilise the function instead.
Is this possible?

4

Answers


  1. That’s impossible to do this operation just with the PHP function.

    you should use javascript as you use that or use socket in javascript to connect you status.php and update without refresh the whole page.

    Login or Signup to reply.
  2. jQuery::load() supports fragment identifiers. So you can just load the part that you want to replace:

    $( "#Status" ).load( "status.php #PartId" );
    

    This will load the HTML output of the script and extract the part. The PHP script will run completely – the rest of the HTML output will be thrown away on the JS side.

    Login or Signup to reply.
  3. I’m not sure if i understood the problem but you can use AJAX to execute specific function. Something like this:

    First build your ajax:

    $.ajax({ 
      type: "POST",
      url: "URL_TO_PHP_FILE",  
      data: "refreshStatus", // this will call the function      
      success: function(status){          
        $('#Status').text(status); // this will load the info you echo
      },
    });
    

    Since you want to do it every second – wrap the whole thing with interval (i use your code from the question):

    var auto_refresh = setInterval( function () {
        $.ajax({ 
          type: "POST",
          url: "URL_TO_PHP_FILE",  
          data: "refreshStatus",
          success: function(status){          
            $('#Status').text(status); 
          },
        });
    }, 1000); 
    

    Then, on you PHP_FILE add condition the execute the specific function when POST been done:

    if ($_SERVER["REQUEST_METHOD"] == "POST" && $_POST['refreshStatus']) {
        // run this code 
    }
    

    Is that what you aimed to achieve?

    Login or Signup to reply.
  4. The solution you have already is the correct way to do this: the JavaScript fetches a URL, and that URL renders the appropriate piece of content.

    It’s important to remember that, as far as the web browser is concerned, PHP doesn’t exist. Any request from the browser – whether you’ve typed in a URL, followed a link, submitted a form, made an AJAX request, etc – is just a message to some remote server for a particular URL, perhaps along with some extra headers and body data. When the server receives that request, it can do whatever it likes to generate a response to the browser.

    So when you write $('#Status').load('status.php');, the browser is sending a request to the server, which happens to be configured to execute the PHP script status.php. You can then do what you like in PHP to produce the response – but there is no direct link between a request and a PHP function.

    However, as others have pointed out, you don’t have to create a new PHP file for every piece of behaviour you want, because inside the PHP code you can check things like:

    • the query string parameters, in $_GET
    • submitted form data, in $_POST
    • the HTTP headers from the request

    These can be set by your JavaScript code to whatever you like, so you could for instance write $('#Status').load('index.php?view=statusonly'); and then at the top of index.php have code like this:

    if ( $_GET['view'] === 'statusonly'] ) {
        echo get_status();
        exit;
    }
    

    How you arrange this is entirely up to you, and that’s what programming is all about 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search