Calling PHP function on page onload

Asked

Viewed 1,135 times

-1

I want to call a PHP function that is in the same file. I tried this within HTML code.

<?php select($dir, $base); ?>

And my PHP function:

function select($dir, $base){       
    $base_hndl  =   new SQLite3($dir.$base);                
    $requete    =   "SELECT id, title, start, end, description FROM \"event\" ORDER BY id DESC";
    $resultat   =   $base_hndl->query($requete);     
    $affiche    =   $resultat->fetchArray();

    echo "<br><label><b>ID: $affiche[id]</b></label><br>";
    echo "<label><b>Title: $affiche[title]</b></label><br>";    
}

1 answer

1

It is not possible to do this directly.

PHP is a server-side technology that is not directly accessible via HTML or Javascript.

To make a PHP call from the event onload, the simplest is to put the function in another PHP file and make an AJAX request to this file. With the response returned, in Javascript you must update the HTML to reflect the returned data.

I believe you are a beginner, so there is plenty to study here. I recommend looking at the pages in English Wikipedia on PHP, Javascript and AJAX just to get an introduction on what each technology is about.

Once this is done, I recommend that you look into the jQuery library, which makes life easier in DOM manipulation and AJAX request creation.

Then try to build the codes, and in case you encounter more difficulties, write questions describing in detail the errors and difficulties encountered.

Wikipedia: Ajax

Wikipedia: Javascript

Wikipedia: PHP

jQuery

jQuery.ajax

  • 1

    I think a basic example could help you understand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.