Learn about...
lab11
folder, create a new file called p1.html
and copy the following code
<!doctype html> <html lang="en"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <title>Part 1</title> <body> <div class="container"> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <script> </script> </body> </html>
<div class="container" id="ctnr">
lab11
folder, create a new file called p1.php
and copy the following code
<? require_once('site_db.php'); $r = run_query("SELECT userid, type FROM users"); echo ' <table class="table"> <tr> <th>Action</th> <th>User ID</th> <th>Type</th> </tr>'; while($u = $r->fetch_assoc()) { echo ' <tr> <td> <button class="btn btn-danger">Delete</button> </td> <td>'.$u['userid'].'</td> <td>'.$u['type'].'</td> </tr>'; } echo '</table>'; ?>
site_db.php
file.site_db.php
from your project2
folder to your lab11
folder.site_db.php
and p1.php
to the server and test the script. It should output an un-styled table of users with a delete button next to each userid.script
tag of p1.html
:
<script> function responseHandler() { alert(this.response); } var xhr = new XMLHttpRequest(); xhr.addEventListener("load", responseHandler); xhr.open("GET", "p1.php"); xhr.send(); </script>
p1.html
to the server and test the script. When the page is loaded, JavaScript sends a request for p1.php
and displays it using an alert.alert(this.response)
with the following:
var c = document.getElementById("ctnr"); c.innerHTML = this.response;
document
is a pointer to the DOM of the current loaded webpage.getElementById
to create a pointer c
to the container div so that it is rendered with Bootstrap's CSS.c.innerHTML
is the container div's content.p1.html
to the server again and test the script. When the page is loaded, the table should now appear inside the container div.p1.html
as p2.html
lab11
folder, create a new file called p2.php
and copy the following code:
<? require_once('site_db.php'); $r = run_query("SELECT userid, type FROM users"); $list = array(); while($u = $r->fetch_row()) { array_push($list, $u); } echo json_encode($list); ?>
p2.php
and test it on the server.
p1.html
as p2.html
p2.html
so that we make an HTTP request for p2.php
instead of p1.php
:
var xhr = new XMLHttpRequest(); xhr.addEventListener("load", responseHandler); xhr.open("GET", "p2.php"); xhr.send();
p2.html
to put the table inside of the container as follows:
<div class="container" id="ctnr"> <table class="table"> <thead> <tr> <th>Action</th> <th>User ID</th> <th>Type</th> </tr> </thead> <tbody id="tbl"> </tbody> </table> </div>
tbody
an id.p2.html
to the server and test it. You should see the JSON encode string loaded in the page, instead of the table.function responseHandler() { var table = document.getElementById("tbl"); var tdata = this.response; var tarray = JSON.parse(tdata); for(var i = 0; i < tarray.length; i++) { var trow = document.createElement("tr"); var td1 = document.createElement("td"); var td2 = document.createElement("td"); var td3 = document.createElement("td"); td1.innerHTML = '<button class="btn btn-danger">delete</button>'; td2.innerHTML = tarray[i][0]; td3.innerHTML = tarray[i][1]; trow.appendChild(td1); trow.appendChild(td2); trow.appendChild(td3); table.appendChild(trow); } }
p2.php
will echo. In this case, p2.php
echos a JSON encoded string that represents a 2D array.td
elements into a row.p2.html
as p3.html
p3.php
in your lab11
and add the following code:
<? require_once('site_db.php'); $userid = $_GET['userid']; $passwd = $_GET['passwd']; $type = $_GET['type']; run_query("INSERT INTO users VALUE ('$userid','$passwd','$type')"); ?>
p3.html
and add the following form elements inside the container and above the table:<div class="container"> <div class="form-group"> <label for="userid">UserID</label> <input type="text" class="form-control" id="userid"> </div> <div class="form-group"> <label for="passwd">Password</label> <input type="password" class="form-control" id="passwd"> </div> <div class="form-group"> <label for="type">Type</label> <input type="text" class="form-control" id="type"> </div> <button class="btn btn-primary" id="sbtn">Submit</button> <hr> <table class="table"> <thead> <tr> <th>Action</th> <th>User ID</th> <th>Type</th> </tr> </thead> <tbody id="tbl"> </tbody> </table> </div> </div>
p3.html
add the following event listener. You can add it anywhere inside the script tag:
var submitBtn = document.getElementById("sbtn"); submitBtn.addEventListener("click", insertUser);
sbtn
.
Notice that we are adding an event listener so that the function insertUser
is called when the submitBtn object
is clicked.
insertUser
functionp.html
, add the following function. You can add it anywhere inside the script tag:
function insertUser() { // Get the submitted form values var usr = document.getElementById("userid").value; var psw = document.getElementById("passwd").value; var typ = document.getElementById("type").value; // Get the table body var table = document.getElementById("tbl"); // Create a new table row var trow = document.createElement("tr"); // Create three new table data cells var td1 = document.createElement("td"); var td2 = document.createElement("td"); var td3 = document.createElement("td"); // Put content and value into the table data cells td1.innerHTML = '<button class="btn btn-danger">delete</button>'; td2.innerHTML = usr; td3.innerHTML = typ; // Append the cells to the row trow.appendChild(td1); trow.appendChild(td2); trow.appendChild(td3); // Insert the row as the first child of the table body table.insertBefore(trow, table.firstChild); // Request p3.php and pass usr, psw and typ in the URL using the GET method var xhr = new XMLHttpRequest(); xhr.open("GET", "p3.php?userid="+usr+"&passwd="+psw+"&type="+typ); xhr.send(); }
var xhr = new XMLHttpRequest(); xhr.open("GET", "p3.php?userid="+usr+"&passwd="+psw+"&type="+typ); xhr.send();
p3.php
return the three values and then we could create an event listener
to dynamically add the values to the displayed table only if p3.php
responds
without error.
If p3.php
returns an error (null or 0), we could then display the error using
an alert or by dynamically inserting a text message into the form.
Note that there is no part 4. It was combined with part 3.
lab11
folderp5.php
in your lab11
folderNothing. You get full credit for using the 2-hour lab time productively.