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

The \"Add to Cart\" and \"Remove* from Cart\" submit buttons are placed in the f

ID: 3867106 • Letter: T

Question

The "Add to Cart" and "Remove* from Cart" submit buttons are placed in the form using PHP code.

The innerHTML of the two submit buttons above have been styled nicely and uniformly. A suggested styling: include a small shopping cart or trash can image along with the text on the face of the button.

The script submits to itself and successfully sends the vendID in the $_POST array, that is $_POST['add'] and $_POST['remove'*] should contain the vendID.

The "Add to Cart" submit button is replaced with a "Remove* from Cart" submit button when a productID has been detected in $_POST).

Explanation / Answer

index.php

<?php

session_start();

require_once("dbc.php");

$dbHan = new DBController();

if(!empty($_GET["action"])) {

switch($_GET["action"]) {

case "add":

if(!empty($_POST["quant"])) {

$prodCode = $dbHan->runQuery("SELECT * FROM tblproduct WHERE cde='" . $_GET["cde"] . "'");

$items = array($prodCode[0]["cde"]=>array('nme'=>$prodCode[0]["nme"], 'cde'=>$prodCode[0]["cde"], 'quant'=>$_POST["quant"], 'cost'=>$prodCode[0]["cost"]));

if(!empty($_SESSION["crtIt"])) {

if(in_array($prodCode[0]["cde"],array_keys($_SESSION["crtIt"]))) {

foreach($_SESSION["crtIt"] as $k => $v) {

if($prodCode[0]["cde"] == $k) {

if(empty($_SESSION["crtIt"][$k]["quant"])) {

$_SESSION["crtIt"][$k]["quant"] = 0;

}

$_SESSION["crtIt"][$k]["quant"] += $_POST["quant"];

}

}

} else {

$_SESSION["crtIt"] = array_merge($_SESSION["crtIt"],$items);

}

} else {

$_SESSION["crtIt"] = $items;

}

}

break;

case "remove":

if(!empty($_SESSION["crtIt"])) {

foreach($_SESSION["crtIt"] as $k => $v) {

if($_GET["cde"] == $k)

unset($_SESSION["crtIt"][$k]);

if(empty($_SESSION["crtIt"]))

unset($_SESSION["crtIt"]);

}

}

break;

case "empty":

unset($_SESSION["crtIt"]);

break;

}

}

?>

<HTML>

<head>

<title>Shopping Cart</title>

<link href="style.css" type="text/css" rel="stylesheet" />

</head>

<body>

<div id="shopCart">

<div class="txtHdr">Shopping Cart <a id="Empty" href="index.php?action=empty">Empty Cart</a></div>

<?php

if(isset($_SESSION["crtIt"])){

$item_total = 0;

?>

<table cellpadding="10" cellspacing="1">

<tbody>

<tr>

<th><strong>Name</strong></th>

<th><strong>Code</strong></th>

<th><strong>Quantity</strong></th>

<th><strong>Price</strong></th>

<th><strong>Action</strong></th>

</tr>

<?php

foreach ($_SESSION["crtIt"] as $item){

?>

<tr>

<td><strong><?php echo $item["nme"]; ?></strong></td>

<td><?php echo $item["cde"]; ?></td>

<td><?php echo $item["quant"]; ?></td>

<td><?php echo "$".$item["cost"]; ?></td>

<td><a href="index.php?action=remove&amp;cde=&lt;?php echo $item["cde"]; ?>" class="removeBtn">Remove Item</a></td>

</tr>

<?php

$item_total += ($item["cost"]*$item["quant"]);

}

?>

<tr>

<td colspan="5" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>

</tr>

</tbody>

</table>

<?php

}

?>

</div>

<div id="prodGrids">

<div class="txtHdr">Products</div>

<?php

$prodArr = $dbHan->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");

if (!empty($prodArr)) {

foreach($prodArr as $keys=>$value){

?>

<div class="prods">

<form method="post" action="index.php?action=add&cde=<?php echo $prodArr[$keys]["cde"]; ?>">

<div class="prodImg"><img src="&lt;?php echo $prodArr[$keys]["image"]; ?>"></div>

<div><strong><?php echo $prodArr[$keys]["nme"]; ?></strong></div>

<div class="prodPrice"><?php echo "$".$prodArr[$keys]["cost"]; ?></div>

<div><input type="text" name="quant" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAdds" /></div>

</form>

</div>

<?php

}

}

?>

</div>

</body>

</html>

style.css

body{width:610px;font-family:calibri;}

#shopCart table{width:100%;background-color:#F0F0F0;}

#shopCart table td{background-color:#FFFFFF;}

.txtHdr{

padding: 10px 10px;

border-radius: 2px;

color: #FFF;

background: #6aadf1;

margin-bottom:10px;

}

a.removeBtn{color:#D60202;border:0;padding:2px 10px;font-size:0.9em;}

a.removeBtn:visited{color:#D60202;border:0;padding:2px 10px;font-size:0.9em;}

#Empty {

background-color: #ffffff;

border: #FFF 1px solid;

padding: 1px 10px;

color: #ff0000;

font-size: 0.8em;

float: right;

text-decoration: none;

border-radius: 4px;

}

.btnAdds{ background-color: #eb9e4f;

border: 0;

padding: 3px 10px;

color: #ffffff;

margin-left: 2px;

border-radius: 2px;

}

#shopCart {margin-bottom:30px;}

.cartItems {border-bottom: #79b946 1px dotted;padding: 10px;}

#prodGrids {margin-bottom:30px;}

.prods { float:left; background: #ffffff;margin:15px 10px; padding:5px;border:#CCC 1px solid;border-radius:4px;}

.prods div{text-align:center; margin:10px;}

.prodCost {

color: #005dbb;

font-weight: 600;

}

.prodImg {height:100px;background-color:#FFF;}

.clear-float{clear:both;}

.ipBox{border-radius:2px;border:#CCC 1px solid;padding:2px 1px;}

Please rate the answer if it helped.......Thankyou

Hope it helps.....

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