0

I'm trying to call ajax() when users enter on input box without page load but it's calling this ajax method when users typing on input box. My input box is following :

<input type="text" name="search" id="txt_name"  placeholder="Keyword"/>

Jquery./ Ajax Code;

$(document).ready(function(){
  $("#txt_name").keyup(function(){

    var value = $('#txt_name').val();

    $.ajax({
        type:"post",
        url:"doSearchEnter.php",
        data :{
                'key' : value               
            },          
        success:function(res){
            $('#showSearchResult').html(res);
            }
       });

  });
});

How do I call I this ajax() after user enter on input box without page load ?

Update:

$('#txt_name').on('click', 'submit', function(e) {
    e.preventDefault();
    $.ajax({
        type:"post",
        url:"doSearchEnter.php",
        data :{
                'key' : value               
            },          
        success:function(res){
            $('#showSearchResult').html(res);
            }
       });
});

Update 2 :

<form>     
<input type="button" name="given_name" value="Given Name" class="search_submit" id="given_name"/>
<input type="button" name="company_name" value="Company Name" class="search_submit" id="company_name"/>
<input type="button" name="cdid" value="ID" class="search_submit" id="cdid"/>
<input type="text" name="search" id="txt_name"  placeholder="Keyword"/>    
</form>

$('form').on('click', 'submit', function(e){
    e.preventDefault();
});

$('form').on('keyup', '#txt_name', function(e){
    if(e.keyCode == '13'){
        $.ajax({
            type:"post",
            url:"doSearchEnter.php",
            data :{
                    'key' : value               
                },          
            success:function(res){
                $('#showSearchResult').html(res);
                }
           });
    }
});
4
  • use focusout() or keydown() event in jquery... Commented Jul 4, 2014 at 6:27
  • What does "without page load" mean? Commented Jul 4, 2014 at 6:29
  • "but it's calling this ajax method when users typing on input box" Right, that's what the keyup event relates to: The "up" part of a keystroke (the parts are keydown [which might repeat if the user holds it down and key repeat starts], keyup, and keypress [which might repeat]). If you want it to happen on another event, use another event. Commented Jul 4, 2014 at 6:29
  • @NikhilTalreja Without Page Load means if user press enter on input box it's should directly call ajax and show the result from php without page load. Commented Jul 4, 2014 at 6:40

4 Answers 4

3

Your question is a little unclear, but I think you want to perform ajax when the user presses the enter key on the input box? If so, you can check what key was pressed in the keyup handler and check if it was the enter key.

  $("#txt_name").keyup(function(event){
    if (event.keyCode == '13') {
      //...
    }
  });
Sign up to request clarification or add additional context in comments.

Comments

1

Placing the input inside of a form with a submit button should do the trick. When the user presses enter the form should submit. All you have to do is use

$('form').on('click', 'submit', function(e) {
    e.preventDefault();
    $.ajax({
        //do your Ajax thing here
     });
});

UPDATE: For three forms you would use the same Ajax call for all three and put your data together like so.

var formData = $(this).closest('form').serialize();
$.ajax({
    type: 'POST',
    url: 'test.php',
    data: formData
});

This would tell jquery to put the forms data in a proper format for passing to your server side script and would work for all three scripts

UPDATE2: UPDATE BASED ON UPDATED QUESTION:

$('form').on('submit', function(e){
    e.preventDefault();
});

$('form').on('keyup', '#txt_name', function(e){
    if(e.keyCode == '13'){
        $.ajax({
            //do Ajax stuff
         });
    }
});

here is link to the working code! jsfiddle

10 Comments

If I've 3 forms in my page then what happened ?
Use ids or classes to specify which ones you want or, better yet, make the data you are sending specifically to that form. That way you only have one Ajax call for three forms and it would work independently for each form:-)
Well, check my update question. It's now load full page when I enter on input box. Is there anything wrong ?
Look there is not submit button. I want load the ajax() when user enter on this Input Box without Page Load.
It is reloading when you press enter? And there is no submit button in your forms?
|
0

Dont use keyup(), use blur() instead.

Comments

0

Change the current anonymous function to run under different circumstances (i.e. different events), such as focusout() or blur()

$(document).ready(function(){
  $("#txt_name").on("blur",function(){

    var value = this.value;

    $.ajax({
        type:"post",
        url:"doSearchEnter.php",
        data :{
                'key' : value               
            },          
        success:function(res){
            $('#showSearchResult').html(res);
            }
       });

  });
});

2 Comments

But it's load the page when I enter on input box. I want to call this ajax without page load when I enter on input box.
do you know how do I prevent this page load when users enter on input box ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.