PDA

View Full Version : Resolved Having problem with focus()



Columbo
08-16-2009, 01:04 AM
I have used the following code to force cursor into the first field of a form but it doesn't work for some reason. The code does not flag any errors but it displays the form with no visible cursor. You have to click in the first field of the form to get the cursor. Can anyone suggest why?



<html>
<head>
<title>Data Entry Form</title>
</head>

<body onload="document.dataform.siteName.focus();">

<center>
<br /><br /><br /><br />
<table width="365px" height="200px" cellpadding="10" style="border: 1px solid #CC0000;">

<form name="dataform" method="POST" action="addsite.php">

<tr><td>
Site Name:
<INPUT type="text" name="siteName" size="40"> <br />
Username:
<INPUT type="text" name="user" size="10"> <br />
Password:
<INPUT type="text" name="password" size="20"> <br />
</td></tr>

<INPUT type="hidden" name="siteName" value="<?php echo $_POST['siteName']; ?>">
<INPUT type="hidden" name="user" value="<?php echo $_POST['user']; ?>">
<INPUT type="hidden" name="pass" value="<?php echo $_POST['password']; ?>">
<tr><td align="center">
<INPUT type="submit" value="Add to database" name="Button1">
<INPUT type="RESET" value="Reset">
</td></tr>

</form>
</table>
</center>
</body>
</html>


Thanks

Nile
08-16-2009, 02:28 AM
Try this:


<html>
<head>
<title>Data Entry Form</title>
</head>

<body onload="document.getElementById('siteName').focus();">

<center>
<br /><br /><br /><br />
<table width="365px" height="200px" cellpadding="10" style="border: 1px solid #CC0000;">

<form name="dataform" method="POST" action="addsite.php">

<tr><td>
Site Name:
<INPUT type="text" name="siteName" id="siteName" size="40"> <br />
Username:
<INPUT type="text" name="user" size="10"> <br />
Password:
<INPUT type="text" name="password" size="20"> <br />
</td></tr>

<INPUT type="hidden" name="siteName" value="<?php echo $_POST['siteName']; ?>">
<INPUT type="hidden" name="user" value="<?php echo $_POST['user']; ?>">
<INPUT type="hidden" name="pass" value="<?php echo $_POST['password']; ?>">
<tr><td align="center">
<INPUT type="submit" value="Add to database" name="Button1">
<INPUT type="RESET" value="Reset">
</td></tr>

</form>
</table>
</center>
</body>
</html>

It's because you have 2 name attributes with the value of siteName.

Columbo
08-16-2009, 03:04 AM
Ok,... the duplicate name="siteName" was the problem.
Thank you very much once again.

Nile
08-16-2009, 03:17 AM
Glad to help you! Your welcome!

It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"