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!