Hi all,
busy day for me on here!.. here\'s another problem I had today:
Have coded a pretty nice AJAX enabled form, which basically takes a value entered into a text field, queries a mySQL database, and returns a bunch of data regarding whatever text is entered.
All was fine and dandy up until this point, it was simple enough to shove an \'onclick\' onto a form button ()
Though what if (and I almost always do this) my user decides to use the enter/return key to submit the form?
The code then decided to use the normal form.submit action, and kept throwing me back to my index page, obviously not as I\'d have hoped.
So I knew I had to somehow override the enter key\'s functionality for this form, but how? Well here goes.
Every time someone presses a key while focussed on the text field, I\'d run a command and check whether that key was \'return\'.. if it was, I\'d execute the relevant AJAX function, halt the default form action, and be done! otherwise I\'d just let the character appear, so my script would be completely transparent.
Here\'s the code for the input field (HTML):
[code]<input onkeydown=\"return entSub(event,this.form,\'{$nav->script}/e/userCheck\');\" type=\"text\" name=\"text\" />[/code]
the \'{$nav->script}/e/userCheck\' parameter of the function is the parameter I need to pass to my AJAX function, obviously yours will differ to this.
Now for the javascript \'entSub\' code...
[code]function entSub(event,formName,xmlPostParam) {
// Enter funcion with event (i.e. what happened to make this function execute).. the form\'s name.. and whatever we\'re passing to xmlhttpPost //
if (event && event.which == 13)
// if event exists, and is 13 (enter key being pressed)...
{
// call xmlHttpPost using the parameter submitted...
xmlhttpPost(xmlPostParam);
// and DON\'T submit the form using normal methods
return false;
}
// if our event is -not- the enter key being pressed..
else
{
// display the character entered in the text field..
return true;
}
}[/code]
As always, probably more elegant ways of accomplishing this, but this code suited my application the most...
enjoy!
Using enter key to submit AJAX Query
Get Help With All Aspects of Web Design & Design Software
Moderators: Moderator, Global Moderator
Using enter key to submit AJAX Query
Last edited by ner0 on Sat Mar 17, 2007 7:45 pm, edited 1 time in total.
[color=\"green\"]'class KPIM::ProcessManager' only defines private constructors and has no friends[/color] <-
poor class
[18:09:00] * ~Moppy stews ner0 erotically 1 times.
[18:09:00] * ~Moppy stews ner0 erotically 1 times.
Return to “Web Design Principles”
Jump to
- General Category
- ↳ KillaNet Country
- ↳ 2D Graphics
- ↳ KillaNet News
- ↳ 3D & Animation
- ↳ Chatroom
- ↳ Flash
- ↳ Help & Suggestions
- ↳ Game Dev
- ↳ Audio & Video
- ↳ Introductions
- ↳ Journalism
- ↳ Phoenix Lounge
- ↳ Application Dev
- ↳ Toga Toga Toga!!!
- ↳ Photography
- ↳ Main Street Archives
- ↳ Web Design
- ↳ Fonts Icons Cursors & Screensavers
- ↳ Book Reviews
- ↳ General
- ↳ Tech Industry News
- ↳ Legal Resources
- ↳ Industry Contests
- ↳ Graphix Battle Arena
- ↳ KillaNet Contests
- ↳ Competition Archives
- ↳ Education Information
- ↳ Careers
- ↳ Game Studies
- ↳ Motivation
- ↳ Conferences & Seminars
- ↳ KillaDesign
- ↳ Animation & Film
- ↳ The Studio
- ↳ Game Development
- ↳ 2D Graphics
- ↳ Audio & Video
- ↳ 3D Graphics
- ↳ Flash
- ↳ Fonts, Icons & Emoticons
- ↳ Design Requests
- ↳ PhotoShop
- ↳ Cinema 4D
- ↳ Bryce
- ↳ Flash
- ↳ Paint Shop Pro
- ↳ Blender
- ↳ Poser
- ↳ The Darkroom
- ↳ Photo & Camera Discussion
- ↳ Photo Journalism
- ↳ Web Design Principles
- ↳ PHP & MySQL
- ↳ Designing For Print
- ↳ Writer\'s Desk
- ↳ Fiction Writing
- ↳ News Journalism
- ↳ Poetry
- ↳ Technical Writing
- ↳ Biographical Writing
- ↳ General Writing Resources
- ↳ Promotional Writing
- ↳ Film & Television
- ↳ VideoGames
- ↳ Computer Department
- ↳ General Discussion
- ↳ Windows Help
- ↳ Linux Help
- ↳ Builds & Mods
- ↳ Geek Gadgets
- ↳ Security
- ↳ Dev Discussion
- ↳ C++
- ↳ Visual Basic
- ↳ Java
- ↳ Application Skinning
- ↳ IRC Scripting
- ↳ Gaming Centre
- ↳ Guild Wars
- ↳ Aion
- ↳ Computer
- ↳ World of Warcraft
- ↳ Nintendo
- ↳ General RPG & MMORPG
- ↳ PlayStation
- ↳ XBox
- ↳ Other Consoles
- ↳ All Action
- ↳ Racers
- ↳ Sports
- ↳ FPS
- ↳ RTS
- ↳ Sim
- ↳ Casual Games
- ↳ Kids' Games
- ↳ Mobile Games
- ↳ Retro Games
- ↳ Challenges, Friendly Taunts & Discussion
- ↳ Game Requests & Bug Reports
- ↳ Open Source Games
- ↳ Puzzle Games
- ↳ HeadQuarters
- ↳ Uber Coffee Room
- ↳ KillaNet
- ↳ Coffee Room
- ↳ KillaNet
- ↳ KillaGraphix
- ↳ KillaHosting
- ↳ Marketing etc.
- ↳ Staff Issues
- ↳ Reference & Software
- ↳ Archives
- ↳ KillaBlogs
- ↳ Journalism
- ↳ Trash Can
