Link to home
Start Free TrialLog in
Avatar of satmanuk
satmanukFlag for United Kingdom of Great Britain and Northern Ireland

asked on

need help tweaking insert/registration form

Hi all,

PHP,MySQL

I have created an insert form using dreamweaver. This acts as a simple user registration form. It lacks a few features that i would like implimented.

1. If any field is left blank when the form is submitted i want to echo some text under the table saying "please fill all fields marked * " 
2. The confirm password field doesnt do anything at the moment but i want to have this compared with the password field to eliminate typo's on password creation.
3. The username field is currently an editable field that the user chooses. I want this to be non editable and to be auto generated based on first name and lastname. example firstname: joe Lastname: bloggs would give username: joe bloggs

Hope this isnt asking too much?


heres my code:

<?php require_once('Connections/atrbcon.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "registerform")) {
  $insertSQL = sprintf("INSERT INTO tbl_users (username, password, firstname, lastname, usergroup, email) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($HTTP_POST_VARS['username'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['password'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['firstname'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['lastname'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['usergroup'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['email'], "text"));

  mysql_select_db($database_atrbcon, $atrbcon);
  $Result1 = mysql_query($insertSQL, $atrbcon) or die(mysql_error());

  $insertGoTo = "newpage.php";
  if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>

<!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>
<title>Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body bgcolor="#000000">

<p align="center"><font color="#FFFFFF" size="4">Registration for site</font></p>
<form name="registerform" id="registerform" method="POST" action="<?php echo $editFormAction; ?>">
  <input name="usergroup" type="hidden" id="usergroup" value="member" />
  <table width="400" border="1" align="center">
    <tr>
      <td colspan="2"><font color="#FFFFFF">All fields marked with <font color="#FF0000">*</font>
        should be filled in</font></td>
    </tr>
    <tr>
      <td width="172"> <div align="center"><font color="#FFFFFF">First Name <font color="#FF0000">*</font></font></div></td>
      <td width="212"> <div align="center">
          <input name="firstname" type="text" id="firstname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Last Name <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="lastname" type="text" id="lastname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Username</font></div></td>
      <td> <div align="center">
          <input name="username" type="text" id="username" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="password" type="text" id="password" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Confirm Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="conpassword" type="text" id="conpassword" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Email</font></div></td>
      <td> <div align="center">
          <input name="email" type="text" id="email" />
        </div></td>
    </tr>
    <tr>
      <td colspan="2"> <div align="center">
          <input type="submit" name="Submit" value="Register" />
        </div></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="registerform">
</form>
<p>&nbsp; </p>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of Mark Gilbert
Mark Gilbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of satmanuk

ASKER

sorry but what questions does this dela with?
Avatar of davidateuropol
davidateuropol

This deals with any empty field in the form...

This also works for that and you would have to do it for every field :

$error_found=false; // to make sure that the variable is on false before you begin checking the fields in general

if(isSet($_POST['firstname'])&&(strlen($_POST['firstname']))==0){ //The field has been set but it is empty
 $error_firstname = "Your errormessage";
 $error_found = true;
}

if(isSet($_POST['lastname'])&&(strlen($_POST['lastname']))==0){ //The field has been set but it is empty
 $error_lastname = "Your errormessage";
 $error_found = true;
}

and so on for the rest of the fields.

Password comparison :
if(isSet($_POST['password'])&& isSet($_POST['conpassword'])){
 if($_POST['password'] != $_POST['conpassword']){
     $error_found = true;
     $error_message = "Passwords don't match";
}
If your script for handling the content is within another file you can jump back to it using the $error_found variable

if($error_found){
   header("Location: whateverpage"); // This can only be used when you have no other headers inside the file before.
}

For your last question :

You can create a field like this :
<input type="text" name="lastandfirstname" disabled>

Now you only have to combine the values within the 2 fields and put it in this one.
Thanks for the respone, i have tried what you said but the code doesnt work. Any ideas how to get what you have suggested into my exisitng page?

ie. where does your suggested code insert into my code?

I will give points to anyone who can tell me where to put the needed code in order to get the results i want.


Thanks
ok so dealing with an empty field....

I added your code and when i tested it the page still goes to a blank white page saying Lastname cannot be NULL

here is where i put the code: i commented a line above and below the added code from your suggestion. any ideas?

<?php require_once('Connections/atrbcon.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}
// start of added code
$error_found=false; // to make sure that the variable is on false before you begin checking the fields in general

if(isSet($_POST['firstname'])&&(strlen($_POST['firstname']))==0){ //The field has been set but it is empty
 $error_firstname = "Please fill in the firstname field";
 $error_found = true;
}

if(isSet($_POST['lastname'])&&(strlen($_POST['lastname']))==0){ //The field has been set but it is empty
 $error_lastname = "Please fill in the lastname field";
 $error_found = true;
}
if(isSet($_POST['password'])&&(strlen($_POST['password']))==0){ //The field has been set but it is empty
 $error_password = "Please fill in the password field";
 $error_found = true;
}
// end of added code


if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "registerform")) {
  $insertSQL = sprintf("INSERT INTO tbl_users (username, password, firstname, lastname, usergroup, email) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($HTTP_POST_VARS['username'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['password'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['firstname'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['lastname'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['usergroup'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['email'], "text"));

  mysql_select_db($database_atrbcon, $atrbcon);
  $Result1 = mysql_query($insertSQL, $atrbcon) or die(mysql_error());

  $insertGoTo = "http://www.site.co.uk";
  if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

?>

<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>

<!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>
<title>Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body bgcolor="#000000">



<p align="center"><font color="#FFFFFF" size="4">Registration for site</font></p>
<form name="registerform" id="registerform" method="POST" action="<?php echo $editFormAction; ?>">
  <input name="usergroup" type="hidden" id="usergroup" value="member" />
  <table width="400" border="1" align="center">
    <tr>
      <td colspan="2"><font color="#FFFFFF">All fields marked with <font color="#FF0000">*</font>
        should be filled in</font></td>
    </tr>
    <tr>
      <td width="172"> <div align="center"><font color="#FFFFFF">First Name <font color="#FF0000">*</font></font></div></td>
      <td width="212"> <div align="center">
          <input name="firstname" type="text" id="firstname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Last Name <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="lastname" type="text" id="lastname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Username</font></div></td>
      <td> <div align="center">
          <input name="username" type="text" id="username" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="password" type="text" id="password" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Confirm Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="conpassword" type="text" id="conpassword" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Email</font></div></td>
      <td> <div align="center">
          <input name="email" type="text" id="email" />
        </div></td>
    </tr>
    <tr>
      <td colspan="2"> <div align="center">
          <input type="submit" name="Submit" value="Register" />
        </div></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="registerform">
</form>

<p>&nbsp; </p>
</body>
</html>
If the system is telling you that the lastname cannot be null, that's a database issue where your database is set to that field as null.  Additionally, if the value is not being parsed correctly to the database as in :

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "registerform")) {
  $insertSQL = sprintf("INSERT INTO tbl_users (username, password, firstname, lastname, usergroup, email) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($HTTP_POST_VARS['username'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['password'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['firstname'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['lastname'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['usergroup'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['email'], "text"));

Then you would need to look at the form and see exactly what the lastname field is actually called.  Chances are you have case incorrect, or there is a problem with the database field.  Give it a shot and see what happens.
issue where your database is set to that field as null should read: issue where your database is set to not null in the database value.  By not giving it a value, and the database receiving a null value, it will bomb out.
sorry but i dont get it.

My form works fine except when the fields are left empty(which peple tend to do)

so the point of this post is to find out what code i have to insert into my page so i get echo'd messages telling user to fill all fields in if they leave any empty

Do you understand?

Thanks
The first bit of code I provided checked whether a field was left blank, and if it was blank, to create a variable value which contained an error message.  I provided that code without every answer available because sometimes it's useful to use the logic and then improve on it to come up with a solution.  

Could I suggest that you review the code again and see where it can fit into the logic of your code.  If you don't understand what it does, let me know and I will do my best to explain how and why it works in the situation that I found it best to work in.  We can get it to fit your model perfectly, it just needs some modifcation and tweaking to fit nicely.

Hope this helps a bit.
Oh just one other thing, I notice that you do have some errors defined in your code above, before the sql insert statement, however your code that is asking whether something has been posted to it just before the sql statement, doesn't look for the error string.  It simply bypasses that.  

To give you a little guidance to how my script above works is that it first checks all the values of the posts, it then checks much lower down "if($isSubmitValid == true){" and then if yes, we then put in our sql statements, and email scripts, or what ever we want to happen if the values are correct and we want to process them.  If not, then we display the error messages.  Otherwise, if the whole form hasn't been submitted at that stage, instead of trying to process anything which doesn't exist, simply display the form for the user to submit :)
sorry mate, i am missing something(and bunged up with a cold)

Not sure what you mean by

review the code again and see where it can fit into the logic of your code

I am a relative newbie at this although i do get quite a bit. I just need help knowing what things actually do and where in my provided script i would put the suggested code.

If i am right the code you posted would need a fair bit of understanding of it, to edit and put in my script. or i am being stupid, which happens.. :O)

Thanks for your patience
Oh dear Satmanuk, I know exactly where you are coming from with the cold situation.  Would you believe that I have also been in the process of getting one too...not nice believe me.

Okay, I'll help you decipher where things need to be sorted because I'm just a nice guy :)  Lol, not really...uhh actually yes, but I know what it's like to be a newbie and don't apologize, that's why we are here...I hope.

I can see that your code has been generated by Dreamweaver, which I use on a daily basis to provide the skeleton of the code I wish to use, but modify it to fit my requirements.  Modify your code to the following and see if that helps:

<?php require_once('Connections/atrbcon.php'); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>

<!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>
<title>Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body bgcolor="#000000">



<p align="center"><font color="#FFFFFF" size="4">Registration for site</font></p>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}


