Solved

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

Posted on 2016-10-19
5
78 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 55

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 55

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 55

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

839 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