calling a function or page to another page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

calling a function or page to another page

Post by jarow »

I have two pages that are part of a user registration script. For simplicity's sake I will call one registration.php and the other validate.php

What I basically would like to do is instead of going from registration.php to validate.php, I would like to call the script from validate.php and use it without leaving registration.php

The following code is a portion of the validate.php code. Here are my questions regarding this page. 1). If I want to call it or include it in registration.php should I make it a function? If so, how would I do that.
2) Can I call the validate page or function through an a href statement? (see registration.php code)

Code: Select all

<?php
<?php  /* Account activation script */ 

// Get database connection 
include 'connlogin.php'; 

// Create variables from URL. 
$userid = $_REQUEST['id']; 
$code = $_REQUEST['code']; 
$usegrp = $_REQUEST['usegrp'];

$sql = mysql_query("UPDATE users SET activated='1' WHERE userid='$userid' AND password='$code' "); 


$sql_doublecheck = mysql_query("SELECT * FROM users WHERE userid='$userid' AND password='$code' AND activated='1'"); 
$doublecheck = mysql_num_rows($sql_doublecheck); 

if($doublecheck == 0){ 
    echo 
	'<div align="center">
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p><font color=red size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>No se   ha podido activar su incscripción. Por favor, póngase en contacto con <a href="mailto:jim@mncn.csic.es"><font color="#FFFF00"> el administrador de este servicio.</strong></font></p>
  </div>';
	} elseif ($doublecheck > 0) {
	$sql_lastlogin = mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'"); 
    echo 
	 '<div align="center">
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Su inscripción ha sido activada correctamente</strong></font></p>
   </div>';
   echo
   '<div align="center"><p align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Tiene acceso a la base de datos del grupo <font color="#FFFF00">'.$_REQUEST['usegrp'].'<font color="#FFFFFF">. <br><br>Por favor, <a href="log_'.$_REQUEST['usegrp'].'.php"><font color="#FFFF00">login </font></a>ahora.</strong></font></p><p align="center">&nbsp;</p><p align="center">&nbsp;</p><p align="center">&nbsp;</p></div>';
	} 
	else
	{
	echo "No Match";
	}
  
?>
The following is a portion of the registration.php code. If I can run the validate.php page from the registration page what should the a href statement look like. Also, another important (and my last) question is how do I write the script so that the successful or unsuccessful VALIDATE messages REPLACE the previous displayed messages that were returned upon successful registration.

Code: Select all

<?php
<?php   if (!empty($error_str)) {
        // errors have occured, halt execution and show form again.
        echo '<p><div align="center"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">There were errors in the information you entered, they are listed below:</font></div></p>';
        echo '<ul>'.$error_str.'</ul>';
     	 // show form again
        user_form();
        } else { 
   $sql = "INSERT INTO users (first_name, last_name, email_address, username, password, user_level, signup_date) 
   VALUES('$first_name', '$last_name', '$email_address', '$username', '$password', '$user_level', NOW())"; 
   @mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>'); 

   if (mysql_affected_rows() < 1) { 
     echo '<table width="300" align="center" cellpadding="3" cellspacing="3">';
     echo '<tr><td colspan="2">&nbsp;</td></tr>';
     echo '<tr><td colspan="2"><strong><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">There has been an error creating your account. Please contact the webmaster..</font></strong></td></tr>';
	 echo '<tr><td colspan="2">&nbsp;</td></tr>';
     echo '</table>';
      } 
	  else
	  { 
	      
     echo '<table width="300" align="center" cellpadding="3" cellspacing="3">';
     echo '<tr><td colspan="2">&nbsp;</td></tr>';
     echo '<tr><td colspan="2"><strong><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">User registered successfully.</font></strong></td></tr>';
     echo '<tr><td colspan="2"><strong><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">These are the login and passwords that you must enter to access the database:</font></strong></td></tr>';
     echo '<tr><td width="191"><strong><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Login: 
            '.$username.' </font></strong></td><td width="86">&nbsp;</td></tr>';
     echo '<tr><td><strong><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Password: 
            '.$password.' </font></strong></td><td>&nbsp;</td></tr>';
     echo '<tr><td colspan="2"><strong><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Click 
            <a href="???????????"><font color="#FFFF00">here</font></a> to validate your account.</font></strong></td></tr>';
     echo '<tr><td colspan="2">&nbsp;</td></tr>';
     echo '</table>';
	}
} 

?>
Many thanks for your help....it is much appreciated.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Re: calling a function or page to another page

Post by scorphus »

I don't think it has to be a function. In registration.php you could set a form with hiden text fileds holding userid, code and usegrp values. Then add an <input type="image"> tag inside the form to perform the submit operation. Like this:

Code: Select all

<form method="post" action="validate.php">
<input type="hidden" name="userid" value="<?=$userid?>">
<input type="hidden" name="code" value="<?=$code?>">
<input type="hidden" name="usegrp" value="<?=$usegrp?>">
<span class="textSpan">Click validate button to complete the registration registration:&nbsp;&nbsp;</span>
<input type="image" value="Validate" name="ValidateButton" src="http://www.host.com/validate-button.gif" border="0" width="32" height="16" align="center">
</form>
Hope it helps. Let me know if does not.

Cheers,
Scorphus.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

one method for this sort of thing that i prefer to use is to put the form on the end of the whole script....
why? because i can validate any data sent, and either reshow the form or show a result/redirect

so really what you would be thinking is:

Code: Select all

if(data_exists)
&#123;
  //validate
  if(validated ok)
  &#123;
      redirect/show result
      exit;
   &#125;
&#125;
//show error messages
//show form
(when the user submits the form the page reloads with new error messages. You just have to make sure to remember to re-fill the form with the data posted)
Post Reply