dongweng9474 2018-06-14 12:07
浏览 51
已采纳

如何从外部php文件访问网页

I'm new to PHP and I'm trying to create a simple register page. I made two separate files for HTML and PHP. So in post method I include this second file like this: <form action="register.php" method="post">. The problem is after submitting registration I want to check all this columns and if are wrong, put some information about it. But everything opens in a new blank page. I want to maintain PHP and HTML in separate files. Index.php:

<!DOCTYPE html>

<html lang="pl">
<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">
<link rel = "stylesheet" type = "text/css" href = "main.css">
<link href="https://fonts.googleapis.com/css?family=Lato:400,700"       
rel="stylesheet">
<link rel="stylesheet"   
href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" 
integrity="sha384- 
DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" 
crossorigin="anonymous">
<title> Rejestracja</title>
</head>

<body>
<div id = "imover">
    <nav><div id = "navcontainer"><ul>
        <li><a href = "#">Home</a></li>
        <li><a href = "#">About us</a></li>
        <li><a href = "#">Our projects</a></li>
        <li><a href = "#">Contact</a></li>
        </ul></div></nav>
    <div id = "socialscontainer"><div id = "fb"><a href = "#"><i class="fab 
fa-facebook-square"></i></a></div><div id = "tw"><a href = "#"><i class="fab 
fa-twitter-square"></i></a>
</div><div style = "clear: both;"></div>

    </div>
    <header><div id = "logocontainer"><h1>Register</h1></div></header>
    <div id = "container">
        <form action="register.php"  method="post">
            <h2>Username</h2><input type = "text" name = "username">
            <h2>Password</h2><input type = "password" name = "pass1">
            <h2>Confirm password</h2><input type = "password" name = "pass2">
            <h2>E-Mail</h2><input type = "text" name = "email">
            <br/><br/>
            <input type = "submit" id ="btn" value="Register now!">
        </form>
    </div>
            <span><a href = "logowanie.php">Do you have an account? Log in 
 now!</a></span>
    </div>

</body>
</html>

And there's register.php:

<?php
session_start();
$username = $_POST['username'];
$password = $_POST['pass1'];
if($_POST['pass2'] !=$password) {header("Location: mistake.php"); exit();}
$hashed_password = password_hash($password,PASSWORD_DEFAULT);
$email = $_POST['email'];

echo $username;
?>
  • 写回答

1条回答 默认 最新

  • dtvq4978 2018-06-14 13:18
    关注

    If u want to stay on the same website u need to delete the 'action'-attribute in the form tag and to use your php file, add
    <?php include ('register.php') ?>
    at the top of your html.

    Maybe u will need to change the '.html' to a '.php' but thats not a problem, i hope.

    Plus u may need to add sth like this:

      if ( isset($_POST['username'])){      
            $username = $_POST['username'];
            $password = $_POST['pass1'];
            if($_POST['pass2'] !=$password) {header("Location: mistake.php"); exit();}
            $hashed_password = password_hash($password,PASSWORD_DEFAULT);
            $email = $_POST['email'];
    
            echo $username;
        }
    

    Otherwise it could crash because there are no values in $_POST then u enter the website the first time

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行