Monday, July 25, 2016

Add Data From Form Using JavaScript

Add Data From Form Using JavaScript

Quesstion

Dynamic Web Design

1 Objectives covered in this laboratory

a) Copy the code of “example1.htm”. The file is located in lecture 5’s
example code zip file. Then, change the ‘write’ function to output given
name/email into a HTML table. Each ‘addressBookItem’ is displayed as
a row in the table, and the fields of fname/lname/email are different
columns (see Figure 1).
Figure

Figure 1 display address book items in a table

b) Based on a), write a function called appendRow() using DOM to
append a new row in a different color at the end of the table (see
Figure 2). The content of new row is taken from the user. You can use
prompt (text, default) function to take the inputs from users.
Figure 2


Figure 2 append new row
--------------------------------------------------------------------------------------------------------------------------

Answer

Code:

         a)Figure 1 display address book items in a table


<!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>
 
table,tr,th,td
       
{
           
border: 1px solid black ;
        }
  </
style>
</
head>
<
body>
    <
table>
      <
tr>
        <
th>First Name</th>
        <
th>Last Name</th>
        <
th>Email Address</th>
      </
tr><br/>
      <
tr>
        <
td>Roger</td>
        <
td>Williams</td>
        <
td>rwilliams@gamil.com</td>
      </
tr><br>
      <
tr>
        <
td>Rose</td>
        <
td>Schultz</td>
        <
td>rose_s@earthlink.net</td>
      </
tr>

    </
table>
</
body>
</
html>

a B)     Figure 2 append new row

<!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>

table, td, th, td {

    border: 1px solid black;

}



</style>

</head>



<body>

<table id="myTable">

  <tr >

        <th>First Name</th>

        <th>Last Name</th>

        <th>Email Address</th>

      </tr><br/>

      <tr>

        <td>Roger</td>

        <td>Williams</td>

        <td>rwilliams@gamil.com</td>

      </tr><br>

      <tr>

        <td>Rose</td>

        <td>Schultz</td>

        <td>rose_s@earthlink.net</td>

    <tr>

        <td id="firstName"></td>

        <td id="lastName"></td>

        <td id="emailAddress"></td>

</table>

<br>

<button onclick="appendRow()">Append New Row</button>



<script>

function appendRow() {

    var Fist = prompt("please enter your First Name", "Adam");

    var Last = prompt("please enter your Last Name", "Hill");

    var Email = prompt("please enter your Email Address", "aHill@gmail.com");

    if (Fist,Last,Email != null){

        document.getElementById("firstName").innerHTML= Fist;

        document.getElementById("lastName").innerHTML= Last;

        document.getElementById("emailAddress").innerHTML= Email;

    }

    document.getElementById("firstName").style.color="lawngreen";

    document.getElementById("lastName").style.color="lawngreen";

    document.getElementById("emailAddress").style.color="lawngreen";

}

</script>



</body>

</html>
Filter Data From DataBase Using PHP on Page

Filter Data From DataBase Using PHP on Page


<?php
if
(isset($_POST['search']))
{
   
$ValueSearch = $_POST['value'];
   
$query = "SELECT * FROM `inventory` WHERE CONCAT(`item_number`, `make`, `model`, `price`, `quantity`) LIKE '%".$ValueSearch."%'";
   
$SearchResult = filterTable($query);
}
else{
   
$query = "SELECT * FROM `inventory`";
   
$SearchResult = filterTable($query);
}
function filterTable($query){
   
$username="root";
   
$password=null;
   
$database="pich";
   
$connect = mysqli_connect("localhost",$username,$password,$database);
   
$filter_Result = mysqli_query($connect, $query);
   
return $filter_Result;
}
?>
<!doctype html>
<
html>
<
head>
    <
title>Filter and Search using PHP&HTML</title>
    <
style>
       
table,tr,th,td
       
{
           
border: 1px solid black ;
        }
    </