if(isset($_POST['submit'])){
//echo "POST DETECTED<br />";
          $isSubmitValid = true;
          //echo $isSubmitValid;
          $strMessage = "";
          $firstname = $_POST["firstname"];
          $lastname = $_POST["lastname"];
          $password = $_POST["password"];

          if($firstname == ""){
               $strMessage .= "<li>Please enter your firstname</li>";
               $isSubmitValid = false;
          }

          if($lastname == ""){
               $strMessage .= "<li>Please enter your lastname</li>";
               $isSubmitValid = false;
          }
          if($password == ""){
               $strMessage .= "<li>Please enter your password</li>";
               $isSubmitValid = false;
          }
         
               
          if($isSubmitValid == true){
$insertSQL = sprintf("INSERT INTO tbl_users (username, password, firstname, lastname, usergroup, email) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['username'], "text"),
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['firstname'], "text"),
                       GetSQLValueString($_POST['lastname'], "text"),
                       GetSQLValueString($_POST['usergroup'], "text"),
                       GetSQLValueString($_POST['email'], "text"));

  mysql_select_db($database_atrbcon, $atrbcon);
  $Result1 = mysql_query($insertSQL, $atrbcon) or die(mysql_error());

  $insertGoTo = "http://www.site.co.uk";
  if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
                <p style="color: red;"><?=$strEndMessage?> </p>
<?php
         
          } else{
?>

<form name="registerform" id="registerform" method="POST" action="<?php echo $editFormAction; ?>">
  <input name="usergroup" type="hidden" id="usergroup" value="member" />
  <table width="400" border="1" align="center">
    <tr>
      <td colspan="2"><font color="#FFFFFF">All fields marked with <font color="#FF0000">*</font>
        should be filled in</font></td>
    </tr>
    <tr>
      <td width="172"> <div align="center"><font color="#FFFFFF">First Name <font color="#FF0000">*</font></font></div></td>
      <td width="212"> <div align="center">
          <input name="firstname" type="text" id="firstname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Last Name <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="lastname" type="text" id="lastname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Username</font></div></td>
      <td> <div align="center">
          <input name="username" type="text" id="username" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="password" type="text" id="password" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Confirm Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="conpassword" type="text" id="conpassword" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Email</font></div></td>
      <td> <div align="center">
          <input name="email" type="text" id="email" />
        </div></td>
    </tr>
    <tr>
      <td colspan="2"> <div align="center">
          <input type="submit" name="Submit" value="Register" />
        </div></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="registerform">
</form>
<?php } ?>

