0

http://www.dandesign.co.uk/steel/register.php

above is the link to my register page, filling the form in works fine but if you leave it blank and click the submit button it removes the footer of my page, please someone help i really need to get this sorted

heres the code to my page too, im not great at php so its a little messy:S

<?php
session_start(); 
include("database.php");
include("login.php");

/**
 * Returns true if the username has been taken
 * by another user, false otherwise.
 */
function usernameTaken($username){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from customer where username = '$username'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}

/**
 * Inserts the given (username, password) pair
 * into the database. Returns true on success,
 * false otherwise.
 */
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO customer (Firstname, Surname, Company, Email, Type, Tel, Mob, username, password) VALUES ('$_POST[first]', '$_POST[surn]', '$_POST[comp]', '$_POST[email]', '$_POST[type]', '$_POST[tel]', '$_POST[mob]', '$username', '$password')";
   return mysql_query($q,$conn);
}

/**
 * Displays the appropriate message to the user
 * after the registration attempt. It displays a 
 * success or failure status depending on a
 * session variable set during registration.
 */
function displayStatus(){
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']){
	   echo ("Registered!");
	   }
	 else{ 
	 
?>
		<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
			<h1>Register Form</h1>
			<p>Personal Details</p>

			<label>First Name:<span class="small">Add your name</span></label><input type="text" name="first" />
            <label>Surname:<span class="small">Add your name</span></label><input type="text" name="surn" />
            <label>Company:<span class="small">Add your name</span></label><input type="text" name="comp" />
            <label>Email:<span class="small">Add a valid address</span></label><input type="text" name="email" />
            <label>Customer<span class="small">Add your name</span></label><input type="radio" name="type" align="left" value="cus" />
            <label>Stockist<span class="small">Add your name</span></label><input type="radio" name="type" align="left" value="stk" />
            <label>Telephone:<span class="small">Add a valid address</span></label><input type="text" name="tel" />
            <label>Mobile:<span class="small">Add a valid address</span></label><input type="text" name="mob" />
			<label>Username:<span class="small">Min. size 8 chars</span></label><input type="text" name="user" />
			<label>Password:<span class="small">Min. size 6 chars</span></label><input type="password" name="pass" />
			<button type="submit" name="subjoin">Sign-up</button>
            <input type="hidden" name="MM_insert" value="fact_form_id" />
		</form>
<?php
}
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body class="thrColElsHdr">

<div id="container">
    <div id="header">
      <div id="styli">
  	    <?php displayLogin(); ?>
  	  </div>
    </div>
  
  <div id="sidebar1">
    <ul id="MenuBar1" class="MenuBarVertical">
      <li><a href="index.php">Home</a></li>
      
      <li><a class="MenuBarItemSubmenu" href="#">Quote Generator</a></li>
      <li><a href="factor_form.php">Login/Register</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Help/FAQ</a></li>
      <li><a href="#">Contact</a></li>
      <li><a href="#">Legal</a></li>
    </ul>
    <div id="news">
    	<h1>News</h1>
    	<h2>30.07.10 - News 1</h2>
    	<p>Lorem ipsum diam nonumy eirmod tempor. Invidunt ut labore et dolore magna...</p>
    	<h2>06.08.10 - News 2</h2><p>We have developed the site to make it 10x faster than before, so you can get your qoutes when you need them...</p>
    </div>
    <div id="guarantee">
    	<h1>The Mission Statement...</h1>
    	<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor. Invidunt ut labore et dolore magna aliquyam erat, seddiam voluptua.</p>
    </div>
  </div>
  
  <div id="sidebar2">
    <h3>SideBar2 Advert</h3>
    <p></p>
    <p></p>
  </div>
  
  <div id="mainContent">
	 <div id="main_img"></div>
	 <div id="main_body">
        <div id="stylized" class="myform">    
	<?php displayStatus(); ?>
    <?php

   if(isset($_POST['subjoin'])){
	   
   /* Make sure all fields were entered */
   if(!$_POST['user'] || !$_POST['pass']){
      die("<p>You didn't fill in a required field</p>");
   }

   /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30){
      die("<p>Sorry, the username is longer than 30 characters, please shorten it</p>");
   }
  

   /* Check if username is already in use */
   if(usernameTaken($_POST['user'])){
      $user = $_POST['user'];
      die("<p>Sorry, the username: <strong>$user</strong> is already taken, please pick another one</p>");
   }


   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']);
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
}
?>
   	 </div>
     </div>
	 </div>
	<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
    
  <div id="footer">
    <p><a href="index.php">Home</a> |  Login Customer  |  <a href="factor_form.php">Login Factor</a> |  News  |  Help/FAQ  |  Contact  |  Legal  |  Terms & Conditions</p>
    <p>Tel: 0114 XXX XXXX  |  Fax: 0114 XXX XXXX</p>
  </div>
<!-- end #container --></div>
  <div id="design">
    <p>Website Designed by: <strong>Fissure Design</strong></p>
  </div>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>


<?php
   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
?>
2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by MindSter
1

Correct me if I'm wrong, but when you put DIE(), the rest of the page is ignored ... Thus,

if(!$_POST['user'] || !$_POST['pass']){
      die("<p>You didn't fill in a required field</p>");
   }

Can simply be

if(!$_POST['user'] || !$_POST['pass']){
      echo '<p>You didn\'t fill in a required field</p>';
   }

{btw, you could try and sanitize the entry data!}

Edited by MindSter: n/a

Votes + Comments
Really helpful Thanks
0
if(!$_POST['user'] || !$_POST['pass']){
      echo '<p>You didn\'t fill in a required field</p>';
   }

this would be great but using echo still executes the script and enters it into the database which i dont want becuase it is blank

0

