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

From databases to forms: Use Lab Exercise 6 (below) and change it to read the st

ID: 3775894 • Letter: F

Question

From databases to forms: Use Lab Exercise 6 (below) and change it to read the states from a database table instead of putting them into a PHP array yourself.

The database is named weblab and the table of states is named state_t. The table was created as follows:

Display the state name in the drop-down, but transmit the state abbreviation through the form. You do this by using a value attribute on the <option> element. The form area for State will look something like the following. Of course, you have to build this using PHP, and not just type it in.

You will not use the state_zone attribute. In "real life" it would be used for calculating shipping, maybe.

Present the state names in alphabetical order on your form. The easy way to do this is to have the database management system sort them for you using an ORDER BY clause in your SQL. For those of you who took Database long ago and far away (or not at all!), a suitable query for populating the array is this:

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

<!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
$username="your_username";
$password="your_password";
$database="weblab";
$servername = "127.0.0.1";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT state_abbr, state_name from state_t ORDER BY state_name;";
$result = $conn->query($query);

echo "<select name=state>";
while($row = $result->fetch_assoc()) {
echo "<option value = ".$row["state_abbr"].">".$row["state_name"]."</option>";
}   
echo "</select>";
$conn->close();
?>
<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>

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