简体   繁体   中英

How do I include array from another PHP file

In the handle_signup.php page, I try to handle with the data which was sent from the sign up page. If the nickname blank has no value, then the error message is "Please enter your nickname". So, in the signup page, I try to include the $errorMsgs array from handle_signup.php page and show the msg below the nickname input. How can I get the array from the handle_signup.php page? Currently, I use include_once, but there is no message in the array after transferring to handle_signup.php. Could some one help this issue? Thank you.

Handle the value from signup.php

<?php
  require_once('./conn.php');
  $errorMsgs = array('nickname'=>'', 'email'=>'', 'password'=>'');


  if(isset($_POST['submit'])) {
    if(empty($_POST['nickname'])) {
      $errorMsgs['nickname'] = "Please enter your nickname";
      header("Location: ./signup.php");
    }
  };
?>

Sign up page - signup.php

<?php
  include_once 'handle_signup.php';
  var_dump($errorMsgs);
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
  <title>Message Board - Sign Up</title>
</head>
<body>
  <div class="container_sign">
    <h1 class="title">Create Account</h1>
    <form class="sign" method="POST" action="./handle_signup.php">
      <div>
        <i class="far fa-user"></i>
        <input type="text" placeholder="Nickname" name="nickname">
      </div>
      <p class="warning__msg"></p>
      <div>
        <i class="far fa-envelope"></i>
        <input type="text" placeholder="Email" name="email">
      </div> 
      <p class="warning__msg"></p>
      <div>
        <i class="fas fa-lock"></i>
        <input type="password" placeholder="Password" name="password">
      </div>
      <p class="warning__msg"></p>
      <input type="submit" value="SIGN UP" name="submit">
    </form>
  </div>
</body>
</html>

正如 DarkBee 所建议的,您可以使用$_SESSION来显示错误消息,或者干脆不使用header("Location: ./signup.php")而是将所有 PHP 代码包含在同一页面中。

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