-1

我有两个 php 页面。index.php 和functions.php。在 index.php 中,我从 functions.php 调用登录和更改密码函数。我的问题是,当我单击登录时,我的登录表单将与更改密码表单一起显示。我只想在单击登录时显示更改密码表单。请帮忙!!

这是我的代码:index.php

<?php session_start(); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Login to the site</title>
</head>
<body>

<form action="index.php?act=login" method="post" id= "user_login" >
<center>
<table>
            <tr>
                    <td>User Login</td>
                    <td><a href="index.php?act=login" >Click Here</a></td>
            </tr>
            <tr>
                    <td>Forgot Password?</td>
                    <td><a href="index.php?act=forgotpassword">Click Here</a></td>
            </tr>
     </center>
     <table>

     </form>

     <?php
     include "functions.php";
     $action_type = $_GET["act"];
     $form_name = $_GET["name"];
     if ($action_type == "login")
     { 
      return login();
     }
     elseif ($action_type == "forgotpassword")
     {
     return forgotpassword();
     }
     elseif ($action_type == "changepassword")
     {
     return changepassword();
     }

     ?>

     </body>
     </html>

函数.php:

function login()
 {
     echo' <script type="text/javascript">
function showhide() {
  var form = document.getElementById("login");
    if (form.style.visibility == "visible") {
form.style.visibility = "hidden";
}

}
   </script>
    <form action="index.php?act=login" method="post" id="login" >
    <center>
    <table cellpadding="2" cellspacing="2" border="0">
            <tr>
        <td width="30%" align="right">Username</td>
        <td width="70%"><input type="text" name="user" id="text_user"/></td>
            </tr>
            <tr>
                    <td align="right">Password</td>
                    <td><input type="password" name="pass" id="text_pass"  /></td>
            </tr>
            <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" value="login" name="submit"   onsubmit="showhide()" /></td>
            </tr>
    </center>
            </table>
       </form> </body></html>';

// login code goes here                


   }

   function changepassword()
   {
    echo'<form action ="index.php?act=changepassword" method="post" >
    <table cellpadding="2" cellspacing="2" border="0">
            <tr>
                    <td>Existing Password</td>
                    <td><input type="password" value="" name="pass" id="text_pass" /></td>
            </tr>
            <tr>
                    <td>New Password</td>
                    <td><input type="password" value="" name="cpass" id="text_cpass"/></td>
            </tr>
            <tr>
                    <td>Retype Password</td>
                    <td><input type="password" value="" name="crepass" id="text_crepass" /></td>
            </tr>
            <tr>
                    <td><input type="submit" value="change" name="submit" /></td>
            </tr>
    </table>
            <tr align = "left"> <td><br><a href="index.php?act=logout">LogOut</a></br> </td></tr>


     </form>';

     // changepassword code goes here


     }

   ?>
4

5 回答 5

1

我认为你的代码应该是这样的

测试.php

<?php session_start(); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Login to the site</title>
</head>
<body>

<form action="test.php?act=login" method="post" id= "user_login" >
<center>
<table>
            <tr>
                    <td>User Login</td>
                    <td><a href="test.php?act=login" >Click Here</a></td>
            </tr>
            <tr>
                    <td>Forgot Password?</td>
                    <td><a href="test.php?act=forgotpassword">Click Here</a></td>
            </tr>
     </center>
     <table>

     </form>

     <?php
     include "functions.php";
     if(isset( $_GET["act"])) {
        $action_type = $_GET["act"];
        if ($action_type == "login")
     { 
      return login();
     }
     elseif ($action_type == "forgotpassword")
     {
     return forgotpassword();
     }
     elseif ($action_type == "changepassword")
     {
     return changepassword();
     }  
     }
     elseif(isset($_GET["name"])) {
        $form_name = $_GET["name"]; 
     }



     ?>

     </body>
     </html>

还有你的functions.php页面

