How To Call Php Function In Html

Table of contents:

How To Call Php Function In Html
How To Call Php Function In Html

Video: How To Call Php Function In Html

Video: How To Call Php Function In Html
Video: How to Execute a PHP Function on Button Click 2024, April
Anonim

HTML is a markup language that allows you to pass certain parameters to a script written in PHP for further processing. To call a PHP function in HTML, you can use the POST and GET transmission methods commonly used in web programming.

How to call php function in html
How to call php function in html

POST

The POST method allows you to pass information that has been entered by the user into a web form, enclosed in tags. All recorded information will be stored in the form fields, and after clicking on the button, the data will be copied to the $ _POST global array, through which the form handler function can be called.

This mechanism can be used to create a registration form or feedback from visitors. The systems for commenting records work according to this principle, for example, in the news feed, guest books, forums, chats, etc.

To apply the method, you must first declare the desired function in the file:

<? php

function example () {

// list of operations in the function

}

?>

In this example, using the function command, the creation of a function named example is declared, which will later be used to process the entered form data. After that, you need to display the HTML form, through which the PHP functions will be called:

In this case, a form is created that sends the code to the form handler using the POST method. To initialize the function in this example, a hidden text field is used, which conveys information for its further processing. To run the desired function, it will be necessary to establish whether the user has pressed a button. If the button was pressed, the previously described function will be triggered:

<?

if (isset ($ _ POST [‘function_start’]) == ‘go’) {

example (); }

?>

This code checks for the presence of the data transferred from the form in the script using the isset () function. If there is data entered in the hidden form, the execution of the previously declared function begins.

GET

The transfer of information by the GET method can occur without using the form data through the entered address. Similarly, at the beginning of the script, the required function is declared using the function statement. After that, to transfer information, for example, you can create an HTML link of the form instead of:

Passing GET

In this case, the test element is added to the address with a fixed value of 1, which is required to initialize the function. The test metric will be stored in the global $ _GET array.

After the user clicks on the link, the script will need to analyze the available data. If there is an element test in the $ _GET array, the function will be called. Processing can be done as follows:

if (isset ($ _ GET [‘test’])) {

example ($ _ GET [‘example’]); }

This code checks for the presence of an array element in the address bar. After that, the script initializes the previously declared example function to process the data and then run the program.

Recommended: