Jump to content

[SOLVED] A better way to write this script??


farkewie

Recommended Posts

Hello,

 

i am trying o write a register script here are the features im trying to get working

 

#confirm password

#check each field is filled out

#display all errors in a message box

#leaving fields populated when gets errors

 

i have all of that working but im thingking when it does get anerror its going to still insert in to the database and email user because there iss no "die;" or "exit;" after the error

 

but if i put them in the form doesnt display again

and if i add the php below the html the message doesnt display??

 

any ideas?

 

<?php 


include ("sql.php");


$user_name = $_POST['user_name'];
$user_password = $_POST['user_password'];
$user_password2 = $_POST['user_password2'];
$user_firstname = $_POST['user_firstname'];
$user_lastname = $_POST['user_lastname'];
$user_email = $_POST['user_email'];
$form_name = $_POST['form_name'];
$confirmkey =  md5(uniqid(rand()))



//check if username exists
mysql_select_db("tyspicsc_albumv1", $sqlconnection);
$check = "SELECT * FROM users WHERE user_name='$user_name'";
$check = mysql_query($check);
$check_row = mysql_fetch_assoc($check);

$check = $check_row['user_name'];   

echo  $check ;



if ($form_name == "register_form") {


//make sure all the field are fill out correctly

	if (empty($user_name)) {
		$message = "Please fill out Username!";
		header('Location: '.$_SERVER['HTTP_REFERER'].$_SERVER['QUERY_STRING']);
		exit;
	}
	if (empty($user_password)) {
		$message = "Please fill out Password!";
	}
	if (empty($user_password2)) {
		$message = "Please Confirm Password!";
	}
	if (empty($user_firstname)) {
		$message = "Please fill out First Name!";
	}
	if (empty($user_lastname)) {
		$message = "Please fill out Last Name!";
	}
	if (empty($user_email)) {
		$message = "Please fill out Email!";
	}		
	if (isset($check)) {
		$message = "Username already taken! <a href=\"pwremind.php\">Click Here</a> if you have forgotten your password.";

	}
	if ($user_password !== $user_password2) {
		$message =  "Passwords do not match!!";
	}
	else { 
		$user_password = md5($user_password);	
	}



	 ##############################################
	 ###### insert in to database and email admin##
	 ##############################################
//end form script	
}




?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="AC_RunActiveContent.js" type="text/javascript"></script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tyspics.com <?php echo $album ;?></title>

<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>


<p><span class="heading">Tyspics.com</span><br>
    <span class="Logo">All Our Trashy Photo's</span></p>
<p>
  <?php if (isset($message)){?>
</p>
<table width="468" border="1" align="center">
  <tr>
    <td class="aaa">Message</td>
  </tr>
  <tr>
    <td class="errortext"><?php echo $message ?></td>
  </tr>
</table>
<?php } ?>
<form action="<?php $_SERVER['PHP_SELF']  ?>" method="post" name="register_form" id="register_form">
  <table width="468" height="192" border="1" align="center">
    <tr>
      <td colspan="2" class="errortext">All details are required!
      <input name="form_name" type="hidden" id="form_name" value="register_form" /></td>
    </tr>
    <tr>
      <td width="215" class="Logo">Username:</td>
      <td width="237"><label>
        <input name="user_name" type="text" class="aaa" id="user_name" value="<?php  echo $_POST['user_name'];?>" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">Password:</td>
      <td><label>
        <input name="user_password" type="password" class="aaa" id="user_password" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">Confirm Password:</td>
      <td><label>
        <input name="user_password2" type="password" class="aaa" id="user_password2" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">First Name:</td>
      <td><label>
        <input name="user_firstname" type="text" class="aaa" id="user_firstname" value="<?php  echo $_POST['user_firstname'];?>" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">Last Name:</td>
      <td><label>
        <input name="user_lastname" type="text" class="aaa" id="user_lastname" value="<?php  echo $_POST['user_lastname'];?>" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">Email Address</td>
      <td><label>
        <input name="user_email" type="text" class="aaa" id="user_email" value="<?php  echo $_POST['user_email'];?>" />
      </label></td>
    </tr>
    <tr>
      <td colspan="2" class="Logo"><div align="center">
        <label>
        <input name="button" type="submit" class="mybutton" id="button" value="Register" />
        </label>
      </div></td>
    </tr>
  </table>
</form>
<p> </p>
</body>


</html>

 

i know i havent set the email and mysql insert up yet i can handle that later.

 

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/75991-solved-a-better-way-to-write-this-script/
Share on other sites

what I'd do, is make a page called page_fns.php, and then have:

 

<?php
function doForm(){
?>
ANY HTML YOU WANT IN THE FORM (php works aswell)
<?php
}
?>

 

Then, whenever you want the form, put doForm(); So then, you can exit(); and stuff.

 

<?php

if (empty($user_name)) {
		$message = "Please fill out Username!";
		doForm();
		exit;
	}
	if (empty($user_password)) {
                        doForm();
		$message = "Please fill out Password!";
		exit;
	}
	if (empty($user_password2)) {
                        doForm();
		$message = "Please Confirm Password!";
		exit;
	}
	if (empty($user_firstname)) {
                        doForm();
		$message = "Please fill out First Name!";
		exit;
	}
	if (empty($user_lastname)) {
                        doForm();
		$message = "Please fill out Last Name!";
		exit;
	}
	if (empty($user_email)) {
                        doForm();
		$message = "Please fill out Email!";
		exit;
	}		
	if (isset($check)) {
                        doForm();
		$message = "Username already taken! <a href=\"pwremind.php\">Click Here</a> if you have forgotten your password.";
		exit;
	}
	if ($user_password !== $user_password2) {
                        doForm();
		$message =  "Passwords do not match!!";
		exit;
	}
	else { 
		$user_password = md5($user_password);	
	}