<p>&nbsp; </p>
</body>
</html>

I'm not sure what version of dreamweaver you are working with, or what version of php you have on your server, however if your php version is higher than what your code was scripted with then HTTP_POST_VARS would be heavily outdated.  So I replaced it with _POST.  Just to note that both follow a $ which is a variable starter.  If you find that your php version is older and doesn't support $_POST, but only $HTTP_POST_VARS then do a search and replace on $_POST with $HTTP_POST_VARS.

Hope this helps.  let me know if you have any difficulties.
oops, where the

 header(sprintf("Location: %s", $insertGoTo));
}
?>
                <p style="color: red;"><?=$strEndMessage?> </p>
<?php
         
          } else{
?>



replace it with this:

  header(sprintf("Location: %s", $insertGoTo));
// On this page, display that the user was successful
} else{

?>
<p style="color: red;"><?=$strMessage; ?> <?php } ?>

<?php
         
          } else{
?>
Thanks for the help and understanding!

My PHP version is 4.4.4

I have run the page with the changed code but now the page loads fine but when i try and submit the form, the form emptys and it returns to the same page. I have tried with all fields filled out and i have tried missing some out but the same things happens.

i am pretty sure i have used POST in other pages do you know if this is ok?
i have tried replacing all $_post with $HTTP_POST_VARS  but nothing changes.

The page loads but is un useable, if you fill the form in and submit it, it returns to the same page and emptys the fields. No message about not filling in the fields and no data is inserted into the database.


anyone got any ideas on this?
Okay, I've had a look at it, and noticed that you don't have a default font colour, so all the text on the page is hidden.  I have done a slight re-write so that you can see how your code should look with the couple of mistakes fixed, along with some visible text.  Modify it so that it fits your design, but also use your mouse if you don't see an error message, especially if the form says it's successfully been submitted.

<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>

<!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>
<title>Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body bgcolor="#000000">



<p align="center"><font color="#FFFFFF" size="4">Registration for site</font></p>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . $_SERVER['QUERY_STRING'];
}


