Page 1 of 1

How to set focus to fields in error on input form??

Posted: Mon Jun 20, 2005 8:07 am
by ramstan
Hi people,

I have a php/html form where the user fills in fields, the the form is submitted, the data validated and if all is OK the data is stored in SQL database. No problem.

Obviously, if the data the user has entered fails vaildation then the data will not be saved. The input screen is redisplayed as the user entered - and an error message displayed indicating the field in error. The only thing I would like to do and can't work out how to do is set focus to the field in error at this point.

PS: All the processing here is done in one php script.
PPS: I do know about the focus() function but I guess I need to know where to use this.

cheers,
ramstan

Posted: Mon Jun 20, 2005 8:11 am
by Syranide
First, off, this isn't about PHP, it's client side or some other forum.
Second, try MSDN (msdn.microsoft.com), has information about pretty everything you want to know about jscript or just try google (as you knew the function name)

Third, you assign the field an ID (id="foo") then you use foo.focus()... HOWEVER, do note that this cannot be done before the control is created, even if it works for You, it will not work for other people, so make sure it is done when the page is loaded, not loading.

Posted: Mon Jun 20, 2005 8:22 am
by CoderGoblin
This topic should be in Client Side (as it is only possible using Javascript)... of which we do answer questions, although not our area of "expertise".

During PHP validation you need to store the name of the form field you wish to have the focus on. You need to ensure that this focus is the "first" error on the form. (I normally perform validation in reverse order so I can simply overwrite the value. :wink:

If you have an error you then need to set the focus,
instead of outputting

Code: Select all

<body>
in HTML simply output

Code: Select all

<body onload=&quote;tagname.focus();&quote;>
If the php code is structured well (i.e not input/processing/output all over the place) this is easily achieved.

Hope that helps.

Posted: Mon Jun 20, 2005 9:17 am
by ramstan
Well excuse me for posting something in the wrong letterbox. As it's a php function I did't think I was far wrong.

I've tried the <body onload="name.focus();"> but to no avail.

OK thanks, don't worry I'll go and bother some client-side kids.

Posted: Mon Jun 20, 2005 9:19 am
by JayBird
Moved to Client Side

Try this !

Posted: Mon Jun 20, 2005 9:30 am
by asmie
Hie,

You can write a javascript function that will be called on the onLoad event.

<script language="JavaScript">
function callonload(control)
{
document.all(control).focus();
}

</script>


This function has to be called like this :

<BODY onload="callonload()">

You can also assign the field where the focus has to be set using a hidden variable and in the onload it will be something like this :

<script language="JavaScript">
function callonload()
{
var control = document.form1.myhiddenfield.value;
document.all(control).focus();
}
</script>




Hope this helps.

:)
Asmie

Posted: Mon Jun 20, 2005 9:41 am
by ramstan
Thanks very much Asmie - that looks like the ticket to me.

ramstan