?>

Ok so that is working

 

but my errors and not being displayed in the message box still ?

 

register.php

<?php 
 	include ("functions.php");
	include ("sql.php");


$user_name = $_POST['user_name'];
$user_password = $_POST['user_password'];
$user_password2 = $_POST['user_password2'];
$user_firstname = $_POST['user_firstname'];
$user_lastname = $_POST['user_lastname'];
$user_email = $_POST['user_email'];
$form_name = $_POST['form_name'];
$confirmkey =  md5(uniqid(rand()));






//check if username exists
mysql_select_db("tyspicsc_albumv1", $sqlconnection);
$check = "SELECT * FROM users WHERE user_name='$user_name'";
$check = mysql_query($check);
$check_row = mysql_fetch_assoc($check);

$check = $check_row['user_name'];   

echo  $check ;



if ($form_name == "register_form") {


//make sure all the field are fill out correctly

	if (empty($user_name)) {
		$message = "Please fill out Username!";
		form();
		exit;
	}
	if (empty($user_password)) {
		$message = "Please fill out Password!";
		form();
		exit;
	}
	if (empty($user_password2)) {
		$message = "Please Confirm Password!";
		form();
		exit;
	}
	if (empty($user_firstname)) {
		$message = "Please fill out First Name!";
		form();
		exit;
	}
	if (empty($user_lastname)) {
		$message = "Please fill out Last Name!";
		form();
		exit;
	}
	if (empty($user_email)) {
		$message = "Please fill out Email!";
		form();
		exit;
	}		
	if (isset($check)) {
		$message = "Username already taken! <a href=\"pwremind.php\">Click Here</a> if you have forgotten your password.";
		form();
		exit;

	}
	if ($user_password !== $user_password2) {
		$message =  "Passwords do not match!!";
	}
	else { 
		$user_password = md5($user_password);	
	}



	 ##############################################
	 ###### insert in to database and email admin##
	 ##############################################
//end form script	
}

if (empty($_POST['form_name'])) {
	form();
}



?>

 

functions.php

 

<?php function form() {?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="AC_RunActiveContent.js" type="text/javascript"></script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tyspics.com <?php echo $album ;?></title>

<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>


<p><span class="heading">Tyspics.com</span><br>
    <span class="Logo">All Our Trashy Photo's</span></p>
<p>
  <?php if (isset($message)){?>
</p>
<table width="468" border="1" align="center">
  <tr>
    <td class="aaa">Message</td>
  </tr>
  <tr>
    <td class="errortext"><?php echo $message ?></td>
  </tr>
</table>
<?php } ?>
<form action="<?php $_SERVER['PHP_SELF']  ?>" method="post" name="register_form" id="register_form">
  <table width="468" height="192" border="1" align="center">
    <tr>
      <td colspan="2" class="errortext">All details are required!
      <input name="form_name" type="hidden" id="form_name" value="register_form" /></td>
    </tr>
    <tr>
      <td width="215" class="Logo">Username:</td>
      <td width="237"><label>
        <input name="user_name" type="text" class="aaa" id="user_name" value="<?php  echo $_POST['user_name'];?>" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">Password:</td>
      <td><label>
        <input name="user_password" type="password" class="aaa" id="user_password" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">Confirm Password:</td>
      <td><label>
        <input name="user_password2" type="password" class="aaa" id="user_password2" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">First Name:</td>
      <td><label>
        <input name="user_firstname" type="text" class="aaa" id="user_firstname" value="<?php  echo $_POST['user_firstname'];?>" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">Last Name:</td>
      <td><label>
        <input name="user_lastname" type="text" class="aaa" id="user_lastname" value="<?php  echo $_POST['user_lastname'];?>" />
      </label></td>
    </tr>
    <tr>
      <td class="Logo">Email Address</td>
      <td><label>
        <input name="user_email" type="text" class="aaa" id="user_email" value="<?php  echo $_POST['user_email'];?>" />
      </label></td>
    </tr>
    <tr>
      <td colspan="2" class="Logo"><div align="center">
        <label>
        <input name="button" type="submit" class="mybutton" id="button" value="Register" />
        </label>
      </div></td>
    </tr>
  </table>
</form>
<p> </p>
</body>


</html>

<?php
}
?>

Change the function form() to function form($message). Then when you want the message, don't put:

 

<?php if (isset($message)){?>
</p>
<table width="468" border="1" align="center">
  <tr>
    <td class="aaa">Message</td>
  </tr>
  <tr>
    <td class="errortext"><?php echo $message ?></td>
  </tr>
</table>
<?php } ?>

put

<?php if ($message != ''){?>
</p>
<table width="468" border="1" align="center">
  <tr>
    <td class="aaa">Message</td>
  </tr>
  <tr>
    <td class="errortext"><?php echo $message ?></td>
  </tr>
</table>
<?php } ?>

 

So when you call the function, say,

 

<?php
if (empty($user_email)) {
		$message = "Please fill out Email!";
		form($message);
		exit;
	}		
?>

 

if there is no message, just put form('');

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.