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
How to set focus to fields in error on input form??
Moderator: General Moderators
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.
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.
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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.
If you have an error you then need to set the focus,
instead of outputting
in HTML simply output
If the php code is structured well (i.e not input/processing/output all over the place) this is easily achieved.
Hope that helps.
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.
If you have an error you then need to set the focus,
instead of outputting
Code: Select all
<body>Code: Select all
<body onload="e;tagname.focus();"e;>Hope that helps.
Try this !
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
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