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

asked on

Auto generate a field in form based on 2 other fields

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>
Avatar of VoteyDisciple
VoteyDisciple

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?
Avatar of satmanuk

ASKER

sorry what does that code do?
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.
Okay, so this should be done dynamically (i.e. with JavaScript) as the user types her first and last name?
Can anyone help me impliment this into my code?
ASKER CERTIFIED SOLUTION
Avatar of ellandrd
ellandrd
Flag of Ireland 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
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!
2 secs - i'll write you a script...
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;
?>
thanks for your help.