Solved

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

Posted on 2016-10-19
5
70 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 54

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 54

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 54

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i Return this word's max repeat count? 2 19
Jquery 10 20
why $(this).attr('id'); function not returning value ? 9 38
Adding Row and Cell on a table 14 20
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 Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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)

810 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