How to run a PHP function from an HTML form?

I am an absolute newbie in web technology. I know that my question is very simple, but I do not know how to do it. For example, I have a function:

function addNumbers($firstNumber, $secondNumber) { echo $firstNumber + $secondNumber; } 

And I have a form:

 <form action="" method="post"> <p>1-st number: <input type="text" name="number1" /></p> <p>2-nd number: <input type="text" name="number2" /></p> <p><input type="submit"/></p> 

How can I enter variables into my text fields and call my function by pressing a button with the arguments I wrote to the text fields? For example, I write 5 - the first text field, 10 - the second text field, then I press the button, and I get the result 15 on the same page. EDITED I tried to do it like this:

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

But this does not work, always the answer is 0.

+7
source share
5 answers

You have a β€œfunction” on the server side . Server code is executed before and only before data is returned to your browser (usually displayed as pages, but can also be an ajax request ).

Your form is on the client side . This form is displayed by your browser and is not "connected" to your server, but can send data to the server for processing .

Therefore, in order to start a function, the following thread must occur:

  1. The server displays a page with a form. Server-side processing is not required.
  2. The browser loads this page and displays the form.
  3. User enters data in the form
  4. The user clicks the submit button, an HTTP request with data is sent to your server.
  5. The page processing the request (may coincide with the first request) receives data from the request, runs your function and displays the result on the HTML page.

Here is an example PHP script that does all this:

 <?php function addNumbers($firstNumber, $secondNumber) { return $firstNumber + $secondNumber; } if (isset($_POST['number1']) && isset($_POST['number2'])) { $result = addNumbers(intval($_POST['number1']), intval($_POST['number2'])); } ?> <html> <body> <?php if (isset($result)) { ?> <h1> Result: <?php echo $result ?></h1> <?php } ?> <form action="" method="post"> <p>1-st number: <input type="text" name="number1" /></p> <p>2-nd number: <input type="text" name="number2" /></p> <p><input type="submit"/></p> </body> </html> 

Note:

  • Even if this β€œpage” contains both PHP and HTML code, your browser never knows what PHP code is. All he sees is the result of HTML. Everything inside <?php ... ?> executed by the server (in which case echo creates the only output from this execution), while everything that is outside the PHP & tags - in particular, the HTML & code - is directly output to HTTP answer.
  • You will notice that the HTML code <h1>Result:... is inside the PHP if . This means that this line will not be displayed on the first pass, because there is no $result .
  • Since the action form does not matter, it is sent to the same page (URL) on which the browser is already running.
+18
source

You need to collect the values ​​from the $_POST variable and pass them to the function.

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

Please note that you should not trust user input and therefore should check and deactivate your input.

+4
source

Try it.

  <?php function addNumbers($firstNumber, $secondNumber) { if (isset($_POST['number1']) && isset($_POST['number2'])) { $firstNumber = $_POST['number1']; $secondNumber = $_POST['number2']; $result = $firstNumber + $secondNumber; echo $result; } } ?> <form action="urphpfilename.php" method="post"> <p>1-st number: <input type="text" name="number1" /></p> <p>2-nd number: <input type="text" name="number2" /></p> <?php addNumbers($firstNumber, $secondNumber);?> <p><?php echo $result; ?></p> <p><input type="submit"/></p> 
+4
source

The variables will be in the $ _POST variable.

To parse it into a function, you need to do this:

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

Make sure that you check the input, users can add everything they need to it. For example, use the is_numeric () function

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

Also, there is no echo inside the 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']); } 
+1
source

You are missing the underscore in

 $_POST['number1'] 

What all.

+1
source

All Articles