13

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:

<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 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.

1
  • I'm very glad that you have an experience and you know how to solve tasks like this. First of all I've tried to find it via Google and on StackOverflow but I've not got enough result. Second: please, can you show me where was this "stupid" question asked? Good luck! Commented May 12, 2011 at 10:39

7 Answers 7

28

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:

<?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>

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 <?php ... ?> 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 <h1>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.
5

Try This.

  <?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

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.

7
  • Thanx, but I don't know how it works when variables aren't assigned on page load, so when I press button nothing happens Commented May 11, 2011 at 17:41
  • You need to assign an action to your form as well. Put the url to your page in the action attribute.
    – Dave Kiss
    Commented May 11, 2011 at 17:46
  • Oh, thank you again) I've changed $POST to $_POST and it almost works! But when page loads I have a label with text 0, how can I fix it? Thanx. Commented May 11, 2011 at 17:47
  • add the if statement above that only runs the function if the page is posted to.
    – Dave Kiss
    Commented May 11, 2011 at 17:48
  • @Dave Kiss, I know that I should assign something to action, but I don't know what could be there, maybe the same file, where I write it? Commented May 11, 2011 at 17:48
1

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']);
}
1
  • Thanx a lot, I got a lot of wholesome advices. Commented May 11, 2011 at 18:07
1

You are missing the underscores in

$_POST['number1']

That's all.

0
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 :

...
<form action="Controllers/set_data.php?post=login" method="post" >
...

In the php :

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

  switch ($post_select) {
    case 'setup':
      set_data_setup();
      break;
...
0

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.

1
  • Welcome to Stackoverflow. You answered a question from 10 years ago that already had a well-written solution. I also don't see how this answer is related to the author's problem. Maybe you tried to reply to another answer in a comment or something? Commented Oct 3, 2021 at 3:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.