简体   繁体   中英

PHP Code Doesn't Execute (new to php)

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM