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

More databases to forms (PHP and SQL): Use Lab Exercise 6 (below). The database

ID: 3776662 • Letter: M

Question

More databases to forms (PHP and SQL): Use Lab Exercise 6 (below). The database weblab has a table of fruit named fruit_t. The table was created as follows:

CREATE TABLE fruit_t (
    fruit_item_no char(10) PRIMARY KEY,
    fruit_name char(20),
    fruit_price numeric(6, 2),
    fruit_weight numeric(4, 1),
    fruit_picture char(30),
    fruit_description varchar);
Change your fruit order form to construct the table of names, prices and weights by extracting the items from the database rather than hard-coding them. Display every item from the database. Do not assume that there will be a given number of items.

In the database table definition above, numeric(6, 2) means the item has a total of six digits, of which two are to the right of the decimal point.

Your order form should display the following items for each fruit:

-Name

-Price

-Shipping Weight

Each idem of your form should have a box to allow the customer to enter quantity as you did in earlier assignments.

You will not need the fruit_picture or fruit_description attributes. Present the fruit names in alphabetical order. The following is a suitable query for retrieving from the database:

select fruit_item_no, fruit_name, fruit_price,
       fruit_weight from fruit_t
order by fruit_name;
Note: This change will "break" your JavaScript validation routines if you still have them in your form. In real life, you would have to fix this. However, in real life, you would have started with the database-driven form, so you would not have to back-track. You do not have to change your validation routines

Lab Exercise 6:

<!DOCTYPE html>
<html lang = "en">
<head>
<title>l6p2</title>
<meta charset = "UTF-8"/>

<script type = "text/javascript">

function quantity(){
var apples = parseInt(document.getElementById

("apple").value);
var pumpkins = parseInt(document.getElementById
("pumpkin").value);
var bananas = parseInt(document.getElementById
("banana").value);

if(isNaN(apples))
alert("Enter a number (Apple)");
else
if(isNaN(pumpkins))
alert("Enter a number (Pumpkin)");
else
if(isNaN(bananas))
alert("Enter a number (Banana)");
else
{
document.forms['orderform'].submit();
}
}

</script>

</head>
<body>
<h1>Lab 4, Part 3</h1>
<h2>IT 3203</h2>

<h4>Customer Order Form</h4>

<form name = newCalculation
action = "http://weblab.kennesaw.edu/formtest.php" id = "orderform">
<p>
<!--Name and address boxes-->
<label> Name:
<input type = "text" name = "name"
size = "35"/>
</label><br/>

<label> Address:
<input type = "text" name = "street"
size = "35"/>
</label><br/>

<label> City, ZIP:
<input type = "text" name = "city"
size = "35"/>
</label><br/>

Select State:

<?php
$states = array("Al", "Ga", "Fl");
echo "<select name=state>";
for($i = 0; $i < count($states); $i++)
echo "<option value = ".$states[$i].">".$states[$i]."</option>";
echo "</select>";
?>
<br/>

<!--item table-->
<table>
<tr>
<th>Fruit</th>
<th>Price per Weight</th>
<th>Quantity</th>
</tr>

<tr>
<td>Apples (3 lbs.)</td>
<td>$5.97</td>

<td> <input type = "text" id = "apple"
size = "7"/>
</td>
</tr>

<tr>
<td>Pumpkins (15 lbs.)</td>
<td>$8.49</td>
<td> <input type = "text" id = "pumpkin"
size = "7"/>
</td>
</tr>

<tr>
<td>Bananas (2 lbs)</td>
<td>$1.80</td>
<td><input type = "text" id = "banana"
size = "7"/>
</td>
</tr>

</table>

<!--Payment method, radio button-->
<h4> Payment Method</h4>
<p>
<label> <input type = "radio" name = "payment"
value = "visa"/> Visa
</label>

<label> <input type = "radio" name = "payment"
value = "mc"/> MasterCard
</label>

<label> <input type = "radio" name = "payment"
value = "ae"/> American Express
</label><br/>
</p>

<!--submit and clear-->

<p>
Confirm Order:<br/>
<input type = "button" value = "Submit"/>
<input type = "reset" value = "Clear"/>
</p>
</form>
</body>
</html>

Explanation / Answer

order.php

<?php
session_start();
header("Cache_control:private");
$mysql_server ="localhost";
$mysql_port ="3306";
$mysql_username ="root";
$mysql_password = "root";
$mysql_database = "fruit_db";

$dblink = mysqli_connect($mysql_server, $mysql_username, $mysql_password,$mysql_database);
if (!$dblink) {
die("Connection failed: " . mysqli_connect_error());
}
?>

<!DOCTYPE html>
<html lang = "en">
<head>
<title>l6p2</title>
<meta charset = "UTF-8"/>
<script type = "text/javascript">
function quantity(){
   var qty = parseInt(document.getElementById("qty").value);
   if(isNaN(qty)){
       alert("Enter a number of quantity");
   }
   else
   {      
       document.forms['orderform'].submit();
   }
}
</script>
</head>
<body>
<h1>Lab 4, Part 3</h1>
<h2>IT 3203</h2>
<h4>Customer Order Form</h4>
<form name = newCalculation
action = "http://weblab.kennesaw.edu/formtest.php" id = "orderform">
<p>
<!--Name and address boxes-->

<label> Name:
<input type = "text" name = "name"
size = "35"/>
</label><br/>
<label> Address:
<input type = "text" name = "street"
size = "35"/>
</label><br/>
<label> City, ZIP:
<input type = "text" name = "city"
size = "35"/>
</label><br/>
Select State:
<?php
$states = array("Al", "Ga", "Fl");
echo "<select name=state>";
for($i = 0; $i < count($states); $i++)
echo "<option value = ".$states[$i].">".$states[$i]."</option>";
echo "</select>";
?>
<br/>
<!--item table-->


<table border="1">
<thead>
<tr>
</thead>
<th>Sr. No</th>
<th>Fruit Name</th>
<th>Price per Weight</th>
<th>Quantity</th>
<th>Description</th>
</thead>
<tbody>
<?php
$query = "select fruit_item_no, fruit_name, fruit_price, fruit_weight,fruit_description from fruit_t order by fruit_name;";
$result = mysqli_query($dblink, $query);
   if($result && mysqli_num_rows($result)) {
       while($row = mysqli_fetch_array($result)) {
           $id = $row['fruit_item_no'];
           $name = $row['fruit_name'];
           $price = $row['fruit_price'];
           $description = $row['fruit_description'];
           echo "<tr>";
           echo "<td >$id</td>";
           echo "<td >$name</td>";
           echo "<td >$price</td>";
           echo "<td ><input type = 'text' id = 'qty' name ='$name' size = '7'/></td>";
           echo "<td >$description</td>";
           echo "</tr>";
       }
   }
   else {
       echo "<tr><td colspan=11 ><b><center><font color='#b94a48'>Fruits not available</font></center></b></td></tr>";
   }?>
</tbody>
</table>


<!--Payment method, radio button-->
<h4> Payment Method</h4>
<p>
<label> <input type = "radio" name = "payment"
value = "visa"/> Visa
</label>
<label> <input type = "radio" name = "payment"
value = "mc"/> MasterCard
</label>
<label> <input type = "radio" name = "payment"
value = "ae"/> American Express
</label><br/>
</p>
<!--submit and clear-->
<p>
Confirm Order:<br/>
<input type = "button" value = "Submit"/>
<input type = "reset" value = "Clear"/>
</p>
</form>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote