Auto generate a field in form based on 2 other fields

satmanuk
satmanuk used Ask the Experts™
on
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>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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?

Author

Commented:
sorry what does that code do?

Author

Commented:
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.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Okay, so this should be done dynamically (i.e. with JavaScript) as the user types her first and last name?

Author

Commented:
Can anyone help me impliment this into my code?
Commented:
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

Author

Commented:
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!

Commented:
2 secs - i'll write you a script...

Commented:
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;
?>

Author

Commented:
thanks for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial