Solved

is it simple to change the ajax example code with jquery?

Posted on 2016-10-19
5
51 Views
Last Modified: 2016-10-26
Hello guys

I am studying ajax option to use in my site.

I have used some jquery example and this example doesn't use it.

Could you give me the example with javascript part make in jquery?

this is the example:  http://www.w3schools.com/php/php_ajax_database.asp

thanks
Alex
0
Comment
Question by:hidrau
  • 3
5 Comments
 
LVL 9

Expert Comment

by:Karen
ID: 41851313
it would be something like

$.get( "getuser.php", { q: str } )
.done(function( data ) {
$("#txtHint").html(data);
});

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41851520
Here is the same sample using jQuery
<html>
<head>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
  $('#users').change(function() {
    $.ajax({
      url: 'getuser.php?q=' + $(this).val(),
      type: 'GET',
    }).done(function(resp) {
      $('#txtHint').html(resp);
    });
  });
});
</script>
</head>
<body>

<form>
<select name="users" id="users">
  <option value="">Select a person:</option>
  <option value="1">Peter Griffin</option>
  <option value="2">Lois Griffin</option>
  <option value="3">Joseph Swanson</option>
  <option value="4">Glenn Quagmire</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

Open in new window

Working example here
The PHP script and DB used is the same as the one in the example
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41851527
Breaking down the changes

1. Change the <select>
  a) add id="users" so we can target it
  b) remove the onclick - we will bind to the change event with jQuery

2. Remove the showUser() function and replace with jQuery below

3. Add reference to the jQuery library
<script src="http://code.jquery.com/jquery.js"></script>

Open in new window


4. Create document ready function with event handler for change to do AJAX call
See comments inline below and refer the jQuery .ajax() documentation for more info
<script>
// DOCUMENT READY FUNCTION
$(function() {

  // BIND TO CHANGE EVENT ON <select>
  $('#users').change(function() {

    // MAKE THE ajax CALL
    // READ DOCUMENTATION FOR MORE OPTIONS
    $.ajax({
      // SET THE URL
      url: 'getuser.php?q=' + $(this).val(),

       // SET THE REQUEST TYPE
      type: 'GET',
    })
    // ASYNCH RESPONSE FROM SERVER ARRIVES HERE
    .done(function(resp) {
      // resp USER DEFINED PARAMETER NAME
      // HOLDS THE OUTPUT SENT BACK FROM
      // THE SERVER

      // WE SELECT THE txtHint BY ITS ID AND SET
      // ITS HTML TO THE resp
      $('#txtHint').html(resp);
    });
  });
});
</script>

Open in new window

0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 41861277
thanks a lot
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41861596
You are welcome.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now