1

Okay so I am total noob in php since I recently started watching videos on php. So what I am trying to do is I have this html file:

<!DOCTYPE html>
<html>
    <head>
        <title>Testing PHP</title>
    </head>

    <body>
        <form action="action.php" method="POST">

            <input type="text" name="first">
            <input type="text" name="second">
            <input type="submit" value="Submitto!">
        </form>
    </body>
</html>

and then I have my action.php file which has this code:

<!DOCTYPE html>
<html>
    <head>
        <title>Testing PHP</title>
    </head>

    <body>

        <?php
            $first = $_POST['first'];
            $second = $_POST['second'];

            echo $first + $second;
        ?>

    </body>
</html>

However when I click on the submit button in my html file it sends me to a blank page. By the url I see that it has sent me to the right file but seems like the code doesn't execute.

PS: I tried searching on google but what I found was mostly stuff about apache not executing php which doesn't work for me since I try to run the files locally on my machine.

2
  • 3
    What are you using to run PHP locally? It will not run in the client like JavaScript. You need to setup a local server to run PHP locally on your machine. Commented Feb 12, 2016 at 22:01
  • 1
    I think this IS about Apache not executing PHP. Try right-clicking the blank page and viewing the page source. If you see your PHP code, read some of the stuff you found. Commented Feb 12, 2016 at 22:34

2 Answers 2

4

For Blank Page Problem: First you have to install a local server on your machine like apache then you have to request this page from the local server to run PHP locally on your machine.

Here is the download page.

For String concatenation:
In php string concatenation operator is . sign not the + sign,so you have to use . operator instead of + operator.

echo $first . $second;

action.php

<!DOCTYPE html>
<html>
    <head>
        <title>Testing PHP</title>
    </head>

    <body>

        <?php
            $first = $_POST['first'];
            $second = $_POST['second'];

            echo $first . $second;
        ?>

    </body>
</html>

For more knowledge about String Operators read this

Sign up to request clarification or add additional context in comments.

2 Comments

String concatenation is not an issue, it can not result in empty page. And if he is trying to add two numbers, then "+" will work, while "." will produce wrong result.
Yes string concatenation is not the issue here but i thought he is trying to add two string in his code that's why i give him a hint of string concatenation.
0

Your code works (I just actually checked it), but you still need a web server, even if you run your code locally.

Recent versions of php include the built-in webserver, so first try this (in the folder where your html and php files are placed):

php -S localhost:8888

Then open http://localhost:8888/your.html in the browser.

If your php is too old (older than 5.4.0), you will need to setup apache or ngnix to be able to run you php code.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.