Jump to content

problem displaying results of submitted forms


TheAntipop

Recommended Posts

Hi all,

 

I am new to PHP and I am having an issue implementing a login script into a site I'm creating.  The script is not my own, it was posted on evolt.org here

 

The site I am coding will have a navigation bar on the left side, and a content window to the right.  On the main page (index.php), I am using a variable called $location to determine what page will load in the content pane. The links on the navigation bar specify the contents of $location. For example, the link "index.php?location=login" will set $location to "login" and, using a switch statement, my PHP script executes the appropriate code to display login.php in the content pane.

 

I am using this method to display the login script's input forms for users to login and register. I want the forms and the results (once submitted) to display in my content pane. The problem is, once I click Submit on a form, the results disappear into the ether, and index.php is reloaded. 

 

The reason this is happening is, when I click on a navigation link that specifies "location=", I have control over where the contents of the linked file are displayed (the content pane).  However, when I click Submit on the form, the form code is executed, but I don't have control over where the results are displayed.  I'm wondering if there's a way to tell the form where to display the results.   

 

I know that when I click the submit on a form, the input strings are handed off to process.php. From there I am a bit fuzzy about what happens, because frankly the programming is a bit above my level of understanding, especially with the instantiating of classes.

 

Here is the code for index.php:

 

<?php

include("include/session.php");

$location=$_GET['location'];
if (empty($location))
{ $location='index'; }

include("header.html");
echo '<br><br>';
echo '<table class="test" cellspacing="0" align="center">';
echo '<tr>';
echo '<td class="head" colspan="2" valign="top">';
echo 'Notre Dame Club of RI/SE Mass';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="nav" valign="top">';

include('navbar.html');
echo '</td>';
echo '<td class="content">';

switch ($location)
{
case 'index':
include ('actualindex.html');
break;

case 'sixcs':
include ('sixcs.html');
break;

case 'officers':
include ('officers.html');
break;

case 'login':
include ('login.html');
break;

case 'register':
include('register.php');
}

echo '</td></tr></table>';
include ('footer.html');
?>

 

Here is register.php:

 

<?php
/**
* Register.php
* 
* Displays the registration form if the user needs to sign-up,
* or lets the user know, if he's already logged in, that he
* can't register another name.
*
* Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
* Last Updated: August 19, 2004
*/
?>

<html>
<title>Registration Page</title>
<body>

<?php
/**
* The user is already logged in, not allowed to register.
*/
if($session->logged_in){
   echo "<h1>Registered</h1>";
   echo "<p>We're sorry <b>$session->username</b>, but you've already registered. "
       ."<a href=\"main.php\">Main</a>.</p>";
}
/**
* The user has submitted the registration form and the
* results have been processed.
*/
else if(isset($_SESSION['regsuccess'])){
   /* Registration was successful */
   if($_SESSION['regsuccess']){
      echo "<h1>Registered!</h1>";
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, "
          ."you may now <a href=\"main.php\">log in</a>.</p>";
   }
   /* Registration failed */
   else{
      echo "<h1>Registration Failed</h1>";
      echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, "
          ."could not be completed.<br>Please try again at a later time.</p>";
   }
   unset($_SESSION['regsuccess']);
   unset($_SESSION['reguname']);
}
/**
* The user has not filled out the registration form yet.
* Below is the page with the sign-up form, the names
* of the input fields are important and should not
* be changed.
*/
else{
?>

<h1>Register</h1>
<?
if($form->num_errors > 0){
   echo "<td><font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font></td>";
}
?>

<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" maxlength="50" value="<? echo $form->value("email"); ?>"></td><td><? echo $form->error("email"); ?></td></tr>
<tr><td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1">
<input type="submit" value="Join!"></td></tr>
<tr><td colspan="2" align="left"><a href="main.php">Back to Main</a></td></tr>
</table>
</form>
<?

}

?>

 

And here is process.php:

 

<?php
/**
* Process.php
* 
* The Process class is meant to simplify the task of processing
* user submitted forms, redirecting the user to the correct
* pages if errors are found, or if form is successful, either
* way. Also handles the logout procedure.
*
* Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
* Last Updated: August 19, 2004
*/
include("include/session.php");

class Process
{
   /* Class constructor */
   function Process(){
      global $session;
      /* User submitted login form */
      if(isset($_POST['sublogin'])){
         $this->procLogin();
      }
      /* User submitted registration form */
      else if(isset($_POST['subjoin'])){
         $this->procRegister();
      }
      /* User submitted forgot password form */
      else if(isset($_POST['subforgot'])){
         $this->procForgotPass();
      }
      /* User submitted edit account form */
      else if(isset($_POST['subedit'])){
         $this->procEditAccount();
      }
      /**
       * The only other reason user should be directed here
       * is if he wants to logout, which means user is
       * logged in currently.
       */
      else if($session->logged_in){
         $this->procLogout();
      }
      /**
       * Should not get here, which means user is viewing this page
       * by mistake and therefore is redirected.
       */
       else{
          header("Location: main.php");
       }
   }