bump I know that for some reason that if the field is left blank or the password is taken it die' and this stops the footer of the html being processed but im desperate for a way around it please someone help

0

Well, as a start you could rewrite your code as this :

if (!requirement){
//echo false
}
elseif(!req2){
//echofalse
}
else{//everyting is fiiiine
addNewUser($_POST['user'], $md5pass);
}

I'm pretty sure this will work.
EDIT: Here's the supposed-to-be-working code:

<?php
session_start(); 
include("database.php");
include("login.php");

/**
 * Returns true if the username has been taken
 * by another user, false otherwise.
 */
function usernameTaken($username){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from customer where username = '$username'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}

/**
 * Inserts the given (username, password) pair
 * into the database. Returns true on success,
 * false otherwise.
 */
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO customer (Firstname, Surname, Company, Email, Type, Tel, Mob, username, password) VALUES ('$_POST[first]', '$_POST[surn]', '$_POST[comp]', '$_POST[email]', '$_POST[type]', '$_POST[tel]', '$_POST[mob]', '$username', '$password')";
   return mysql_query($q,$conn);
}

/**
 * Displays the appropriate message to the user
 * after the registration attempt. It displays a 
 * success or failure status depending on a
 * session variable set during registration.
 */
function displayStatus(){
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']){
	   echo ("Registered!");
	   }
	 else{ 
	 
?>
		<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
			<h1>Register Form</h1>
			<p>Personal Details</p>

			<label>First Name:<span class="small">Add your name</span></label><input type="text" name="first" />
            <label>Surname:<span class="small">Add your name</span></label><input type="text" name="surn" />
            <label>Company:<span class="small">Add your name</span></label><input type="text" name="comp" />
            <label>Email:<span class="small">Add a valid address</span></label><input type="text" name="email" />
            <label>Customer<span class="small">Add your name</span></label><input type="radio" name="type" align="left" value="cus" />
            <label>Stockist<span class="small">Add your name</span></label><input type="radio" name="type" align="left" value="stk" />
            <label>Telephone:<span class="small">Add a valid address</span></label><input type="text" name="tel" />
            <label>Mobile:<span class="small">Add a valid address</span></label><input type="text" name="mob" />
			<label>Username:<span class="small">Min. size 8 chars</span></label><input type="text" name="user" />
			<label>Password:<span class="small">Min. size 6 chars</span></label><input type="password" name="pass" />
			<button type="submit" name="subjoin">Sign-up</button>
            <input type="hidden" name="MM_insert" value="fact_form_id" />
		</form>
<?php
}
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body class="thrColElsHdr">

<div id="container">
    <div id="header">
      <div id="styli">
  	    <?php displayLogin(); ?>
  	  </div>
    </div>
  
  <div id="sidebar1">
    <ul id="MenuBar1" class="MenuBarVertical">
      <li><a href="index.php">Home</a></li>
      
      <li><a class="MenuBarItemSubmenu" href="#">Quote Generator</a></li>
      <li><a href="factor_form.php">Login/Register</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Help/FAQ</a></li>
      <li><a href="#">Contact</a></li>
      <li><a href="#">Legal</a></li>
    </ul>
    <div id="news">
    	<h1>News</h1>
    	<h2>30.07.10 - News 1</h2>
    	<p>Lorem ipsum diam nonumy eirmod tempor. Invidunt ut labore et dolore magna...</p>
    	<h2>06.08.10 - News 2</h2><p>We have developed the site to make it 10x faster than before, so you can get your qoutes when you need them...</p>
    </div>
    <div id="guarantee">
    	<h1>The Mission Statement...</h1>
    	<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor. Invidunt ut labore et dolore magna aliquyam erat, seddiam voluptua.</p>
    </div>
  </div>
  
  <div id="sidebar2">
    <h3>SideBar2 Advert</h3>
    <p></p>
    <p></p>
  </div>
  
  <div id="mainContent">
	 <div id="main_img"></div>
	 <div id="main_body">
        <div id="stylized" class="myform">    
	<?php displayStatus(); ?>
    <?php

   if(isset($_POST['subjoin'])){
	      /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);//added at the top
   
   /* Make sure all fields were entered */
   if(!$_POST['user'] || !$_POST['pass']){//1st check
      echo"<p>You didn't fill in a required field</p>";
   }


   elseif(strlen($_POST['user']) > 30){//second check
      echo"<p>Sorry, the username is longer than 30 characters, please shorten it</p>";
   }
  

   /* Check if username is already in use */
   elseif(usernameTaken($_POST['user'])){//last check
      $user = $_POST['user'];
      echo "<p>Sorry, the username: <strong>$user</strong> is already taken, please pick another one</p>";
   }

	else{//Everything is fine
   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']);
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
	}
}
?>
   	 </div>
     </div>
	 </div>
	<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
    
  <div id="footer">
    <p><a href="index.php">Home</a> |  Login Customer  |  <a href="factor_form.php">Login Factor</a> |  News  |  Help/FAQ  |  Contact  |  Legal  |  Terms & Conditions</p>
    <p>Tel: 0114 XXX XXXX  |  Fax: 0114 XXX XXXX</p>
  </div>
<!-- end #container --></div>
  <div id="design">
    <p>Website Designed by: <strong>Fissure Design</strong></p>
  </div>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>


<?php
   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
?>

Edited by MindSter: New code added

0

how did you do that, im trying to see what you changed so i can learn, thanks for that it works great, ive been swearing at this for two days now

0

I explained at THE TOP of the post, and commented the code :)
Again, I used IF/ELSEIF statement to check for 'unusual' stuff; when ALL-OK, it jumps right to the FINAL ELSE, when it adds the data.
SUM: remove DIE, put ECHO, treat the code so as the Query will be executed ONLY if all checks out :)
(you could mark the thread as solved now ;) )

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.