Auto-populating field?

Hi Guys,

I have a php form with a field called Email address, i also have another field called username

I would like to auto populate the username field with what has been entered into the email address field

i know how to pull the variable form the database…however in this case the field needs to auto populate before it is submitted to the database

is this possible? if not with php then is it possible with javascript

thanks

You’re wanting it to populate without submission, it’ll have to be javascript.

Something along the lines of (and i’m probably partly pseudocoding)


<input type='text' id='email'.....  onBlur='quack()'>

<script type='text\\javascript'>
function quack() {
 if(document.getElementById('username').value == '') {
    document.getElementById('username').value = document.getElementById('email').value;
 }
}
</script>

Sounds more like a job for JavaScript. But it sounds like you want the email address that is typed in to appear straight away in the username field. Why would you want that? It sounds like an annoyance to me.

Hi, i tried that, it doesnt seem to work

here is my current form code

<TR><TD CLASS="mainlightest" >Email Address *</TD><TD CLASS="mainlightest" ><INPUT TYPE="TEXT" NAME="email_address" value="EMAIL_ADDRESS" /></TD></TR>
<TR>
        <TD CLASS="mainlightest" >Username *</TD><TD CLASS="mainlightest" ><INPUT TYPE="TEXT" NAME="username" value="USERNAME" /></TD></TR>

<script type="text/javascript">
function quack() {
 if(document.getElementById('username').value == '') {
    document.getElementById('username').value = document.getElementById('email_address').value;
 }
}


</script>

You need to add id=“username” for you input field as well as email

As Gman said. If you look at my input field in the code, i specify an id attribute.

GetElementById works on… ID. not name.