470,849 Members | 1,036 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Form Field Set Focus?

Greetings,

I'm still something of a newbie to html/php/mysql.

I have a php/html form with several fields of type "input". If the user
enters improper data in a particular field and clicks the Submit button,
the php script has to trap it and return a javascript error message box
with an OK button. That part I've already got down.

But, you know how in Visual Basic you can set the focus on the offending
field on a form, so if the user gets a Message Box with an error message
and clicks OK, the form goes something like,

myForm!myField.SetFocus

Is there an equivalent way in php/html way do this?

Jul 17 '05 #1
5 27797
'bonehead wrote:
Greetings,

I'm still something of a newbie to html/php/mysql.

I have a php/html form with several fields of type "input". If the
user enters improper data in a particular field and clicks the Submit
button, the php script has to trap it and return a javascript error
message box with an OK button. That part I've already got down.

But, you know how in Visual Basic you can set the focus on the
offending field on a form, so if the user gets a Message Box with an
error message and clicks OK, the form goes something like,

myForm!myField.SetFocus

Is there an equivalent way in php/html way do this?


This is not a PHP issue... PHP is rendered totally server side, and has no
control over the client like that. You are looking for some sort of
client-side scripting - either way, it's not a PHP thing.
Jul 17 '05 #2
While the city slept, 'bonehead <se*********@here.org> feverishly typed:
Greetings,

I'm still something of a newbie to html/php/mysql.

I have a php/html form with several fields of type "input". If the
user enters improper data in a particular field and clicks the Submit
button, the php script has to trap it and return a javascript error
message box with an OK button. That part I've already got down.

But, you know how in Visual Basic you can set the focus on the
offending field on a form, so if the user gets a Message Box with an
error message and clicks OK, the form goes something like,


As already stated, you need to use a client-side technology to *attempt
to*[1] set the focus, which is quite easy in Javascript (something along the
lines of document.yourFormName.yourFieldName.focus();). However, you have to
be careful, as the user may have started typing into other fields in the
form while the page is still loading, then when the page loads and your
chosen field gets focus, the user may not realise and continue typing -
unaware that they are now typing nonsense into the wrong field. I set up
this page: http://www.nigenet.org.uk/bits-n-bob...FocusTest.html a while
ago when this issue came up over in news:alt.html. It has 3 text fields, and
when the page is loaded it will set the focus to the first one, unless the
user has already started using any of the fields, in which case it doesn't
set the focus. The so-called "huge image to slow the loading process"
probably doesn't apply in this day of ever-spreading broadband usage - maybe
I should set up a special broadband version with the full size image on? ;-)

Anyway, take a look at the code and see if you can adapt it to your needs.
You can use PHP to write out the javascript to set the field, so it sets to
whichever is the offending field. Something along the lines of:

<?php
// Your form validation stuff here
$badfield = // set this up to be the name of the field you want to set the
focus to
?>

<script type="text/javascript">
<!-- Hide

// some javascript stuff here... see the source in my example page

<?
echo("document.yourFormName.$badfield.focus();\n") ;
?>

// dunhidin -->
</script>

[1] This will only work if javascript is available to the end-user, and
enabled. The same goes for your javascript validation script, so always
validate on the server-side as well.

Hope that helps,
Nige

--
Nigel Moss.

Email address is not valid. ni***@nigenetDOG.org.uk. Take the dog out!
http://www.nigenet.org.uk | Boycott E$$O!! http://www.stopesso.com
In the land of the blind, the one-eyed man is very, very busy!
Jul 17 '05 #3
I noticed that Message-ID: <c7************@ID-112325.news.uni-berlin.de>
from nice.guy.nige contained the following:
Greetings,


Hi nige,

I'm beginning to worry about the links being forged between
uk.rec.humour and PHP...

--
black-dog

A dog does not care about the previous dogs in your life.
Jul 17 '05 #4
Thanks! This looks great...I'll try it.

nice.guy.nige wrote:
<?php
// Your form validation stuff here
$badfield = // set this up to be the name of the field you want to set the
focus to
?>

<script type="text/javascript">
<!-- Hide

// some javascript stuff here... see the source in my example page

<?
echo("document.yourFormName.$badfield.focus();\n") ;
?>

// dunhidin -->
</script>


Jul 17 '05 #5
While the city slept, Geoff Berrow <bl******@ckdog.co.uk> feverishly typed:
I noticed that Message-ID:
<c7************@ID-112325.news.uni-berlin.de> from nice.guy.nige
contained the following:
Greetings,


Hi nige,

I'm beginning to worry about the links being forged between
uk.rec.humour and PHP...


What's the difference between PHP? One of it's default settings is both the
same! ;-)

Cheers,
Nige

--
Nigel Moss.

Email address is not valid. ni***@nigenetDOG.org.uk. Take the dog out!
http://www.nigenet.org.uk | Boycott E$$O!! http://www.stopesso.com
In the land of the blind, the one-eyed man is very, very busy!
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by JJ | last post: by
2 posts views Thread by fish | last post: by
1 post views Thread by Cooper | last post: by
6 posts views Thread by Charles Banas | last post: by
4 posts views Thread by Semi Head | last post: by
4 posts views Thread by GTi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.