Solved

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

Posted on 2016-10-19
5
93 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 58

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 58

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 58

Expert Comment

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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 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…
Suggested Courses

636 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