Solved

Auto generate a field in form based on 2 other fields

Posted on 2006-10-31
10
198 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now