Form submitting with Enter and Focus on form objects

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jaspreet
Forum Newbie
Posts: 2
Joined: Fri Nov 12, 2004 1:39 am
Location: Dubai
Contact:

Form submitting with Enter and Focus on form objects

Post by jaspreet »

Hi,my question has 2 parts
  1. I am trying to submit a form in PHP using Enter key, it doesn't work here is the code:

    Code: Select all

    <head>
    <script language="Javascript">
    function keypress(myform,e){if(e && e.which){
     e = e;
     characterCode = e.which; }
    else{ e=event;
     characterCode = e.keyCode; }
    if(characterCode == 13){ 
    myform.submit();
    // return false; }
    else{ return true; }}
    </script>
    </head>
    
    <body>
    <form method="post" name="form1" action="<?php print "$_SERVERїPHP_SELF]";?>">
    <center>Please Select a <?php print "$criteria<br><br>" ?> 
     <select name="choice" size="8" Style="width:150px" onkeypress="return keypress(this.form,event)" > 
    </select></center>
    
    <input type="submit" name="submit" value="Submit">&nbsp
    <input type="submit" name="stop" value=" Show Items ">
    <script language="Javascript"> document.form1.choice.focus();</script>
    
    </body>
    I get an error for myform.submit() saying “Object doesn’t support this property or method”

    I changed the call to keypress, as onkeypress =“return keypress(this.form1,event)”
    Since my form is named form1, now it gives an error “submit is null or not an object”.
  2. My page loads with the focus on the "Select" object. After I submit my page will reload, and I may get another form similar to the look of the one for which code is given or I may get an output. In either case after submitting, if I press the back button to come back to the previous page, my "Select" object looses focus, and I am unable to use the arrow keys to move. PS: The focus is fine when I load the page for the first time, and even on subsequent submits the new page also has focus on the "Select" object (if that is what is loaded) the only problem comes when I go BACK.
Weirdan | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
AnarKy
Forum Contributor
Posts: 119
Joined: Tue Nov 02, 2004 1:49 am
Location: South Africa

For the 2nd Part.

Post by AnarKy »

For the 2nd part of the question:

You may use the onload function to place the focus
on the select box,

http://www.codetoad.com/javascript/misc ... _event.asp
has a good example if you need it.

Hope this works,

AnarKy.
jaspreet
Forum Newbie
Posts: 2
Joined: Fri Nov 12, 2004 1:39 am
Location: Dubai
Contact:

Problem with onload()

Post by jaspreet »

onload() is a good option but the problem is that my page is a php page and the select object may or may not be included (i.e. the form may or may not be loaded). The first time the page loads the form will be displayed for sure. But after that it depends upon user selection, the next page may again be a form with the "Select" object on it or may be an output.

Also I don't understand how can onload work otherwise, becoz for that the object should already be present or should have been loaded. If I try focusing on the "Select" object even before it has loaded it will give me an error.


And anyone with an answer to the first question I had put, I still do not know what to do with the enter key problem??
Post Reply