Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Thanked 0 Times in 0 Posts

    newbie question- form gets submitted twice

    Hello I have written the following HTML page:
     <form name="search" method="get" action="b.php">
       <input type="text" name="query" id="query" onkeydown="if (window.event.keyCode == 13) { this.form.submit(); }" value=""></input>
    sometimes when I press 'Enter' in the textfield, the target page b.php gets called twice.
    Does anyone know what is causing it?
    sometimes, it gets called only once.

    I would appreciate any help in explaining what I am doing wrong

  • #2
    Master Coder
    Join Date
    Feb 2003
    Umeå, Sweden
    Thanked 83 Times in 74 Posts
    Well, it's pretty easy to explain: The enter key usually triggers form submission purely by HTML means. So it does this time as well, since you don't cancel out the default handling. However, you at the same time use the event to - by script - perform form submission, which means that you submit it twice. The fix is to return false in the event handler.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts