-1

I have two php pages. index.php and functions.php. In index.php , I call login and change password functions from functions.php. My problem is , when i click login, my login form is getting displayed along changepassword form.I want only changepassword form to get displayed when i click on login. Please help!!

here is my code: 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>

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


     }

   ?>
1
  • There is no need to add the return there, since your function is echo'ing the HTML.
    – Jules
    Jan 24, 2013 at 9:28

5 Answers 5

1

I think your code should be like this

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

And your functions.php page

<?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>';
     }

?>

Remember in your code you dint have forgotpassword function declared so I just copied the changepassword function and renamed it to forgotpassword function to give you an example

1
  • Yeah.I forgot to post forgotpassword function. Thanks for the reply. But it dint work !!
    – UKR
    Jan 24, 2013 at 10:20
0

You need to remove <form> and </form> tags from index.php

Remove below two lines from index.php

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

and

</form
0
0

use switch to check for action passed and display form accordingly.

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;
}
0

Make change in code :

<?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();
 }

 ?>

To :

 <?php

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

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

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

change this as

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.