if(isset($_POST['MM_insert'])){
//echo "POST DETECTED<br />";
      $isSubmitValid = true;
      //echo $isSubmitValid;
      $strMessage = "";
      $firstname = $_POST["firstname"];
      $lastname = $_POST["lastname"];
      $password = $_POST["password"];
      $conpassword = $_POST["conpassword"];
      
      if($firstname == ""){
      $strMessage .= "<li style=\"color: red;\">Please enter your firstname</li>";
      
      $isSubmitValid = false;
      }
      
      if($lastname == ""){
      $strMessage .= "<li style=\"color: red;\">Please enter your lastname</li>";
      $isSubmitValid = false;
      }
      if($password == ""){
      $strMessage .= "<li style=\"color: red;\">Please enter your password</li>";
      $isSubmitValid = false;
      }
      if($password != $conpassword){
      $strMessage .= "<li style=\"color: red;\">Your passwords do not match...please confirm your password</li>";
      $isSubmitValid = false;
      }

      if($isSubmitValid == true){
            echo "<p style=\"color: red;\">Form was successfully submitted</p>";
            $insertSQL = sprintf("INSERT INTO tbl_users (username, password, firstname, lastname, usergroup, email) VALUES (%s, %s, %s, %s, %s, %s)",
            GetSQLValueString($_POST['username'], "text"),
            GetSQLValueString($_POST['password'], "text"),
            GetSQLValueString($_POST['firstname'], "text"),
            GetSQLValueString($_POST['lastname'], "text"),
            GetSQLValueString($_POST['usergroup'], "text"),
            GetSQLValueString($_POST['email'], "text"));
            
            mysql_select_db($database_atrbcon, $atrbcon);
            $Result1 = mysql_query($insertSQL, $atrbcon) or die(mysql_error());
            
            $insertGoTo = "http://www.site.co.uk";
            
            header(sprintf("Location: %s", $insertGoTo));
            // On this page, display that the user was successful
      } else{
      
      ?>
      <p style="color: red;"><?php echo "There was an error with your submission:<br />".
$strMessage; ?></p> <?php } ?>
      
      <?php
      
} else{
      ?>

<form name="registerform" id="registerform" method="POST" action="<?php echo $editFormAction; ?>">
  <input name="usergroup" type="hidden" id="usergroup" value="member" />
  <table width="400" border="1" align="center">
    <tr>
      <td colspan="2"><font color="#FFFFFF">All fields marked with <font color="#FF0000">*</font>
        should be filled in</font></td>
    </tr>
    <tr>
      <td width="172"> <div align="center"><font color="#FFFFFF">First Name <font color="#FF0000">*</font></font></div></td>
      <td width="212"> <div align="center">
          <input name="firstname" type="text" id="firstname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Last Name <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="lastname" type="text" id="lastname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Username</font></div></td>
      <td> <div align="center">
          <input name="username" type="text" id="username" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="password" type="text" id="password" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Confirm Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="conpassword" type="text" id="conpassword" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Email</font></div></td>
      <td> <div align="center">
          <input name="email" type="text" id="email" />
        </div></td>
    </tr>
    <tr>
      <td colspan="2"> <div align="center">
          <input type="submit" name="Submit" value="Register" />
        </div></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="registerform">
</form>
<?php } ?>

