Updating contact list to a database table structure

Currently, my code displays the data in address books, but I would like it to be shown in a table instead. I attempted to use document.write to create the table, but I'm unsure how to populate the table with the data rather than the address book format. How can I achieve this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Address Book</title>
  <style type="text/css">
      .ab {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: small;
    color: #993300;
    background-color: #CCFFCC;
    padding: 5px;
    height: 100px;
    width: 350px;
    border: thin dotted #993300;
    }
  </style>

  <script type="text/javascript">
    function addressBookItem (fname, lname, email) {
        this.fname= fname;
        this.lname = lname; 
        this.email = email;
    }
    var x = document.createElement("TABLE");
    addressBookItem.prototype.write = function() {
        // var adrbook = "<p class='ab' First Name: " + this.fname + "&ltbr /&gt";
        var adrbook = "<p class='ab'>First Name: " + this.fname + "<br />";
        adrbook += "Last Name: " + this.lname + "<br />";
        adrbook += "Email Address: " + this.email + "</p>";

        document.write(adrbook);
    }

</script>
</head>
<body>
    <script type="text/javascript">
        var aB1 = new addressBookItem('Roger', 'Williams', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e89a9f8184848189859ba88f85898184c68b87'); // [partial email address]'
        var aB2 = new addressBookItem ('Rose', 'Schultz', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99ebf6eafcc6ead9fcf8ebedf1f5f0f7f2b7f7fc'); // [partial email address]'
        document.write("<table border=\"2\"><tr><th>First Name</th><th>Last Name</th><th>Email Address</th></tr>");
        aB1.write();
        aB2.write();
        document.write("</table");
    </script>
</body>
</html>

Answer №1

    function createContact(fname, lname, email) {
        this.fname = fname;
        this.lname = lname; 
        this.email = email;
    }
    var table = document.createElement("TABLE");
    createContact.prototype.write = function() {
    
        var contactInfo = "<tr><td>" + this.fname + "</td>";
        contactInfo += "<td>" + this.lname + "</td>";
        contactInfo += "<td>" + this.email + "</td></tr>";

        document.write(contactInfo);
    }

    var contact1 = new createContact('Roger', 'Williams', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe8c89979292979f938dbe99939f9792d09d9193">[email protected]</a>');
    var contact2 = new createContact ('Rose', 'Schultz', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="addfc2dec8f2deedc8ccdfd9c5c1c4c3c683c3c8d9">[email protected]</a>');

    document.write("<table border=\"2\"><tr><th class='ab'>First Name</th><th class='ab'>Last Name</th><th class='ab'>Email Address</th></tr>");
    contact1.write();
    contact2.write();
    document.write("</table");
.contact-table {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: small;
    color: #993300;
    background-color: #CCFFCC;
    padding: 3px;
    height: 30px;
    width: 350px;
    border: thin dotted #993300;
}
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Address Book</title>
  </head>
<body>



</body>
</html>

Answer №2

Have you considered using HTML tables as an alternative?

var adrbook = "<table>
  <tr>
    <th>First Name</th>
    <th>Last Name</th> 
    <th>Email</th>
  </tr>
  <tr>
    <td>" + this.fname +" </td>
    <td>" + this.lname + "</td> 
    <td>" + this.email + "</td>
  </tr>
</table>"

It's worth considering if this fits your needs, and it could potentially be a viable solution.

Answer №3

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Contact List</title>
  <style type="text/css">
      .cl {
    font-family: Arial, sans-serif;
    font-size: medium;
    color: #336699;
    background-color: #FFFFCC;
    padding: 8px;
    height: 120px;
    width: 300px;
    border: thin solid #336699;
    }
  </style>

  <script type="text/javascript">
    function contactListItem (firstname, lastname, emailaddress) {
        this.firstname = firstname;
        this.lastname = lastname; 
        this.emailaddress = emailaddress;
    }
    var y = document.createElement("TABLE");
    contactListItem.prototype.write = function() {
        // var contacts = "<p class='cl' First Name: " + this.firstname + "&ltbr /&gt";
        var contacts = "<tr><td>" + this.firstname + "</td>";
        contacts += "<td>" + this.lastname + "</td>";
        contacts += "<td>" + this.emailaddress + "</td></tr>";

        document.write(contacts);
    }

</script>
</head>
<body>
    <script type="text/javascript">
        var cl1 = new contactListItem('Alice', 'Johnson', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c2c7d9dcdcd9d1ddc3f0d7ddd1d9dc9ed3dfdd">[email protected]</a>');
        var cl2 = new contactListItem ('Bob', 'Smith', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e2ffe3f5cfe3d0f5f1e2e4f8fcf9fefbbefef5e4">[email protected]</a>');
        document.write("<table border=\"2\"><tr><th>First Name</th><th>Last Name</th><th>Email Address</th></tr>");
        cl1.write();
        cl2.write();
        document.write("</table");
    </script>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance the <div> with interactive content through dynamic updates on click within the same element

I am currently developing an Editor-Menu for a website administration. When I open admin.php, the Editor-Menu should be loaded into the #ausgabe div using the code $('#ausgabe').load('./admin-ajax/ajax2.php'). This functionality is work ...

Struggling to make an AJAX form function properly

I'm running into an issue while setting up a basic AJAX form. My goal is to have a message display on the same page upon successful login using a PHP form through AJAX, but currently, I get redirected to the PHP file after form submission. Can anyone ...

Decode PDF417 barcode images with ease using HTML and JavaScript on both desktop and mobile web browsers

Looking to utilize JavaScript to decode PDF417 type barcode images? If you're encountering Zxing plugin issues in mobile browsers but it works well on desktop browsers with https://github.com/PeculiarVentures/js-zxing-pdf417, consider alternative sol ...

Immutable value of a fixed attribute and the format of the element's date

I have a couple of inquiries regarding the internet TV site I am currently working on. First, I want the main subscription element to have an attribute called (ServerIP) with a fixed value that cannot be changed. However, the code I tried did not work as e ...

Can JavaScript functions be automated on a web browser using Power BI tables?

I am facing a challenge with saving data from a powerbi table on a daily basis. When I hover over the table and click on the ellipsis menu, a button element is generated in HTML that allows me to save the data to a CSV file using Selenium. However, achiev ...

Utilize Flexbox to arrange elements in a column direction without relying on the position property in CSS

Flexbox - align element to the right using flex-direction: column; without relying on position in CSS Hello everyone, I am looking to move my row element to the right without using positioning. This code includes flex-direction: column; and I do not have ...

Retrieval of jQuery remove() method

Essentially, I am utilizing an OnClick function to delete a DIV. Once the OnClick is triggered, it invokes the remove() jQuery function to eliminate the div. Below is my code that implements the removal using remove(): <div id="add"> <button typ ...

Error with decodeURIComponent function in Internet Explorer 8

My widget, loaded as an iframe, requires data about the hosting page. I have UTF-8 strings extracted from a page with Russian text. The page itself has proper HTML5 doctype and meta charset. This is how my code operates: params = "x1=" + encodeURICompone ...

Experiencing problems with the response from the Netlify Lambda function.. consistently receiving undefined results

I've been working on setting up a lambda function to handle authentication, validation, and sending of a contact form. As I'm new to lambda functions, my code might have some flaws. However, despite my efforts, I am struggling to modify the resp ...

I am attempting to create a stylish border for the .navbar-collapse element, but unfortunately, my efforts have been unsuccessful so far

I need assistance with adding a border around my mobile drop-down menu. I've tried using .navbar-collapse but it's not working. Any suggestions? Here is a sample image of the mobile size: Below is my code: <nav class="navbar navbar-default ...

Guide on incorporating mouse movement while the mouse button is held down in JavaScript

For my project, I want to activate the mouse move event only when the mouse button is held down. I specifically need the action "OK Moved" to be triggered only when both the mouse button is held down and there is mouse movement. This is the code that I h ...

What is the best way to organize Node/Express routes based on their type into different files?

My /router/index.js file is becoming too cluttered, and I want to organize my routes by group (user routes, post routes, gear routes) into separate files within /router/routes/. Here's what I currently have set up: app.js var express = require(&apos ...

A step-by-step guide on uploading files from the frontend and saving them to a local directory using the fs and express modules

I'm considering using fs, but I'm not entirely sure how to go about it. Here's the setup: ejs: <form action="/products" method="POST"> <input type="file" name="image" id="image"> <button class="submit">Submit</but ...

Setting the width of colspan elements can be challenging when other columns have dynamic widths

I am working with a table setup that includes various columns for data tracking: <table > <thead style="width: calc(100% - 15px); display: table; table-layout: fixed;"> <tr> <th colspan="3">& ...

When attempting to access /test.html, Node.js server returns a "Cannot GET"

Embarking on the journey of diving into Pro AngularJS, I have reached a point where setting up the development environment is crucial. This involves creating an 'angularjs' directory and placing a 'test.html' file in it. Additionally, o ...

What is the best way to extract data from an array of objects in a JSON response?

I am currently exploring the utilization of API's in React. Right now, I am developing a search input feature for country names using the Rest Countries API. The data is being pulled from . However, I am facing a challenge in handling this data as it ...

The styles order definition in the Material-UI production build may vary from that in development

When using material-ui, an issue arises in the production build where the generated styles differ from those in development. The order of the material-ui styles in production does not match the order in development. In DEV, the material-ui styles are defi ...

What causes the Invalid Form Body error to appear when using the Discord API?

While developing a Discord bot, I encountered an issue with creating a ping command. The error message received was as follows: (node:37584) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.footer.icon_url: Scheme "flashybot& ...

There was a SyntaxError that caught me by surprise in my Javascript code when it unexpectedly encountered

I have been encountering an error in my code consistently, and I am struggling to comprehend why this is happening. The problematic area seems to be in line 29 of my Javascript file. HTML <link href="Styles12.css"; type="text/css" rel="stylesheet"> ...

Show all span elements in a map except for the last one

Within my ReactJS application, I have implemented a mapping function to iterate through an Object. In between each element generated from the mapping process, I am including a span containing a simple care symbol. The following code snippet demonstrates t ...