Tying form in with PHP

This commit is contained in:
Chris Smith
2019-03-04 13:39:34 -06:00
parent 8a6093e263
commit 7e2bbdc297
4 changed files with 82 additions and 12 deletions

6
src/.env.example Normal file
View File

@@ -0,0 +1,6 @@
EVENT_TICKET_PRICE="55"
ENHANCER_TICKET_PRICE="20"
DB_HOST="localhost"
DB_USER="root"
DB_PASS="root"
POSTMARK_API_KEY="special"

64
src/functions.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
/**
* Returns integer and santitizes bad user data
*
* @param $variable
* @param bool $postiveOnly
* @return int
*/
function getInteger($variable, $postiveOnly = true)
{
// Change to integer if not one
if (!is_int($variable)) {
$variable = (int)$variable;
}
if ($postiveOnly && $variable < 0) {
$variable = 0;
}
return $variable;
}
/**
* Converts possible float or string to cents
*
* @param $variable
* @return int
*/
function convertPossibleFloatToCents($variable)
{
$variable = intval(strval(floatval(preg_replace("/[^0-9.]/", "", str_replace(',', '.', $variable))) * 100));
return $variable;
}
/**
* Show shopping cart line item on form
*
* @param $name
* @param $price
*/
function shoppingCartLineItem($name, $price, $description = '')
{
if ($price > 0) {
echo '<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h6 class="my-0">' . $name . '</h6>
<small class="text-muted">' . $description . '</small>
</div>
<span class="text-muted">' . '$' . number_format(($price/100), 2) . '</span>
</li>';
}
}
function shoppingCartTotal($price)
{
echo '<li class="list-group-item d-flex justify-content-between">
<span>Total</span>
<strong>' . '$' . number_format(($price/100), 2) . '</strong>
</li>';
}

View File

@@ -77,7 +77,7 @@
</div> </div>
<div class="col-md-8 order-md-1"> <div class="col-md-8 order-md-1">
<h4 class="mb-3">Your Order</h4> <h4 class="mb-3">Your Order</h4>
<form class="needs-validation" novalidate> <form class="needs-validation" novalidate method="POST">
<div class="mb-3"> <div class="mb-3">
<label for="eventTickets">How many tickets would you like to purchase?</label> <label for="eventTickets">How many tickets would you like to purchase?</label>

View File

@@ -5,23 +5,23 @@
<span class="text-muted">Your cart</span> <span class="text-muted">Your cart</span>
</h4> </h4>
<ul class="list-group mb-3"> <ul class="list-group mb-3">
<li class="list-group-item d-flex justify-content-between lh-condensed"> <?=shoppingCartLineItem('Dinner', $eventTicketPrice, $eventTicketQty . ' x tickets');?>
<div> <?=shoppingCartLineItem('Ticket Enhancers', $ticketEnhancerPrice, $ticketEnhancerQty . ' x enhancers');?>
<h6 class="my-0">Product name</h6> <?=shoppingCartLineItem('Additional Contribution', $additionalContribution);?>
<small class="text-muted">Brief description</small> <?=shoppingCartTotal($cartTotal);?>
</div>
<span class="text-muted">$12</span>
</li>
<li class="list-group-item d-flex justify-content-between">
<span>Total</span>
<strong>$20</strong>
</li>
</ul> </ul>
</div> </div>
<div class="col-md-8 order-md-1"> <div class="col-md-8 order-md-1">
<h4 class="mb-3">Billing address</h4> <h4 class="mb-3">Billing address</h4>
<form class="needs-validation" novalidate> <form class="needs-validation" novalidate>
<?php
$eventTicketQty = getInteger($_POST['eventTicketQty']);
$ticketEnhancerQty = getInteger($_POST['ticketEnhancerQty']);
$additionalContribution = getInteger($_POST['additionalContribution']);
?>
<input type="hidden" name="eventTicketQty" value="<?=$eventTicketQty?>" />
<div class="row"> <div class="row">
<div class="col-md-6 mb-3"> <div class="col-md-6 mb-3">
<label for="firstName">First name</label> <label for="firstName">First name</label>