<p>&nbsp; </p>
</body>
</html>

Hope this helps.
Oh, in answer to using $_POST instead of $HTTP_POST_VARS, as you are running a version of php higher than php 4.3x, you should always use the latest code.  $_POST looks for fields in a form that have been submitted and $_GET looks for variable values in the address line (eg: http://www.mysite.com/index.php?id=63&uid=72&ref=29 produces 3 variables called id, uid, and ref, and each has a value).

Hope this helps.
Ok, thanks again.

A couple of things.
1. The form says "Form was successfully submitted" when  all fields are filled. but nothing is added to the database?
i looked at "view source" of the page and saw this:
<p style="color: red;">Form was successfully submitted</p><br />
<b>Warning</b>:  mysql_select_db(): supplied argument is not a valid MySQL-Link resource in <b>/home/user/public_html/atrb/register3.php</b> on line <b>86</b><br />
<br />
<b>Warning</b>:  mysql_query(): supplied argument is not a valid MySQL-Link resource in <b>/home/user/public_html/atrb/register3.php</b> on line <b>87</b><br />

Line 86:           mysql_select_db($database_atrbcon, $atrbcon);
Line 87:           $Result1 = mysql_query($insertSQL, $atrbcon) or die(mysql_error());

2. If i leave a field blank i do get a message "There was an error with your submission:Please enter your lastname" but this is a blank page and the form is no longer there. I was hoping to have the form sat there and echo the error below the table so thery can try again.
The way it is i dont see the form no more. Works great though...

Thanks so much for this!






 

ok i noticed the first line was missing
<?php require_once('Connections/atrbcon.php'); ?>

So i added that line. The record is now added. so all good. I looked at the "view source" again and noticed this though:
<p align="center"><font color="#FFFFFF" size="4">Registration for site</font></p>
<p style="color: red;">Form was successfully submitted</p><br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/user/public_html/atrb/register3.php:15) in <b>/home/user/public_html/atrb/register3.php</b> on line <b>91</b><br />

Line 91 is:           header(sprintf("Location: %s", $insertGoTo));


here is my code now:

<?php require_once('Connections/atrbcon.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>
<title>Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body bgcolor="#000000">



<p align="center"><font color="#FFFFFF" size="4">Registration for site</font></p>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . $_SERVER['QUERY_STRING'];
}


