Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Ajax and jQUERY

  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts

    Question Ajax and jQUERY

    Hi,

    I am trying to learn ajax with jquery.

    The following code is a simple ajax script that sends the values for 3 fields to another page search.php.

    Code:
     <script type="text/javascript">
        var http = false;
    
        if(navigator.appName == "Microsoft Internet Explorer") {
          http = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
    
    
          http = new XMLHttpRequest();
        }
    	var strkeyword= document.forms['myform'].txtKeyword.value;
    	var strdomain= document.forms['myform'].txtDomain.value;
    	var strengine = document.forms['myform'].txtEngine.value;
    
          http.abort();
          http.open("GET", "search.php?keyword="+ strkeyword +"&domain=" + strdomain + "&engine=" + strengine, true);
          http.onreadystatechange=function() {
      </script>


    Now question: Can this script be written in jQUERY? if yes how?

    I search on the jquery site but could not find anything that can help me.


    Thanx

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Did you look under the 'ajax' topic in the documentation?

    The basics of a get would be

    $.get('url',callback);

    eg.

    $.get( "search.php?keyword="+ strkeyword +"&domain=" + strdomain + "&engine=" + strengine, function(){});

    You could also write it as :

    $.get("search.php", {keyword:strkeyword,domain:strdomain,engine:strengine});

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    OK i got that but how do I display the search results via jquery?


    with normal Ajax function, it would be something like

    Code:
    document.getElementById('foo').innerHTML = http.responseText;

    Code:
    <div id="foo">Your results will be displayed here</div>

  • #4
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    $.get("search.php", {keyword:strkeyword,domain:strdomain,engine:strengine}, function(data)
    {
    $('#foo').html(data);
    });

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    you can also do:

    $('#foo').load("search.php?keyword="+ strkeyword +"&domain=" + strdomain + "&engine=" + strengine);

    if you just want the result of an ajax query to load into a div.


  •  

    Posting Permissions

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