Page wont work when I add form tags!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gm04030276
    New Member
    • Nov 2006
    • 17

    Page wont work when I add form tags!

    Hey.
    I was trying to write a page yesterday where an admin can register a user. I wrote some javascript so that when the forename or surnames are off focus, a script is run to take the forename and surname, concatenate them and put them in the username box as a suggestion. The code worked fine until later when I added the form tags around the inputs (forgot to do it earlier). Code below:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Admin ~ Users</title>
    <link rel="stylesheet" href="../css/admin/users.css" />
    <script type="text/javascript">
    function username(){
    	if(document.getElementById('forename').value != ""){
    		if(document.getElementById('surname').value != ""){
    			name = document.getElementById('forename').value;
    			name = name + document.getElementById('surname').value;
    			name = name.toLowerCase();
    			document.getElementById('username').value = name;
    		}
    	}
    }
    </script>
    </head>
    
    <body>
    <div class='error'><?PHP echo str_replace("\'", "'", $_GET['error']); ?></div>
    <br />
    <form id="adduser" action="users_new_app.php" method="post">
    <table>
    <tr>
    	<th>Field</th>
    	<th>Value</th>
    </tr>
    <tr>
    	<th>Forename</th>
    	<td><input type="text" name="forename" size="50" id="forename" /></td>
    </tr>
    <tr>
    	<th>Surname</th>
    	<td><input type="text" name="surname" size="50" id="surname" /></td>
    </tr>
    <tr>
    	<th>Username</th>
    	<td><input type="text" name="username" size="50" id="username" onfocus="javascript:username();" /></td>
    </tr>
    ....
    </table>
    </form>
    </body>
    </html>
    I can't see anything wrong with it but it only doesn't work when the form tags are there, I took them away and it worked again...but naturally I need them to send the form!
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Change the name of the function to something else. eg suggestUsername ()

    It can't be same with an element name of a form.

    Comment

    • gm04030276
      New Member
      • Nov 2006
      • 17

      #3
      yip! that made it work! thank you sooo much! That was driving me nuts!

      So apparently you can't have a function with the same name as an element in your form...who knew!

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        yeah.. apparently... event I just came to know when I tried your code.

        Comment

        Working...