How to call php function in html

I'm absolute beginner in web technologies. I know that my question is very simple, but I don't know how to do it. For example I have a function:

function addNumbers[$firstNumber, $secondNumber]
{
    echo $firstNumber + $secondNumber;
}

And I have a form:


1-st number:

2-nd number:

How can I input variables on my text fields and call my function by button pressing with arguments that I've wrote into text fields? For example I write 5 - first textfield, 10 - second textfield, then I click button and I get the result 15 on the same page. EDITED I've tried to do it so:

$num1 = $POST['number1'];
$num2 = $POST['number2'];
addNumbers[$num1, $num2];

But it doesn't work, the answer is 0 always.

Nicole

32.2k11 gold badges73 silver badges99 bronze badges

asked May 11, 2011 at 17:36

Frankie DrakeFrankie Drake

1,2399 gold badges24 silver badges37 bronze badges

2

The "function" you have is server-side. Server-side code runs before and only before data is returned to your browser [typically, displayed as a page, but also could be an ajax request].

The form you have is client-side. This form is rendered by your browser and is not "connected" to your server, but can submit data to the server for processing.

Therefore, to run the function, the following flow has to happen:

  1. Server outputs the page with the form. No server-side processing needs to happen.
  2. Browser loads that page and displays the form.
  3. User types data into the form.
  4. User presses submit button, an HTTP request is made to your server with the data.
  5. The page handling the request [could be the same as the first request] takes the data from the request, runs your function, and outputs the result into an HTML page.

Here is a sample PHP script which does all of this:





    
        

Result:

1-st number:

2-nd number:

Please note:

  • Even though this "page" contains both PHP and HTML code, your browser never knows what the PHP code was. All it sees is the HTML output that resulted. Everything inside is executed by the server [and in this case, echo creates the only output from this execution], while everything outside the PHP tags — specifically, the HTML code — is output to the HTTP Response directly.
  • You'll notice that the

    Result:... HTML code is inside a PHP if statement. This means that this line will not be output on the first pass, because there is no $result.

  • Because the form action has no value, the form submits to the same page [URL] that the browser is already on.

answered May 11, 2011 at 17:43

You need to gather the values from the $_POST variable and pass them into the function.

if [$_POST] {
  $number_1 = [int] $_POST['number1'];
  $number_2 = [int] $_POST['number2'];
  echo addNumbers[$number_1, $number_2];
}

Be advised, however, that you shouldn't trust user input and thus need to validate and sanitize your input.

answered May 11, 2011 at 17:38

Dave KissDave Kiss

10.1k11 gold badges51 silver badges75 bronze badges

7

Try This.

  

            
            

1-st number:

2-nd number:

answered May 11, 2011 at 17:48

Vishwanath DalviVishwanath Dalvi

34.3k41 gold badges122 silver badges152 bronze badges

The variables will be in the $_POST variable.

To parse it to the function you need to do this:

addNumbers[$_POST['number1'],$_POST['number2']];

Be sure you check the input, users can add whatever they want in it. For example use is_numeric[] function

$number1 = is_numeric[$_POST['number1']] ? $_POST['number1'] : 0;

Also, don't echo inside a function, better return it:

function addNumbers[$firstNumber, $secondNumber]
{
    return $firstNumber + $secondNumber;
}

// check if $_POST is set
if [isset[$_POST['number1']] && isset[$_POST['number2']]]
{
    $number1 = is_numeric[$_POST['number1']] ? $_POST['number1'] : 0;
    $number2 = is_numeric[$_POST['number2']] ? $_POST['number2'] : 0;

    echo addNumbers[$_POST['number1'],$_POST['number2']];
}

answered May 11, 2011 at 17:41

Rene PotRene Pot

24.2k7 gold badges67 silver badges90 bronze badges

1

You are missing the underscores in

$_POST['number1']

That's all.

answered May 11, 2011 at 17:44

CupsCups

6,8113 gold badges26 silver badges29 bronze badges

0

maybe it's a little late but could you set a parameter in the url of the php file to post example:

In the html :

...

...

In the php :

...
$post_select = $_GET['post'];

  switch [$post_select] {
    case 'setup':
      set_data_setup[];
      break;
...

answered Oct 9, 2020 at 0:05

You can always use this trick. But keep in mind that if the referrer is hidden it doesn't work.

header["Location: " . $_SERVER["HTTP_REFERER"]];

Just add to your PHP page at the point where there is no more code to be executed, but is still executed.

bad_coder

9,33619 gold badges37 silver badges61 bronze badges

answered Oct 2, 2021 at 22:53

1

How do you call a PHP function?

There are two methods for doing this. One is directly calling function by variable name using bracket and parameters and the other is by using call_user_func[] Function but in both method variable name is to be used. call_user_func[ $var ];

How do you call a function button in HTML?

To invoke this function in the html document, we have to create a simple button and using the title event attribute [which is an event handler] along with it, we can call the function by clicking on the button.

How do I call a PHP function when a Link is clicked?

This method calls the PHP function if the data is set and executes the function..
Use jQuery to Execute the PHP Function With the title[] Event..
Use Plain JavaScript to Execute the PHP Function With the title[] Event..
Use the GET Method and the isset[] Function to Execute a PHP Function From a Link..

How do I call a PHP function from another file?

To call a function from another file in PHP, you need to import the file where the function is defined before calling it. You can import a PHP file by using the require statement. To call the greetings[] function from another file, you need to import the library.

Chủ Đề