<?php
function login()
 {
     echo' <script type="text/javascript">
function showhide() {
  var form = document.getElementById("login");
    if (form.style.visibility == "visible") {
form.style.visibility = "hidden";
}

}
   </script>
    <form action="index.php?act=login" method="post" id="login" >
    <center>
    <table cellpadding="2" cellspacing="2" border="0">
            <tr>
        <td width="30%" align="right">Username</td>
        <td width="70%"><input type="text" name="user" id="text_user"/></td>
            </tr>
            <tr>
                    <td align="right">Password</td>
                    <td><input type="password" name="pass" id="text_pass"  /></td>
            </tr>
            <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" value="login" name="submit"   onsubmit="showhide()" /></td>
            </tr>
    </center>
            </table>
       </form> </body></html>';

// login code goes here                


   }

   function changepassword()
   {
    echo'<form action ="index.php?act=changepassword" method="post" >
    <table cellpadding="2" cellspacing="2" border="0">
            <tr>
                    <td>Existing Password</td>
                    <td><input type="password" value="" name="pass" id="text_pass" /></td>
            </tr>
            <tr>
                    <td>New Password</td>
                    <td><input type="password" value="" name="cpass" id="text_cpass"/></td>
            </tr>
            <tr>
                    <td>Retype Password</td>
                    <td><input type="password" value="" name="crepass" id="text_crepass" /></td>
            </tr>
            <tr>
                    <td><input type="submit" value="change" name="submit" /></td>
            </tr>
    </table>
            <tr align = "left"> <td><br><a href="index.php?act=logout">LogOut</a></br> </td></tr>


     </form>';
     }

      function forgotpassword()
   {
    echo'<form action ="index.php?act=changepassword" method="post" >
    <table cellpadding="2" cellspacing="2" border="0">
            <tr>
                    <td>Existing Password</td>
                    <td><input type="password" value="" name="pass" id="text_pass" /></td>
            </tr>
            <tr>
                    <td>New Password</td>
                    <td><input type="password" value="" name="cpass" id="text_cpass"/></td>
            </tr>
            <tr>
                    <td>Retype Password</td>
                    <td><input type="password" value="" name="crepass" id="text_crepass" /></td>
            </tr>
            <tr>
                    <td><input type="submit" value="change" name="submit" /></td>
            </tr>
    </table>
            <tr align = "left"> <td><br><a href="index.php?act=logout">LogOut</a></br> </td></tr>


     </form>';
     }

?>

请记住,在您的代码中,您已经声明了forgotpassword 函数,所以我只是复制了changepassword 函数并将其重命名为forgotpassword 函数,以便为您提供示例

于 2013-01-24T09:40:35.257 回答
0

您需要从 index.php 中删除<form></form>标记

从 index.php 中删除以下两行

<form action="index.php?act=login" method="post" id= "user_login" >

</form
于 2013-01-24T09:32:41.120 回答
0

用于switch检查通过的操作并相应地显示表单。

include "functions.php";
$action_type = $_GET["act"];
$form_name = $_GET["name"];

switch($action_type)
{ 
      case 'login':
          login();
          break;
      case 'forgotpassword' :
          forgotpassword();
          break;
      case 'changepassword' :
          changepassword();
          break;
      default :
          login();
          break;
}
于 2013-01-24T09:39:05.823 回答
0

更改代码:

<?php
 include "functions.php";
 $action_type = $_GET["act"];
 $form_name = $_GET["name"];
 if ($action_type == "login")
 { 
  return login();
 }
 elseif ($action_type == "forgotpassword")
 {
 return forgotpassword();
 }
 elseif ($action_type == "changepassword")
 {
 return changepassword();
 }

 ?>

至 :

 <?php

    include "function.php";
    $action = isset($_REQUEST['act'])?$_REQUEST['act'] : "";
    switch($action)
    {
        case 'login':
            login();
            break;

        case 'forgotpassword':
            changepassword();
            break;  
    }

 ?>
于 2013-01-24T09:40:22.873 回答
0
<form action="index.php?act=login" method="post" id= "user_login" >

将其更改为

<form action="index.php" method="post" id= "user_login" >
于 2013-01-24T11:23:28.623 回答