How to enable ENTER KEY in a Jquery ajax form

I got the script from a blog I managed making it work but the user is forcerd to click on the link next to the input field in order to submit the data.

Can you please help on getting the Enter key working also to submit the form?


Thanks
[SCRIPT THAT SUBMITS THE FORM]

// JavaScript Document



$(document).ready(function(){
		
//add a name
   $("a.sendName").click(function(){
		
		var name = $("#name").val(); // getting values from the ID  --id="message" --  name of the form fields
		var ts = $("#ts").val(); 
		
		$.post("implement/i.Name.php", { name: name, ts: ts }, //this is JSON that gets posted
		 function(data){
            $("#loadName").html(data).slideDown("slow"); // send this data from the PHP echo into the #screen DIV
            $("#update").fadeIn("slow");
        });
    });


}); //end DOC




[THE ACTUAL FORM]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jgo.js"></script>
<title>Untitled Document</title>
</head>

<body>
What is your Name<br />
<input name="name" type="text"  id="name" size="22" />
<input name="ts" id="ts" type="hidden" value="<?php echo date('l jS \of F Y h:i:s A');?>" /><br />

<a href="#" class="sendName">add my name</a>

<div id="loadName" style="display:none"></div><br />


</div>
</body>
</html>

Open in new window

playacarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tdotCommented:
var code = (e.keyCode ? e.keyCode : e.which);
 if(code == 13) { //Enter keycode
    submit()
 }
0
tdotCommented:

use this instead much clearer..



    $("form").keypress(function(e){
        if(e.which==13){
             $('form').submit();
       }    
});

0
tdotCommented:
I HAVE ADDED IT TO YOUR CODE  HERE


$(document).ready(function(){
               
//add a name
   $("a.sendName").click(function(){
               
                var name = $("#name").val(); // getting values from the ID  --id="message" --  name of the form fields
                var ts = $("#ts").val();
               
                $.post("implement/i.Name.php", { name: name, ts: ts }, //this is JSON that gets posted
                 function(data){
            $("#loadName").html(data).slideDown("slow"); // send this data from the PHP echo into the #screen DIV
            $("#update").fadeIn("slow");
        });
    });


 $("form").keypress(function(e){ /// WHEN ENTER KEY CLICKED FORM WILL SUBMIT
        if(e.which==13){
             $('form').submit();
       }    
});


}); //end DOC

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

playacarAuthor Commented:

I tried pasting it in the main scrip but doesn't do anything.

Please note that there is no FORM tag.

I did add The form tag, it responds to the enter key but data does not submit.

I need both submit method working the Click and the Return Key.

I see line 10 has the click() function, can that be modified instead?
0
playacarAuthor Commented:
Still, enter key does not submit the form as it should.
I added a regular Form tag and  the ID to match the
script, I works but it looses the ajax efect it reloads
the entire page.
0
playacarAuthor Commented:

Here is the source of the scripts

http://fluxux.com/tech/132

of course i have modified it and entered the mysql security stuff. As I said before it is working but cant use the enter key.

I was looking for an easy way understand on how to submit a form using ajax and get the results back to my page to continue further processing of the mysql query

I know this is very basic but i am learning by example.

or maybe you can point me to some other resources.

Thanks
0
tdotCommented:
If the form tag is added this should work now.

$(document).ready(function(){
               
//add a name
   $("a.sendName").click(function(){
               
                var name = $("#name").val(); // getting values from the ID  --id="message" --  name of the form fields
                var ts = $("#ts").val();
               
                $.post("implement/i.Name.php", { name: name, ts: ts }, //this is JSON that gets posted
                 function(data){
            $("#loadName").html(data).slideDown("slow"); // send this data from the PHP echo into the #screen DIV
            $("#update").fadeIn("slow");
        });
    });


 $("form").keypress(function(e){     /// WHEN ENTER KEY CLICKED FORM WILL SUBMIT
        if(e.which==13){
             $('a.sendName').trigger('click');
         }    
  });
 
});
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.