简体   繁体   English

PHP无法正常工作; 打印出代码

[英]PHP not working; printing out the code instead

I want to print out the username, password, email, firstname, and lastname in html and php. 我想在html和php中打印出用户名,密码,电子邮件,名字和姓氏。 However, it is not working, and instead of running the code, it's just printing out the code code. 但是,它不起作用,并且只运行代码,而不是运行代码。

Here's the code for the SignUp.html : 这是SignUp.html的代码:

<!DOCTYPE html>
<html>
  <body>
        <script src="index.js"></script>
        <h1 class="hg">Get Started</h1>

        <form action="SignUpPhpTest.php" method="get" class="signup-form" >
            FirstName: <input type="firstname" name="firstname">

            Lastname: <input type="lastname" name="lastname">

            Email: <input type="text" name="mail" id="mail">

            Username: <input type="text" name="Username" id="Username">

            <label>
                Password: <input type="password" name="password" id="password" type="password" onkeyup='check();'>
            </label>

            <label>
                Confirm Password: <input type="password" name="confirm_password" id="confirm_password"  onkeyup='check();'> 
                <span id='message'></span>
            </label>

            <input type="submit" action="SignUp.php" value="submit">
        </form>

        <h2 class="GenderHeading">Gender</h2>
        <form class="Gender">
            <input type="radio" name="gender" value="male">Male

            <input type="radio" name="gender" value="Female">Female

            <input type="submit" value="submit">
        </form>
    </body>

    <link href="index.css" rel="stylesheet" type="text/css" />
</html>

Please note that this is saved as a .html file. 请注意,此文件另存为.html文件。 And here is the code for the SignUpTest.php file: 这是SignUpTest.php文件的代码:

<!DOCTYPE html>
<html>
    <head>
        <p>Info Gathered</p>
    </head>
    <body>
        <table>
            <tr>
                <td>
                    <?php 
                        $firstname = $_POST['fistname']; 
                        $lastname = $_POST['lastname']; 
                        $email = $_POST['mail']; 
                        $username = $_POST['username'];
                        $password = $_POST['password']; 

                        echo $firstname; 
                    ?>
                </td>
                <td>
                    <?php echo $lastname; ?>
                </td>
                <td>
                    <?php echo $email; ?>
                </td>
                <td>
                    <?php echo $username; ?>
                </td>
                <td>
                    <?php echo $password; ?>
                </td>
            </tr>
        </table>
    </body>
</html>

Please not that this one is saved as a .php file. 请不要将此文件另存为.php文件。 All of these are in Notepad. 所有这些都在记事本中。 I have Windows 10. 我有Windows 10。

Any help is appreciated. 任何帮助表示赞赏。 Thanks!!! 谢谢!!!

Your form method must be method="POST" not method="get" and please remove this <form class="Gender"> you can't use 2 forms at the same page so maybe change that into a <div class="Gender"> instead and move your closing </form> at the bottom. 您的表单方法必须是method="POST"而不是method="get" ,请删除此<form class="Gender"> 您不能在同一页面上使用2个表单,因此请将其更改为<div class="Gender">改为使用<div class="Gender">然后在底部移动</form>

<form action="SignUpPhpTest.php" method="get" class="signup-form" >

to

<form action="SignUpPhpTest.php" method="POST" class="signup-form" >

as you are using $_POST['datas'] on your SignUpTest.php 因为您在SignUpTest.php上使用$_POST['datas'] SignUpTest.php $_POST['datas']

Note*: make sure that your Apache is running to make sure PHP is enabled. 注意*:请确保您的Apache正在运行以确保已启用PHP

About the code is printed out you should verify that it is running apache and that PHP is properly configured. 关于代码已打印出来,您应该验证它是否正在运行apache并正确配置了PHP。 If you are on windows you could try xampp for easy and quickly testing. 如果您使用的是Windows,则可以尝试使用xampp进行简单快速的测试。

In the example you share there are many errors, for names some: 2 forms, form is sending as get in test script you expect data in post, some inputs name are misspelled, etc. 在您共享的示例中,存在很多错误,其中包括一些名称:2个表单,表单以测试脚本的形式发送,您期望发布的数据,某些输入名称拼写错误,等等。

Please try this simplified example 请尝试这个简化的例子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="test.php" method="POST" class="signup-form" >
        <p>
            FirstName: <input name="firstname">
        </p>

        <p>
            Lastname: <input name="lastname">
        </p>

        <p>
            Email: <input name="mail">
        </p>

        <p>
            Username: <input name="username">
        </p>

        <p>
            Password: <input type="password" name="password">
        </p>

        <p>
            Confirm Password: <input type="password" name="confirm_password"> 
        </p>

        <input type="submit" value="Submit">
    </form>
</body>
</html>

test.php file test.php文件

<?php
    $firstname = $_POST['firstname']; 
    $lastname  = $_POST['lastname']; 
    $email     = $_POST['mail']; 
    $username  = $_POST['username'];
    $password  = $_POST['password']; 
?>

<!DOCTYPE html>
<html>
    <head>
        <p>Info Gathered</p>
    </head>
    <body>
        <table>
            <tbody>
                <tr>
                    <td>First name</td>
                    <td><?= $firstname ?></td>
                </tr>
                <tr>
                    <td>Lastname</td>
                    <td><?= $lastname; ?></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><?= $email; ?></td>
                </tr>
                <tr>
                    <td>Username</td>
                    <td><?= $username; ?></td>
                </tr>
                <tr>
                    <td>You should not do this in real apps</td>
                    <td><?= $password; ?></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

It's because you are putting < ?php 这是因为您将<?php

?> ?>

After everything. 之后的一切。 Simply put < ?php at the start of all of your php, and ?> at the bottom. 只需将<?php放在所有PHP的开头,然后将?>放在底部。

Maybe PHP is not enabled, check the folder mods-enabled in the Apache directory (default: /etc/apache2/) and please look if you can see a file named php . 也许未启用PHP ,请检查Apache目录中的mods-enabled文件夹(默认值:/ etc / apache2 /),然后查看是否可以看到名为php的文件。 The extension should be .so . 扩展名应为.so

In var/log/apache2/error.log you can see all the errors. var/log/apache2/error.log您可以看到所有错误。

This can happen if Apache is not configured correctly to load PHP. 如果未正确配置Apache来加载PHP,则会发生这种情况。 You should be able to find instructions for installing the PHP Apache 2 handler for your OS and package manager if you don't already have it installed. 如果尚未安装用于操作系统和程序包管理器的PHP Apache 2处理程序,则应该能够找到说明。 Once you do that the important parts of your httpd.conf include the line to load the module itself. 完成后,httpd.conf的重要部分将包含用于加载模块本身的行。 eg. 例如。

LoadModule php7_module modules/mod_php71.so

.. and the section to tell apache to use this to handle files with the .php extension (usually a separate include file), eg. ..和告诉apache使用此部分来处理扩展名为.php (通常是单独的包含文件)的部分。

<IfModule mod_php7.c>

AddType  application/x-httpd-php         .php
AddType  application/x-httpd-php-source  .phps

</IfModule>

Once you have the correct configuration in place, restart apache and then your code should execute. 正确配置后,请重新启动apache,然后应执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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