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

Part 1; From databases to forms: read the states from a database table instead o

ID: 3820062 • Letter: P

Question

Part 1; From databases to forms: read the states from a database table instead of putting them into a PHP array yourself.(the php file is included down below)

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

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:

this is my php file

<!DOCTYPE html>

<html>
<head>
<title>My Form</title>
<meta charset="UTF-8" />

<style type="text/css">
   table {
border-collapse: collapse;
}
td {
padding-top: .5em;
padding-bottom: .5em;
padding-right: 1.5em;
}
</style>
<script type="text/javascript">
  
   function calcCost() {
var sum = 0;
var tax = 0;
var cwQty = document.getElementById("crescent_wrench").value;
var spQty = document.getElementById("spanners").value;
var mwQty = document.getElementById("monkey_wrench").value;

if(isNaN(cwQty)){
            alert("Enter a numeric value for Crescent_Wrench Quantity");
            return false;
            }else if(isNaN(spQty)){
            alert("Enter a numeric value for spanners Quantity");
            return false;
            }else if(isNaN(mwQty)){
           alert("Enter a numeric value for monkey_wrench Quantity");
            return false;
            }else{
cwQty = (cwQty == "") ? parseInt("0") : parseInt(cwQty);
spQty = (spQty == "") ? parseInt("0") : parseInt(spQty);
mwQty = (mwQty == "") ? parseInt("0") : parseInt(mwQty);
  
sum = (cwQty*15) + (spQty*10) + (mwQty*12) ;
tax = sum * 1.07;
return confirm("The total cost of your order is $" + (sum + tax).toFixed(2));
    }
    }

</script>


<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet"
type="text/css"
href="l2p1.css"/>
</head>
<body>
<form action="http://weblab.kennesaw.edu/formtest.php" method="post">
<h3>Order</h3>
<h4>Product quantity wanted</h4>

<table>
   <tr>
       <th>Product</th>
       <th>Price</th>
       <th>Quantity</th>
   </tr>
   <tr>
       <td>crescent_wrench</td>
       <td>$15</td>
       <td><input id="crescent_wrench" type="text" name="crescent_wrench" ></td>
   </tr>
   <tr>
       <td>spanners</td>
       <td>$10</td>
       <td><input id="spanners" type="text" name="spanners"></td>
   </tr>
   <tr>
       <td>monkey_wrench</td>
       <td>$12</td>
       <td><input id="monkey_wrench" type="text" name="monkey_wrench"></td>
   </tr>
</table>
<h4>Customer infromation: </h4>
<div>
   <label for="firstName">First Name:</label>
       <input id="firstName" type="text" name="FirstName">
   <label for="LastName">Last Name:</label>
       <input id="LastName" type="text" name="LastName">

   <label for="address">Shipping address:</label>
       <input id="address" type="text" name="address">
</div>

<br>
<select name="states">
   <option selected="selected">--- Select State---</option>
   <?php
   $states=array("Georgia","Alabama","Florida");
   foreach($states as $state){
   ?>
       <option value="<?php echo strtolower($state);?>"><?php echo $state;?></option>"
       <?php
   }
   ?>
</select>


<div>
   <h4>payement methods: </h4>
   <label for="Visa">Visa: </label>
   <input id="Visa" type="radio" name="payement" value="Visa">

   <label for="MasterCard">MasterCard: </label>
   <input id="MasterCard" type="radio" name="payement" value="MasterCard">

   <label for="AmericanExpress">American Express: </label>
   <input id="AmericanExpress" type="radio" name="payement" value="AmericanExpress">
</div>

<p> <input type="submit" value="Submit Order" /></p>

</form>

</body>
</html>

Explanation / Answer

<?php
//--- This is the way to connect MySQLi Procedural
$servername = "localhost"; //your server name
$username = "username"; //user name
$password = "password";//password name
$dbname = "weblab"; // your DB name

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT state_abbr, state_name from state_t ORDER BY state_name"; /* By default it will sort state_name by ascending. For descending you need to add query like SELECT state_abbr, state_name from state_t ORDER BY state_name DESC */
$state_result = mysqli_query($conn, $sql);
?>

<!DOCTYPE html>

<html>
<head>
<title>My Form</title>
<meta charset="UTF-8" />

<style type="text/css">
    table {
    border-collapse: collapse;
}
td {
    padding-top: .5em;
    padding-bottom: .5em;
    padding-right: 1.5em;
}
</style>
<script type="text/javascript">
  
      function calcCost() {
           var sum = 0;
           var tax = 0;
           var cwQty = document.getElementById("crescent_wrench").value;
           var spQty = document.getElementById("spanners").value;
           var mwQty = document.getElementById("monkey_wrench").value;

           if(isNaN(cwQty)){
                   alert("Enter a numeric value for Crescent_Wrench Quantity");
                   return false;
               }else if(isNaN(spQty)){
                   alert("Enter a numeric value for spanners Quantity");
                   return false;
               }else if(isNaN(mwQty)){
                   alert("Enter a numeric value for monkey_wrench Quantity");
                   return false;
               }else{
           cwQty = (cwQty == "") ? parseInt("0") : parseInt(cwQty);
           spQty = (spQty == "") ? parseInt("0") : parseInt(spQty);
           mwQty = (mwQty == "") ? parseInt("0") : parseInt(mwQty);
        
           sum = (cwQty*15) + (spQty*10) + (mwQty*12) ;
           tax = sum * 1.07;
           return confirm("The total cost of your order is $" + (sum + tax).toFixed(2));
           }
      }

</script>


<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet"
type="text/css"
href="l2p1.css"/>
</head>
<body>
<form action="http://weblab.kennesaw.edu/formtest.php" method="post">
<h3>Order</h3>
<h4>Product quantity wanted</h4>

<table>
    <tr>
        <th>Product</th>
        <th>Price</th>
        <th>Quantity</th>
    </tr>
    <tr>
        <td>crescent_wrench</td>
        <td>$15</td>
        <td><input id="crescent_wrench" type="text" name="crescent_wrench" ></td>
    </tr>
    <tr>
        <td>spanners</td>
        <td>$10</td>
        <td><input id="spanners" type="text" name="spanners"></td>
    </tr>
    <tr>
        <td>monkey_wrench</td>
        <td>$12</td>
        <td><input id="monkey_wrench" type="text" name="monkey_wrench"></td>
    </tr>
</table>
<h4>Customer infromation: </h4>
<div>
    <label for="firstName">First Name:</label>
        <input id="firstName" type="text" name="FirstName">
    <label for="LastName">Last Name:</label>
        <input id="LastName" type="text" name="LastName">

    <label for="address">Shipping address:</label>
        <input id="address" type="text" name="address">
</div>

<br>
<select name="states">
    <option selected="selected">--- Select State---</option>
    <?php
       if (mysqli_num_rows($state_result) > 0) {
           while($a_state = mysqli_fetch_assoc($state_result)) {
   ?>
               <option value="<?php echo strtolower($a_state['state_abbr']);?>" > <?php echo $a_state['state_name'];?> </option>
   <?php
           }
       }
    ?>
</select>


<div>
    <h4>payement methods: </h4>
    <label for="Visa">Visa: </label>
    <input id="Visa" type="radio" name="payement" value="Visa">

    <label for="MasterCard">MasterCard: </label>
    <input id="MasterCard" type="radio" name="payement" value="MasterCard">

    <label for="AmericanExpress">American Express: </label>
    <input id="AmericanExpress" type="radio" name="payement" value="AmericanExpress">
</div>

<p> <input type="submit" value="Submit Order" /></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