   /**
    * procLogin - Processes the user submitted login form, if errors
    * are found, the user is redirected to correct the information,
    * if not, the user is effectively logged in to the system.
    */
   function procLogin(){
      global $session, $form;
      /* Login attempt */
      $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
      
      /* Login successful */
      if($retval){
         header("Location: ".$session->referrer);
      }
      /* Login failed */
      else{
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
   }
   
   /**
    * procLogout - Simply attempts to log the user out of the system
    * given that there is no logout form to process.
    */
   function procLogout(){
      global $session;
      $retval = $session->logout();
      header("Location: main.php");
   }
   
   /**
    * procRegister - Processes the user submitted registration form,
    * if errors are found, the user is redirected to correct the
    * information, if not, the user is effectively registered with
    * the system and an email is (optionally) sent to the newly
    * created user.
    */
   function procRegister(){
      global $session, $form;
      /* Convert username to all lowercase (by option) */
      if(ALL_LOWERCASE){
         $_POST['user'] = strtolower($_POST['user']);
      }
      /* Registration attempt */
      $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email']);
      
      /* Registration Successful */
      if($retval == 0){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = true;
         header("Location: ".$session->referrer);
      }
      /* Error found with form */
      else if($retval == 1){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
      /* Registration attempt failed */
      else if($retval == 2){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = false;
         header("Location: ".$session->referrer);
      }
   }
   
   /**
    * procForgotPass - Validates the given username then if
    * everything is fine, a new password is generated and
    * emailed to the address the user gave on sign up.
    */
   function procForgotPass(){
      global $database, $session, $mailer, $form;
      /* Username error checking */
      $subuser = $_POST['user'];
      $field = "user";  //Use field name for username
      if(!$subuser || strlen($subuser = trim($subuser)) == 0){
         $form->setError($field, "* Username not entered<br>");
      }
      else{
         /* Make sure username is in database */
         $subuser = stripslashes($subuser);
         if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
            !eregi("^([0-9a-z])+$", $subuser) ||
            (!$database->usernameTaken($subuser))){
            $form->setError($field, "* Username does not exist<br>");
         }
      }
      
      /* Errors exist, have user correct them */
      if($form->num_errors > 0){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
      }
      /* Generate new password and email it to user */
      else{
         /* Generate new password */
         $newpass = $session->generateRandStr(;
         
         /* Get email of user */
         $usrinf = $database->getUserInfo($subuser);
         $email  = $usrinf['email'];
         
         /* Attempt to send the email with new password */
         if($mailer->sendNewPass($subuser,$email,$newpass)){
            /* Email sent, update database */
            $database->updateUserField($subuser, "password", md5($newpass));
            $_SESSION['forgotpass'] = true;
         }
         /* Email failure, do not change password */
         else{
            $_SESSION['forgotpass'] = false;
         }
      }
      
      header("Location: ".$session->referrer);
   }
   
   /**
    * procEditAccount - Attempts to edit the user's account
    * information, including the password, which must be verified
    * before a change is made.
    */
   function procEditAccount(){
      global $session, $form;
      /* Account edit attempt */
      $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']);

      /* Account edit successful */
      if($retval){
         $_SESSION['useredit'] = true;
         header("Location: ".$session->referrer);
      }
      /* Error found with form */
      else{
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
   }
};

/* Initialize process */
$process = new Process;

?>

 

For example,  if I click on register in my navigation bar, it loads register.php into the content pane.  It displays the forms to input desired username password and email.  When I do this and click submit on the form, the forms disappear and index.php is reloaded.  I am trying to get the form results to display in the content pane.

 

Any help will be appreciated.

I've uploaded my site-in-progress to show you the problem in action.  If you click on Login / Register it will display the forms in the content pane.  However, as soon as you click on a submit button (Login or Join) it reloads index.php and I lose the results of the form submission.  I know the form is going through, because if I register a username and password it is added to my MySQL databse.  I just can't see the results on the web page (which process.php is supposed to handle).

 

Like I said, whenever I click a link on the navigation bar I am specifying "location=" so the $location variable gets a value.  But when I submit a form, I have no way of specifying where it will show up (that I know of), which is what is giving me the problem. 

 

Anyway, here's the site.  Any suggestions are welcome...

 

http://mattmasi.com/test/index.php

I would be able to help you better if I were on my computer but at the moment I am on my iPod touch. It sounds like the problem lies in the output of your process.php script. Are you trying to display the data on the process.php file or the index.php file

 

I would be able to help you better if I were on my computer but at the moment I am on my iPod touch. It sounds like the problem lies in the output of your process.php script. Are you trying to display the data on the process.php file or the index.php file

 

Thanks for the reply.  I'm trying to display the results on index.php.  I did not write any of the code in process.php, it is part of the open source login script I am using.  I know that file is not the problem though...

 

For example, if I include the registration form on the home page without using the $location variable (outside of the switch statement), it works.  But I don't want it on the home page, I want to load it by itself on the content pane.

 

Here's what it looks like if I include the register form on the home page without using the $location variable or switch statement:

 

http://mattmasi.com/test/index2.php

 

If you fill out the forms and click join it will tell you it registered successfully (or display errors if the formatting is wrong).  This tells me that it has something to do with the $location variable and the way I am using it with the forms.

The problem is the way the process.php is written:

 

<?php
      /* Login successful */
      if($retval){
         header("Location: ".$session->referrer);
      }
?>

 

Specifically the header() line. You either need to make a new file (logged_in.php) to display when the user is logged in, or use another variable (logged=true) in the URL to display content that only users logged in should see.. basically the way it is now, after a successful login it will go back to the referrer page.. which is index.php and it will not have any url variables, so it will display the home page.

 

get it?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.