简体   繁体   中英

PHP not working; printing out the code instead

I want to print out the username, password, email, firstname, and lastname in html and 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 :

<!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. And here is the code for the SignUpTest.php file:

<!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. All of these are in Notepad. I have 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.

<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

Note*: make sure that your Apache is running to make sure PHP is enabled.

About the code is printed out you should verify that it is running apache and that PHP is properly configured. If you are on windows you could try xampp for easy and quickly testing.

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.

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

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

?>

After everything. Simply put < ?php at the start of all of your php, and ?> at the bottom.

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 . The extension should be .so .

In var/log/apache2/error.log you can see all the errors.

This can happen if Apache is not configured correctly to load 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. Once you do that the important parts of your httpd.conf include the line to load the module itself. 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.

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

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