简体   繁体   中英

php and html form in single file

I wonder if there is a problem for a php file that contains php codes and html form. for example: in the file the first part would be:

<?php ...
?>

<html>...<form action = "current file"> .....  </form>
</html>

The action will refer to the name of this current file.

or do I have to separate the php code in a file with extension .php, and html code in a file with extension .html?

Test it on your own– You can. You can also use PHP inside of a HTML tag, because the PHP is loaded server-side whenever the client sends a request.

Client sends request --> Server gets request and loads up the .php file --> Server loads the php, executes the php, and replaces all php objects with text that it returns, or nothing --> Client gets the file that has been loaded (and edited) via the server

Note: If you combine PHP with HTML, the file needs the extension .php because HTML does not originally support PHP tags.

Here's an example for a php and html form in same file. Ofcourse the file extension needs to be .php

You just need to change ' action="current file" ' to ' action="" '

index.php

    <?php
    if(isset($_POST['submit'])) {
        // The things that needs to be checked and executed WHEN submitted.

        $email = htmlspecialchars($_POST['Email']);
        $email = strip_tags($email);

        $password = htmlspecialchars($_POST['Password']);
        $password = strip_tags($password);

        //SQL STUFF

        if ($email === $row['email'] && $password === $row['password']) {
            $_SESSION['uuid'] = $row['uuid'];
            header("Location: profile.php?id=". $row['uuid'] ."");
        }
    }
?>
<!DOCTYPE html>
<html>
<head>
    <title>My Form</title>
</head>
<body>
    <form action="" method="POST">
        <input type="email" name="Email" placeholder="Email..." required>
        <input type="password" name="Password" placeholder="Password..." required>
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
</html>

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