Solved

Auto generate a field in form based on 2 other fields

Posted on 2006-10-31
10
204 Views
Last Modified: 2013-12-12
Hi all,

I have an insert form using PHP and MySQL. The form is used as a registration form.

The form has 6 fields. Firstname, Lastname, Username, password, confirm password and email.

All of these are editable test fields.

I want to have the username field generated automatically by the first and lastname(as they are filled in) So if joe bloggs filled in the form he would not be able to choose his username it would be there once he filled the first 2 fields.

Is this possible?


my code:

<?php

     //      ******************************
     //     **          Include Globals          **
     //     ******************************

     require_once('Connections/atrbcon.php');

     
     //      ******************************
     //     **          Script Functions      **
     //     ******************************
     
     
     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;
     }
     
     
     function outputXmlHeaders () {
         
          // This Function will Output HTTP Headers
          echo      "     <?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">";
          echo     "     <!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>
                    ";
     }
     
     
     //      ******************************
     //     **          Script Body                **
     //     ******************************
     
     
     $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"];
         
           // i want these lines below centered
          if($firstname == ""){
          $strMessage .= "<li align=\"center\" 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) {
               
               // User was Successful DONT output anything just send them to the new page
               
                 $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());
             
               // Redirect User
               $insertGoTo = "http://www.mysite.co.uk";
               header("Location: ". $insertGoTo);
               exit();
 
          }

         
          else {          // Output that there was an Error
         
         
                 // Include HTML Headers
                 outputXmlHeaders();
                 
                 // Output Error Message
                       echo("<p align=\"center\" style=\"color: red;\">There was an error with your submission:<br />$strMessage</p>");
          }

     
     }
     
     else {
         
          // Form was Not Valid!
         
          // Include HTML Headers
            outputXmlHeaders();
           
            // Output Signup Form

          ?>

          <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>
0
Comment
Question by:satmanuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17847079
Well, the easy part is generating the basic username:
strtolower(substr($_POST['firstname'], 0, 1) . substr($_POST['lastname'], 1, 7))

What, though, should the behavior be if that username is already taken?
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17848496
sorry what does that code do?
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17848744
How does this work in times of from the users pointof view?

Will they see their username printed? The problem i have is the members dont all have emails so i need them to knoiw what their log in details are when they register.
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17848749
Okay, so this should be done dynamically (i.e. with JavaScript) as the user types her first and last name?
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17848751
Can anyone help me impliment this into my code?
0
 
LVL 16

Accepted Solution

by:
ellandrd earned 500 total points
ID: 17849494
yes - do this:

if(isset($_POST['MM_insert']))
{
          $isSubmitValid = true;
          $strMessage = "";
          $firstname = $_POST["firstname"];
          $lastname = $_POST["lastname"];
          $password = $_POST["password"];
          $conpassword = $_POST["conpassword"];

$firstname = $_POST["firstname"];
 $lastname = $_POST["lastname"];

$username = '';
$username .= $firstname.' '.$lastname;


/// rest of your code

then instead of using this:

GetSQLValueString($_POST['username'], "text"),

use this:

GetSQLValueString($username, "text"),

let me know if your stilling needing help!

ellandrd
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17849701
Excellent! that work a treat

Do you know what i need to add to my code so that all first names and last names will begin with a capital letter?


Thanks again!
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 17849719
2 secs - i'll write you a script...
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 17849929
here is an example:

<?php
$fn = 'sean';
$sn = 'delaney';

echo 'Firstname: '.$fn.'<br>';
echo 'Surname: '.$sn.'<br><br>';

$fn = strtoupper(substr($fn,0,1)).strtolower(substr($fn,1,strLen($fn)));
$sn = strtoupper(substr($sn,0,1)).strtolower(substr($sn,1,strLen($sn)));

echo 'Fullname: '.$fn.' '.$sn;
?>
0
 
LVL 1

Author Comment

by:satmanuk
ID: 17852561
thanks for your help.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question