if(isset($_POST['MM_insert'])){
//echo "POST DETECTED<br />";
     $isSubmitValid = true;
     //echo $isSubmitValid;
     $strMessage = "";
     $firstname = $_POST["firstname"];
     $lastname = $_POST["lastname"];
     $password = $_POST["password"];
     $conpassword = $_POST["conpassword"];
     
     if($firstname == ""){
     $strMessage .= "<li style=\"color: red;\">Please enter your firstname</li>";
     
     $isSubmitValid = false;
     }
     
     if($lastname == ""){
     $strMessage .= "<li style=\"color: red;\">Please enter your lastname</li>";
     $isSubmitValid = false;
     }
     if($password == ""){
     $strMessage .= "<li style=\"color: red;\">Please enter your password</li>";
     $isSubmitValid = false;
     }
     if($password != $conpassword){
     $strMessage .= "<li style=\"color: red;\">Your passwords do not match...please confirm your password</li>";
     $isSubmitValid = false;
     }

     if($isSubmitValid == true){
          echo "<p style=\"color: red;\">Form was successfully submitted</p>";
          $insertSQL = sprintf("INSERT INTO tbl_users (username, password, firstname, lastname, usergroup, email) VALUES (%s, %s, %s, %s, %s, %s)",
          GetSQLValueString($_POST['username'], "text"),
          GetSQLValueString($_POST['password'], "text"),
          GetSQLValueString($_POST['firstname'], "text"),
          GetSQLValueString($_POST['lastname'], "text"),
          GetSQLValueString($_POST['usergroup'], "text"),
          GetSQLValueString($_POST['email'], "text"));
         
          mysql_select_db($database_atrbcon, $atrbcon);
          $Result1 = mysql_query($insertSQL, $atrbcon) or die(mysql_error());
         
          $insertGoTo = "http://www.site.co.uk";
         
          header(sprintf("Location: %s", $insertGoTo));
          // On this page, display that the user was successful
     } else{
     
     ?>
     <p style="color: red;"><?php echo "There was an error with your submission:<br />".
$strMessage; ?></p> <?php } ?>
     
     <?php
     
} else{
     ?>

<form name="registerform" id="registerform" method="POST" action="<?php echo $editFormAction; ?>">
  <input name="usergroup" type="hidden" id="usergroup" value="member" />
  <table width="400" border="1" align="center">
    <tr>
      <td colspan="2"><font color="#FFFFFF">All fields marked with <font color="#FF0000">*</font>
        should be filled in</font></td>
    </tr>
    <tr>
      <td width="172"> <div align="center"><font color="#FFFFFF">First Name <font color="#FF0000">*</font></font></div></td>
      <td width="212"> <div align="center">
          <input name="firstname" type="text" id="firstname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Last Name <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="lastname" type="text" id="lastname" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Username</font></div></td>
      <td> <div align="center">
          <input name="username" type="text" id="username" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="password" type="text" id="password" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Confirm Password <font color="#FF0000">*</font></font></div></td>
      <td> <div align="center">
          <input name="conpassword" type="text" id="conpassword" />
        </div></td>
    </tr>
    <tr>
      <td> <div align="center"><font color="#FFFFFF">Email</font></div></td>
      <td> <div align="center">
          <input name="email" type="text" id="email" />
        </div></td>
    </tr>
    <tr>
      <td colspan="2"> <div align="center">
          <input type="submit" name="Submit" value="Register" />
        </div></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="registerform">
</form>
<?php } ?>

<p>&nbsp; </p>
</body>
</html>

Thanks again
Okay, if you want the script to direct the user to a new page, with a custom message, change header(sprintf("Location: %s", $insertGoTo)); to header(sprintf("Location: mynewpage.php"));  Otherwise comment out the line with two forward slashes // header(sprintf("Location: %s", $insertGoTo));

hope this helps.
for some reason that head line doesnt work, i get this in the view source on internet explorer and that tells me thats why it isnt going to the page i have chosen in the header string.

<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/user/public_html/atrb/register3.php:15) in <b>/home/user/public_html/atrb/register3.php</b> on line <b>91</b><br />
     
Line 91 is:           header(sprintf("Location: http://www.mysite.co.uk"));

i have tried both

          header(sprintf("Location: http://www.mysite.co.uk"));
and

        //  header(sprintf("Location: %s", $insertGoTo));

with the same results.


The page stays on register.php and the table disapears.
If you want the table to show, remove the text that says the form was submitted, and then put in the form before the else
hey? you lost me

The way i imagined my page to work is.

visit page and view a form to fill.
If you forget to fill a field or pasword doesnt match then the echo'd message would be displayed below the showing table/form. Bit like when you register on any site really. You still see the form as you havent filled it out correctly yet. and the echo'd messages display either below the table or some have it by the field you havent filled in. i would be happy with the message being displayed below the table, as i want the user to still try and regsiter using the table/form. If the page goes else where then they may not go back and try again.

If the form is filled correctly then they would not see the form they would be directed to www.mysite.co.uk


Does that make sesne?

sorry and thanks
hey, no worries i kind of got what i am looking for. i will put another post up about the fine tweaking....


Thanks
Hi Satmanuk, glad you got it working.  Thanks for the points and grade, have a great day.