473,473 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 28088
'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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JJ | last post by:
Hi All, When someone selects a value in a select on a form I want to show the same form with another select with certain values depending on first select. How can I do this? Should I keep...
2
by: fish | last post by:
Hi, I have an HTML page with a FORM and some input fields. On the fields I wish to do validation as the punters change the field values. If they get it wrong, then I tell them and then wish...
1
by: Cooper | last post by:
Hello, i have a form with TEXT, SELECT etc, elements. Simple, i suppose two TEXT (name: T1 and T2). If a user insert a bad value, it display a error message and turn back at form. My problem is:...
6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
4
by: Semi Head | last post by:
Hello folks, I'm looking for a script to validate a specific number value in a standard form input field. An example would be, if someone enters a number into a form input, I want the script to...
15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
1
by: tdmailbox | last post by:
Is there a vb command that can tell me if I have focus in the child or parent form? Basicly I have a search macro that needs me to have focus to any field in the parent form. If my focus is set...
4
by: GTi | last post by:
Is it possible to have a generic script that set the input focus on the first valid element in a document (not hidden or disabled) ? This script is at the end of a document, but don't work. ...
8
by: M.L. | last post by:
Hello. I created a form using JS validation with the form tag as follows: <form name="form1" action="dynaform.php" method="post" onsubmit="return pvg_sub();"> The js validation script sends...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.