Solved

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

Posted on 2016-10-19
5
83 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 56

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 56

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 56

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Worldmap 1 28
Use "if not" in a condition 2 18
Aspx calendar pop up control 3 27
Table header must be on top 2 25
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)
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…

762 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