doutian3269
2016-02-12 21:58
浏览 142
已采纳

PHP代码不执行(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.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douchenhui5569 2016-02-12 22:58
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzhuxun5136 2016-02-12 22:49

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题