Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you will improve your blogging platform by adding AJAX-based

ID: 3560617 • Letter: I

Question

In this assignment, you will improve your blogging platform by adding AJAX-based functionality to two of your pages. After the user leaves the email field, check against the database to see if the email is already registered to an existing account. If it is, display an appropriate error message on the page (without refreshing the page). When a user submits the comment form, add the new comment to the comments section (without refreshing the page). The comment form should return to a blank state after the form is submitted. Responses from the server side should be provided in the form of valid JSON objects. All JavaScript event processing should be registered using the DOM 2 method.

Explanation / Answer

Back-end coding to support AJAX requests

// Some functions that work with a database.
include "suggestions.lib.php";
// Get the query result.
$query = $_REQUEST["query"];
$result = get_suggestions($query);
// Give a name to the first element of result.
$result["first"] = $result[0];
$GLOBALS['_RESULT'] = $result; // $_RESULT will be passed to frontend

Properly formatted JSON objects holding the updated data

if (Integer.parseInt(res) == 1) {
pDialog.setMessage("Loading User Space");
pDialog.setTitle("Getting Data");
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
/**
* Clear all previous data in SQlite database.
**/
UserFunctions logout = new UserFunctions();
logout.logoutUser(getApplicationContext());
db.addUser(json_user.getString(userFirstName), json_user.getString(userLastName), json_user.getString(userEmail));
/**
*If JSON array details are stored in SQlite it launches the User Panel.
**/
Intent upanel = new Intent(getApplicationContext(), MainActivity.class);
upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pDialog.dismiss();
startActivity(upanel);
/**
* Close Login Screen
**/
finish();
logout user:

public boolean logoutUser(Context context){
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return true;
}

adduser:

public void addUser(String userFirstName, String userLastName, String userEmail) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, userFirstName); // FirstName
values.put(KEY_LASTNAME, userLastName); // LastName
values.put(KEY_EMAIL, userEmail); // Email


Front-end coding to produce AJAX requests

// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
}

// Send a query.
JsHttpRequest.query(
"/ajax/suggester", // backend address
{ query: search_string.value }, // parameters
function(result, errors) {
// This function will be called when result is ready.
// Suggest the most popular word as query.
autocomplete_query(result["first"]["name"]);
// Draw "suggestions" to query.
show_suggestions (result);
}
);