style>
</
head>
<
body>
<
form action='Exercise_Php_C.php' method='POST'>
    Please Select a make:

    <select name='value'>

        <option value=''>All</option>

        <option value='Yamaha'>Yamaha</option>

        <option value='Matin'>Matin</option>

        <option value='Washborn'>Washborn</option>

        <option value='Fender'>Fender</option>

        <option Value='Honda'>Honda</option></select><br><br>

    <input type='submit' name='search' value='Search'><br><br>
           <hr>
    <table>

        <tr>

            <th>Item Number</th>

            <th>Make</th>

            <th>Model</th>

            <th>Price</th>

            <th>Quantity</th>

        </tr>

        <?php while ($row = mysqli_fetch_array($SearchResult)):?>

            <tr>

                <td><?php echo $row['item_number'];?></td>

                <td><?php echo $row['make'];?></td>

                <td><?php echo $row['model'];?></td>

                <td><?php echo $row['price'];?></td>

                <td><?php echo $row['quantity'];?></td>

            </tr>

        <?php endwhile; ?>

    </table>

</form>

</body>

</html>


You can go to get more information about this code by click here: https://youtu.be/hCny9MtpUSk
Add data into database from Form using PHP

Add data into database from Form using PHP

Code:

<?php

//PHP insert new Record

if(isset($_POST['add'])){

    try{

        $PDOConnect = new PDO("mysql:host=localhost;dbname=pich","root","");

    } catch (PDOException $exc){

        echo $exc->getMessage();

        exit();

    }

    $make = $_POST['make'];

    $model = $_POST['model'];

    $price = $_POST['price'];

    $quantity = $_POST['quantity'];



    $PDOQuery = "INSERT INTO `inventory`(`make`, `model`, `price`, `quantity`) VALUES (:make,:model,:price,:quantity)";

    $PDOResult = $PDOConnect->prepare($PDOQuery);

    $PDOExecute = $PDOResult->execute(array(":make"=>$make,":model"=>$model,":price"=>$price,"quantity"=>$quantity));



    if($PDOExecute){

        echo 'Successfully inserted data into invetory table';

    }else{

        echo 'data Not Inserted';

    }

}

//end insert



//Display Table information

if(isset($_POST['add']))

{

    $query = "SELECT * FROM `inventory` WHERE CONCAT(`item_number`, `make`, `model`, `price`, `quantity`)";

    $SearchResult = filterTable($query);

}

else{

    $query = "SELECT * FROM `inventory`";

    $SearchResult = filterTable($query);

}

function filterTable($query){

    $username="root";

    $password=null;

    $database="pich";

    $connect = mysqli_connect("localhost",$username,$password,$database);

    $FilterResult = mysqli_query($connect, $query);

    return $FilterResult;

}

//end Table Display

?>



<!doctype html>

<html>

<head>

    <title>Insert Record Using PHP Code</title>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>

        table,tr,th,td

        {

            border: 1px solid black ;

        }

    </style>

</head>

<body>

<form action="Exercise_Pph_D.php" method="post">

    <h1>Insert a new row: </h1>

    Make&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="make" required placeholder="Make" ><br><br>

    Model&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="model" required placeholder="Model" ><br><br>

    Price&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="number" name="price" required placeholder="Price"> <br><br>

    Quantity<input type="number" name="quantity" required placeholder="Quantity"> <br><br>

    <input type="submit" name="add" value="Add">
    <hr>



    <table>

        <h1>content of inventory table</h1>

        <tr>

            <th>Item Number</th>

            <th>Make</th>

            <th>Model</th>

            <th>Price</th>

            <th>Quantity</th>

        </tr><br><br>



        <?php while ($row = mysqli_fetch_array($SearchResult)):?>

            <tr>

                <td><?php echo $row['item_number'];?></td>

                <td><?php echo $row['make'];?></td>

                <td><?php echo $row['model'];?></td>

                <td><?php echo $row['price'];?></td>

                <td><?php echo $row['quantity'];?></td>

            </tr>

        <?php endwhile; ?>

    </table>

</form>

</body>

</html>


You can go to get more education of above this code by click on here: https://youtu.be/uMxaAdP3LrM