Form Field Set Focus?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 'bonehead

    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?

  • Agelmar

    #2
    Re: Form Field Set Focus?

    'bonehead wrote:[color=blue]
    > 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?[/color]

    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.


    Comment

    • nice.guy.nige

      #3
      Re: Form Field Set Focus?

      While the city slept, 'bonehead <senmenospam@he re.org> feverishly typed:
      [color=blue]
      > 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,[/color]

      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.yourFo rmName.yourFiel dName.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.$b adfield.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. nigel@nigenetDO G.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!


      Comment

      • Geoff Berrow

        #4
        Re: Form Field Set Focus?

        I noticed that Message-ID: <c72jlj$gukr5$1 @ID-112325.news.uni-berlin.de>
        from nice.guy.nige contained the following:
        [color=blue][color=green]
        >> Greetings,[/color][/color]

        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.

        Comment

        • 'bonehead

          #5
          Re: Form Field Set Focus?

          Thanks! This looks great...I'll try it.

          nice.guy.nige wrote:
          [color=blue]
          > <?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.$b adfield.focus() ;\n");
          > ?>
          >
          > // dunhidin -->
          > </script>[/color]

          Comment

          • nice.guy.nige

            #6
            OT: humour/PHP (was Re: Form Field Set Focus?)

            While the city slept, Geoff Berrow <blthecat@ckdog .co.uk> feverishly typed:
            [color=blue]
            > I noticed that Message-ID:
            > <c72jlj$gukr5$1 @ID-112325.news.uni-berlin.de> from nice.guy.nige
            > contained the following:
            >[color=green][color=darkred]
            >>> Greetings,[/color][/color]
            >
            > Hi nige,
            >
            > I'm beginning to worry about the links being forged between
            > uk.rec.humour and PHP...[/color]

            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. nigel@nigenetDO G.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!


            Comment

            Working...