Part 1; From databases to forms: Read Database Access with PHP. Copy your form f
ID: 3859647 • Letter: P
Question
Part 1; From databases to forms: Read Database Access with PHP. Copy your form from Lab Exercise6 to l7p1.php (see 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: CREATE TABLE state_t ( state_abbr char(2) PRIMARY KEY, state_name char(20), state_zone integer); 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
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.
AlabamaFloridaGeorgiaTennessee 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: SELECT state_abbr, state_name from state_t ORDER BY state_name;
Below ismy previous code that needs to be changed
<!DOCTYPE html>
<html>
<head>
<title> Tools For Sale </title>
<script>
function validateForm() {
var a=document.getElementById("saw_qty").value;
var b=document.getElementById("crescent_wrench_qty").value;
var c=document.getElementById("hoe_qty").value;
if(isNaN(a)){
alert("Enter a numeric value for Saw Quantity");
return false;
}else if(isNaN(b)){
alert("Enter a numeric value for Crescent Wrenc Quantity");
return false;
}else if(isNaN(c)){
alert("Enter a numeric value for Hoe Quantity");
return false;
}else{
var price= (a*8)+(b*5)+(c*5);
var total=price*1.07;
if (confirm("The total cost of order is: "+total) == true)
{
return true;
}
else{ return false;
}
}
}
</script>
</head>
<body>
<h1> Tools For Sale </h1>
<table>
<tr>
<th>Tools</th>
<th>Price</th>
<th>Shipping weight</th>
</tr>
<tr>
<td>Saw</td>
<td>$8.00 </td>
<td>5.0 lb</td>
</tr>
<tr>
<td>Crescent Wrench</td>
<td>$6.00</td>
<td>5.0 lb</td>
</tr>
<tr>
<td>Hoe</td>
<td>$8.50 </td>
<td>2.0 lb</td>
</tr>
</table>
<br>
<form name="myForm" action="http://weblab.kennesaw.edu/formtest.php"
onsubmit="return validateForm()" method="post">
Customer Name <input type="text" name="Customer name"> <br />
Saw Quantity <input type="text" id="saw_qty" name="Saw"><br />
Crescent Wrench Quantity <input type="text" id="crescent_wrench_qty" name="Crescent Wrench"><br />
Hoe Quantity <input type="text" id="hoe_qty" name="Hoe unit"><br />
Shipping Address <input type="text" name="Shipping address"> <br />
<select>
<option selected = "selected">Choose a State</option>
<?php
$states = array ("Florida", "Georgia", "Alabama");
foreach($states as $item){
?>
<option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>
<?php
}
?>
</select>
Payment Mode
<select name="Payment mode" >
<option value="Visa"> Visa </option>
<option value="MasterCard"> MasterCard</option>
<option value="Amex">American Express</option>
</select>
<input type="submit" value="Submit" > <input type="reset" value="Clear Form">
</form>
</body>
</html>
Explanation / Answer
1. Connect to the data base using any DBMS packages. Best suitable package for students is phpMyadmin.
2. Write code for connecting to the database.
3.Write query to retrieve data from database.
4. Execute query.
5. Display the result.
.........................Here is the sample code for the same........................
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'username';
/*** mysql password ***/
$password = 'password';
// Create connection
$con=mysqli_connect(host,username,password,dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$query="SELECT state_abbr, state_name from state_t ORDER BY state_name";
$result = $conn->query($queryl);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo $row["state_name"]; // displaying state name from table
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.