Question

How can I use AJAX request in this code? This code is a part of an...

How can I use AJAX request in this code?

This code is a part of an web application using AJAX that allows users to browse music albums.

/** Load the list of albums */
function listAlbums() {
// TODO make an AJAX request to /albums
// then populate the "albums_list" list with the results
}
/** Show details of a given album */
function showAlbum(album_id) {
// TODO make an AJAX request to /albuminfo with the selected album_id as parameter (i.e., /albuminfo?album_id=xxx),
// then show the album cover in the "album_cover" div and display the tracks in a table inside the "album_songs" div
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

This example require jquery2.1.1, PHP and Database.

Following are components of the examples

1. Database table structure.

CREATE TABLE AlbumMaster (
id number NOT NULL,
album_name` varchar(255) NOT NULL
)

2. One index.php where user would search albums.

3. readAlbum.php contains the business logic to query database based on the user input.

4. dbcontroller.php provides the database connectivity.

index.php

<html>
<head>
<TITLE>Album Search Example</TITLE>
<head>
<style>
body{width:610px;}
.frmSearch {border: 1px solid #a8d4b1;background-color: #c6f7d0;margin: 2px 0px;padding:40px;border-radius:4px;}
#album-list{float:left;list-style:none;margin-top:-3px;padding:0;width:190px;position: absolute;}
#album-list li{padding: 10px; background: #f0f0f0; border-bottom: #bbb9b9 1px solid;}
#album-list li:hover{background:#ece3d2;cursor: pointer;}
#search-box{padding: 10px;border: #a8d4b1 1px solid;border-radius:4px;}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
   $("#search-box").keyup(function(){
       $.ajax({
       type: "POST",
       url: "readAlbum.php",
       data:'keyword='+$(this).val(),
       beforeSend: function(){
           $("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
       },
       success: function(data){
           $("#suggesstion-box").show();
           $("#suggesstion-box").html(data);
           $("#search-box").css("background","#FFF");
       }
       });
   });
});

function selectAlbum(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
</head>
<body>
<div class="frmSearch">
<input type="text" id="search-box" placeholder="Album Name" />
<div id="suggesstion-box"></div>
</div>
</body>
</html>

readAlbum.php

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["keyword"])) {
$query ="SELECT * FROM AlbumMaster WHERE album_name like '" . $_POST["keyword"] . "%' ORDER BY album_name LIMIT 0,6";
$result = $db_handle->runQuery($query);
if(!empty($result)) {
?>
<ul id="album-list">
<?php
foreach($result as $album) {
?>
<li onClick="selectAlbum('<?php echo $album["album_name"]; ?>');"><?php echo $album["album_name"]; ?></li>
<?php } ?>
</ul>
<?php } } ?>

dbcontroller.php

<?php
class DBController {
   private $host = "localhost";
   private $user = "root";
   private $password = "";
   private $database = "phppot_examples";
   private $conn;
  
   function __construct() {
       $this->conn = $this->connectDB();
   }
  
   function connectDB() {
       $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
       return $conn;
   }
  
   function runQuery($query) {
       $result = mysqli_query($this->conn,$query);
       while($row=mysqli_fetch_assoc($result)) {
           $resultset[] = $row;
       }      
       if(!empty($resultset))
           return $resultset;
   }
  
   function numRows($query) {
       $result = mysqli_query($this->conn,$query);
       $rowcount = mysqli_num_rows($result);
       return $rowcount;  
   }
}
?>

Add a comment
Know the answer?
Add Answer to:
How can I use AJAX request in this code? This code is a part of an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON PROGRAMMING NEED HELP ASAP You will write an application to manage music collections -- a music collection is a l...

    PYTHON PROGRAMMING NEED HELP ASAP You will write an application to manage music collections -- a music collection is a list of albums. The named tuples used for Albums and Songs are defined below, and an example of a music collection is given. (DO NOT HARDCODE THE VALUES FOR MUSIC!) Album = namedtuple('Album', 'id artist title year songs') Song = namedtuple('Song', 'track title length play_count') MUSIC = [ Album("1", "Peter Gabriel", "Up", 2002, [Song(1, "Darkness", 411, 5), Song(2, "Growing Up",...

  • HTML: ch14 IC_1B - use a fadeToggle to control jQuery to show the second part of...

    HTML: ch14 IC_1B - use a fadeToggle to control jQuery to show the second part of the content on Chp 14 slide 33. <!DOCTYPE html> <html lang="en"> <head>    <title> jQuery </title>    <meta charset="utf-8">    <style>        #details { display: none; }    </style>       <xxxx src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></xxxx>       </head> <body>    <h1>jQuery</h1>        <p>Many websites, including Amazon and Google, use jQuery, to provide interaction and dynamic effects on web pages.        <a href="#" id="more">...

  • need help to complete the task: app.py is the Python server-side application. It includes a class...

    need help to complete the task: app.py is the Python server-side application. It includes a class for representing the list of albums. You need to complete the missing parts such that it loads data from the data/albums.txt and data/tracks.txt files. You can decide what internal data structure you want to use for storing the data. It is already implemented that a single instance of the Albums class is used, so that loading from the files happens only once (and not...

  • Please advise what is being doing wrong. Requirement. See if you can modify the code of...

    Please advise what is being doing wrong. Requirement. See if you can modify the code of Listings 17.1 and 17.3 to present a message to the user while waiting for an Ajax request to complete. //Code <!DOCTYPE html> <html> <head> <title>Keywords Grabber</title> <script src="myAjaxLib.js"></script> <script> function display(content) { document.getElementById("displaydiv").innerHTML = content; </script> <script> function getXMLHttpRequest() { try { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { return new ActiveXObject("Msxml2.XMLHTTP"); } } catch(e) { return new XMLHttpRequest(); } } function doAjax(url,...

  • Question: How can this code be rewritten without using array? Code in action here: After Inserting...

    Question: How can this code be rewritten without using array? Code in action here: After Inserting number: Full code is here: How would I rewrite this without using built in array or an array? C nked list ← ⓘ솔JSFiddle Ltd (GB) https:/JSfiddle.net/7m80w1c5/2/ .Most Visited @ Getting StartedのHP-See what's Hot Suggested Sites . Comments for The Ch web Slice Gallery C New Tab 60 D> Run 9 Update Fork Tidy Collaborate Embed v/ Settings <form> iddle Meta HTML Insert Number Here:<br>...

  • I have the code buts its not working as this below instruction can anyone help Use...

    I have the code buts its not working as this below instruction can anyone help Use the requirements from the pizza part of Assignment 6 and do the following using CSS and an internal style sheet: 1.     Your Javascript function should be in an external file (don’t forget to move it up to studentweb too) 2.     Put a red border of 300px in width around the form 3.     There should be three Div sections: header, main_body, and footer. 4.     Center the name of your...

  • Create a new project in BlueJ and name it LastName-lab8-appstore, e.g., Smith-lab8-appstore. If your App class from Lab 4 is fully functional, copy it into the project. (You can drag a file from File...

    Create a new project in BlueJ and name it LastName-lab8-appstore, e.g., Smith-lab8-appstore. If your App class from Lab 4 is fully functional, copy it into the project. (You can drag a file from File Explorer onto the BlueJ project window.) Otherwise, use the instructor's App class: Create a new class using the "New Class..." button and name it App. Open the App class to edit the source code. Select and delete all the source code so that the file is...

  • Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based...

    Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based on the number of user enters, to find out how many prime numbers there are up to and including the user inputted number and then display them on the web page. The following are the detailed steps to complete this assignment: Step 1. [30 points] In “find_primeV2.js”, complete isPrime() function by (1) Adding one parameter in function header. That parameter is used to accept...

  • i need help with this homework webpage and code too: The webpage must include: Global Structure...

    i need help with this homework webpage and code too: The webpage must include: Global Structure Tags Text Tags Images   Your first page must be named index.html Include a table within your site, challenge yourself to add a header in the table, and choose interesting border and/or cell formatting. Include a hyperlink within your site. Include an image in your site. Include a form in your site to demonstrate understanding of a variety of form components (the form does not...

  • Java Inventory Management Code Question

    Inventory ManagementObjectives:Use inheritance to create base and child classesUtilize multiple classes in the same programPerform standard input validationImplement a solution that uses polymorphismProblem:A small electronics company has hired you to write an application to manage their inventory. The company requested a role-based access control (RBAC) to increase the security around using the new application. The company also requested that the application menu must be flexible enough to allow adding new menu items to the menu with minimal changes. This includes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT