So I have been working on a website and I tried to make a functional shopping cart with a checkout system. However, the shopping cart does not allow removal of added items in Mozilla Firefox, on other browsers it works well; I do not know why this is so.
Furthermore, my second problem is that in the checkout.php function, whenever I press the checkout button in the shopping cart page, it does not send the email of order to the business email. Is there a way how I can make this work please? Below I have put all the necessary code. Thank you.
cart.php:
<link rel="stylesheet" href = "styles/styling.css" media="all" />
<?php
session_start();
include("adminarea/includes/DBcon.php");
include ("functions/php1.php");
include ("header.php");
require 'obj.php';
?>
<body>
<?php
//Fetches information from the database and displays them with the help of obj.php
if(isset($_GET['pID'])){
$res = mysqli_query($connection, 'select * from product where pID='.$_GET['pID']);
$prod = mysqli_fetch_object($res);
$obj = new obj();
$obj->pID = $prod->pID;
$obj->pName = $prod->pName;
$obj->pPrice = $prod->pPrice;
$obj->qty = 1;
//to check if products exists in cart or not
$index = -1;
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0;$i<count($cart);$i++)
if($cart[$i]->pID==$_GET['pID'])
{
$index = $i;
break;
}
if($index==-1)
$_SESSION['cart'][] = $obj;
else{
$cart[$index]->qty++;
$_SESSION['cart']=$cart;
}
echo "
<script>
window.open('cart.php','_self')
</script>
";
}
if(!(isset($_SESSION['cart']))){
echo "
<script>
alert('Shopping cart is empty!')
</script>
";
echo "
<script>
window.open('products.php','_self')
</script>
";
}
//if statement to delete the chosen product inside the cart
if(isset($_GET['index']))
{
$cart = unserialize(serialize($_SESSION['cart']));
unset ($cart[$_GET['index']]);
$cart = array_values($cart);
$_SESSION['cart'] = $cart;
}
?>
<!-- This is to display the shopping cart table-->
<table cellpadding="5" cellspacing="4" border ="9" align="center" width="100%" border="9" bgcolor="darkred">
<td style="color:#FFF" colspan="10" align="center"><h2><u><i>Shopping Cart:</i></u></h2>
<tr>
<th style="color:#FFF">Option</th>
<th style="color:#FFF">Id</th>
<th style="color:#FFF">Name</th>
<th style="color:#FFF">Price</th>
<th style="color:#FFF">Quantity</th>
<th style="color:#FFF">SubTotal</th>
</tr>
<?php
$cart = unserialize(serialize($_SESSION['cart']));
$s = 0;
$index = 0;
for($i=0; $i<count($cart); $i++){
$s += $cart[$i] ->pPrice * $cart[$i]->qty;
?>
<tr>
<td>
<div class="shopcart">
<button style="width:150px; height:50px;">Remove</button></td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->pID; ?> </td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->pName; ?></td>
<td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice; ?></td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->qty; ?></td>
<td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice * $cart[$i]->qty;?></td>
</tr>
<?php }
$index++;
?>
<tr>
<td colspan="5" align="right" style="color:#FFF">Total</td>
<td style="color:#FFF" align="center">€<?php echo $s;?></td>
</tr>
</table>
<br>
<a id="a" style="margin-left: 10px;" href="products.php"> Go back</a><br><br>
<div id="checkout">
<form id="checkout" method="post" action="checkout.php">
<input id="input" type="submit" name="check" value="Checkout" style="background-color:gray; width:200px; margin-right: 10px;">
</div>
</div>
<?php include("footer.php") ?>
</body>
</html>
checkout.php:
<?php
//starting the session
session_start();
$from = "techologyy#gmail.com"; //from
$feedback = "Purchase details";
$to = "techologyy#gmail.com";//direction
$email = "Email from: $from \r\n";
mail("techologyy#gmail.com", $feedback, $from);
header("Location: index.php"); //returns the user back to homepage
echo "
<script>
alert('The checkout has been done successfully! Thank you')
</script>
";
?>
It looks to me like your mail() call is incorrect
The order of the parameters should be: to address, subject, email body and then sender. The sender should be in the format From: sender#email.com
So, adjust your code to something like this:
$from = "techologyy#gmail.com"; //from
$feedback = "Purchase details";
$to = "techologyy#gmail.com";//direction
$email = "Email from: $from \r\n";
mail($to, $feedback, $email, "From: $from");
Related
I have a PhP shopping cart for autoparts and I need to capture all the chosen products and send them to my email together with a contact form upon clicking "Submit order". As of now I've succeeded at sending just the first chosen product but I need to capture all the chosen products not just one.
I tried capturing the array itself with all the atributes of the chosen products but it's sending me only the first chosen product.
PhP
<?php
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM mystuff WHERE mscode='" . $_GET["mscode"] . "'");
$itemArray = array($productByCode[0]["mscode"]=>array('mscategory'=>$productByCode[0]["mscategory"], 'mscatnum'=>$productByCode[0]["mscatnum"], 'msnomer'=>$productByCode[0]["msnomer"], 'msmark'=>$productByCode[0]["msmark"], 'msmodel'=>$productByCode[0]["msmodel"], 'msyear'=>$productByCode[0]["msyear"],'mscode'=>$productByCode[0]["mscode"], 'quantity'=>$_POST["quantity"], 'msprice'=>$productByCode[0]["msprice"], 'msimage'=>$productByCode[0]["msimage"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["mscode"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["mscode"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>
HTML
<HTML>
<HEAD>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</HEAD>
<div class="bodyr">
<div id="shopping-cart">
<div class="txt-heading"></div>
<?php
if(isset($_SESSION["cart_item"])){
$total_quantity = 0;
$total_price = 0;
?>
<table class="tbl-cart" cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;" width="2%">Category</th>
<th style="text-align:left;" width="2%">Category №:</th>
<th style="text-align:left;" width="2%">Stock №:</th>
<th style="text-align:left;" width="2%">Mark:</th>
<th style="text-align:left;" width="2%">Model:</th>
<th style="text-align:left;" width="2%">Year:</th>
<th style="text-align:left;" width="2%">Quantity:</th>
<th style="text-align:left;" width="2%">Price:</th>
<th style="text-align:left;" width="2%">Total price:</th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
$item_price = $item["quantity"]*$item["msprice"];
?>
<tr>
<td><?php echo $item["mscategory"]; ?></td>
<td><?php echo $item["mscatnum"]; ?></td>
<td><?php echo $item["msnomer"]; ?></td>
<td><?php echo $item["msmark"]; ?></td>
<td><?php echo $item["msmodel"]; ?></td>
<td><?php echo $item["msyear"]; ?></td>
<td style="text-align:left;"><?php echo $item["quantity"]; ?></td>
<td style="text-align:left;"><?php echo "$ ".$item["msprice"]; ?></td>
<td style="text-align:left;"><?php echo "$ ". number_format($item_price,2); ?></td>
</tr>
<?php
$total_quantity += $item["quantity"];
$total_price += ($item["msprice"]*$item["quantity"]);
}
?>
<tr>
<td colspan="7" align="left"> <div style="font-weight:bold; font-size:14px"> Total:</div></td>
<td align="left"><?php echo $total_quantity; ?></td>
<td align="right" colspan="2"><strong><?php echo "$ ".number_format($total_price, 2); ?></strong></td>
</tr>
</tbody>
</table>
<?php
} else {
?>
<div class="no-records">Your cart is empty</div>
<?php
}
?>
</div>
<form action="" method="post">
<input type="text" name="namet" placeholder="Name: *" required>
<input type="tel" name="adrphonenumbert" placeholder="Phone number: *" required>
<input type="email" name="emailt" placeholder="E-mail: *" required>
</form>
</html>
PhP Mail() function
<?php
$response = '';
$subject = 'Autoparts';
if (isset($_POST['namet'], $_POST['phonenumbert'], $_POST['emailt'] )) {
if (!filter_var($_POST['emailt'], FILTER_VALIDATE_EMAIL)) {
$response = 'The e-meil address is not valid!';
} else if (empty($_POST['namet']) || empty($_POST['phonenumbert']) || empty($_POST['emailt'])) {
$response = 'Please, fill all the fields.';
} else {
$namet = $_POST['namet'];
$phonenumbert = $_POST['phonenumbert'];
$emailt= $_POST['emailt'];
$to = 'LLstdz#gmail.com';
foreach ($_SESSION["cart_item"] as $itemArray){
if ($itemArray > 0){
$txt = '<h2>Autoparts purchase:</h2>
<p><b>Name:</b> '.$namet.'</p>
<p><b>Phone number:</b> '.$phonenumbert.'</p>
<p><b>E-meil:</b> '.$emailt.'</p>
<p><b>Category:</b><br/>'.$itemArray["mscategory"].'</p>;
<p><b>Category №:</b><br/>'.$itemArray["mscatnum"].'</p>;
<p><b>Stock №:</b><br/>'.$itemArray["msnomer"].'</p>;
<p><b>Mark:</b><br/>'.$itemArray["msmark"].'</p>;
<p><b>Model:</b><br/>'.$itemArray["msmodel"].'</p>;
<p><b>Year:</b><br/>'.$itemArray["msyear"].'</p>;
<p><b>Quantity:</b><br/>'.$itemArray["quantity"].'</p>;
<p><b>Price:</b><br/>'.$itemArray["msprice"].'</p>';
};
}
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $txt, $headers);
echo '<div style="font-weight:bold;font-size:20px;text-align:center">Thank you for using our services.</div>' ;
}
}
?>
This is because you should concatenate the string $txt, so use .= instead =
At the time you are sending only last item in $_SESSION["cart_item"] (not first)
for your case:
$txt = '';
foreach ($_SESSION["cart_item"] as $itemArray){
if ($itemArray > 0){
$txt .= '<h2>Autoparts purchase:</h2>
<p><b>Name:</b> '.$namet.'</p>
<p><b>Phone number:</b> '.$phonenumbert.'</p>
<p><b>E-meil:</b> '.$emailt.'</p>
<p><b>Category:</b><br/>'.$itemArray["mscategory"].'</p>;
<p><b>Category №:</b><br/>'.$itemArray["mscatnum"].'</p>;
<p><b>Stock №:</b><br/>'.$itemArray["msnomer"].'</p>;
<p><b>Mark:</b><br/>'.$itemArray["msmark"].'</p>;
<p><b>Model:</b><br/>'.$itemArray["msmodel"].'</p>;
<p><b>Year:</b><br/>'.$itemArray["msyear"].'</p>;
<p><b>Quantity:</b><br/>'.$itemArray["quantity"].'</p>;
<p><b>Price:</b><br/>'.$itemArray["msprice"].'</p>';
};
}
By the way, I strongly recommend to use phpmailer (https://github.com/PHPMailer/PHPMailer) or swiftmail for sending email.
when i clicked the link it does not redirect to the page that specified.i have the sqlite database and there is a database column for status(added,updated like wise).i want to redirect to the page when i clicked the status link in my link.php.Now Object not found error is getting.
this is my link.php code below.
Please can you help me guys.
<?php
// Includs database connection
include "db_connect.php";
// Makes query with rowid
$query = "SELECT rowid, * FROM registration";
// Run the query and set query result in $result
// Here $db comes from "db_connection.php"
$result = $db->query($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Data List</title>
<script>
function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=1076,height=768,directories=no,location=no')
}
</script>
</head>
<body>
<br>
<br>
<!-- Add New-->
<table>
<tbody>
<tr>
<th style="">status</th>
<th style="">submitted</th>
<th style="">department</th>
<th style="">head</th>
<th style="">title</th>
<th style="">applicant</th>
<th style="">date</th>
</tr>
<?php while($row = $result->fetchArray()) {?>
<!--<tr class="table-row" data-href="update.php?id=<?php echo $row['rowid'];?>" data-target="_blank">-->
<tr>
<td>
</td>
<td>view
<?php
if ($row['rowid'] == "added"){
echo "<a href='updated.php" . $row['submitted'] . "updated'> </a>";
} else {
echo "<a href='next.php" . $row['submitted'] . "next'> </a>";
}
?>
</td>
<td><?php echo $row['status'];?></td>
<td><?php echo $row['submitted'];?></td>
<td><?php echo $row['department'];?></td>
<td><?php echo $row['head'];?></td>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['applicant'];?></td>
<td><?php echo $row['date'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
<script>
var tableRows = document.getElementsByClassName('table-row');
for (var i = 0, ln = tableRows.length; i < ln; i++) {
tableRows[i].addEventListener('click', function() {
window.open(this.getAttribute('data-href'), this.getAttribute('data-target'));
});
}
</script>
<br>
<br>
</body>
</html>
How can you pass dynamic details which are retrieved from a database (e.g. details in a shopping cart table) from one page to another page so that you could send them via email using the mail() function?I tried many ways using the "$message" but none worked. I am new to PHP and do not have much experience with it yet. Any help would be appreciated, thank you.
page 1:
<?php session_start();
//starting the session
include("adminarea/includes/DBcon.php");
include ("functions/php1.php");
include ("header.php");
require 'obj.php';
?>
<link rel="stylesheet" href = "styles/styling.css" media="all" />
<body>
<?php
//Fetches information from the database and displays them with the help of obj.php
if(isset($_GET['pID'])){
$res = mysqli_query($connection, 'select * from product where pID='.$_GET['pID']);
$prod = mysqli_fetch_object($res);
$obj = new obj();
$obj->pID = $prod->pID;
$obj->pName = $prod->pName;
$obj->pPrice = $prod->pPrice;
$obj->qty = 1;
//to check if products exists in cart or not
$index = -1;
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0;$i<count($cart);$i++)
if($cart[$i]->pID==$_GET['pID'])
{
$index = $i;
break;
}
if($index==-1)
$_SESSION['cart'][] = $obj;
else{
$cart[$index]->qty++;
$_SESSION['cart']=$cart;
}
echo "
<script>
window.open('cart.php','_self')
</script>
";
$_SESSION['pID'] = $_POST['pID'];
$_SESSION['pName'] = $_POST['pName'];
$_SESSION['pPrice'] = $_POST['pPrice'];
$_SESSION['qty'] = $_POST['qty'];
}
if(!(isset($_SESSION['cart']))){
echo "
<script>
alert('Shopping cart is empty!')
window.location.href='index.php';
</script>
";
}
//if statement to delete the chosen product inside the cart
if(isset($_GET['index']))
{
$cart = unserialize(serialize($_SESSION['cart']));
unset ($cart[$_GET['index']]);
$cart = array_values($cart);
$_SESSION['cart'] = $cart;
}
?>
<!-- This is to display the shopping cart table-->
<table cellpadding="5" cellspacing="4" border ="9" align="center" width="100%" border="9" bgcolor="darkred">
<td style="color:#FFF" colspan="10" align="center"><h2><u><i>Shopping Cart:</i></u></h2>
<tr>
<th style="color:#FFF">Option</th>
<th style="color:#FFF">Id</th>
<th style="color:#FFF">Name</th>
<th style="color:#FFF">Price</th>
<th style="color:#FFF">Quantity</th>
<th style="color:#FFF">SubTotal</th>
</tr>
<?php
$cart = unserialize(serialize($_SESSION['cart']));
$s = 0;
$index = 0;
for($i=0; $i<count($cart); $i++){
$s += $cart[$i] ->pPrice * $cart[$i]->qty;
?>
<tr>
<td>
<div class="shopcart">
<input id="input" type="submit" name="ctable"/>Remove</input></td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->pID; ?> </td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->pName; ?></td>
<td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice; ?></td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->qty; ?></td>
<td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice * $cart[$i]->qty;?></td>
</tr>
<?php }
$index++;
?>
<tr>
<td colspan="5" align="right" style="color:#FFF">Total</td>
<td style="color:#FFF" align="center">€<?php echo $s;?></td>
</tr>
</table>
<br>
<a id="a" style="margin-left: 10px;" href="products.php"> Go back</a><br><br>
<div id="checkout">
<form id="checkout" method="post" action="checkout.php">
<input id="input" type="submit" name="check" value="Checkout" style="background-color:gray; width:200px; margin-right: 10px;">
</div>
</div>
<?php include("footer.php") ?>
</body>
</html>
page 2:
<?php session_start();
require 'obj.php';
include("adminarea/includes/DBcon.php");
$to = "techologyy#gmail.com";//direction
$subject = "Purchase Details:";
$message = $_SESSION['pID'];
$message .= $_SESSION['pName']."\r\n";
$message .= $_SESSION['pPrice']."\r\n";
$message .= $_SESSION['qty']."\r\n";
$headers = 'From: techologyy#gmail.com' . "\r\n"; //from
//mail paramter with correct order
mail($to, $subject, $message, $headers);
//echo to display alert
echo "
<script>
alert('The checkout has been done successfully! Thank you')
window.location.href='index.php';
</script>
"; //returns the user back to homepage
?>
Please specify what you have tried so far.
you want to send the data from one page to another, try using form tag in html
and set the method attribute to post and action attribute to where you want to send the data.
The html code will look like this
<form action="post" action="getdata.php"> <input id="val" type="text" value="" name="cart" style="display:none;"> <button type="submit" >hit me</button> </form>
You can set the value of input using javascript
document.getElementById("val").value="YourValue";
getdata.php will look like this
> if ($_SERVER["REQUEST_METHOD"] == "POST"){
> $cartval=$_POST['cart'];
> echo $cartval; }
be sure to validate the data and check for any hidden code before executing the user input
I am trying to redirect the page back to general_faq_category route but when it is redirected the data is not loaded correctly. So i manually refresh the page and then the data is loaded correctly (sorted). Why is the page not refreshing automatically? It should load the data sorted but it isn't loading when the below func is executed, however after manually refreshing the route the changes appear.
The following is the code i used for redirecting
Function for sorting data:
function executeFaqCategoryOrder(sfWebRequest $request) {
$faqid = $request->getParameter('faqid');
$direction = $request->getParameter('direction');
if ($faqid & $direction) {
$c = new Criteria();
$c->add(FaqCategoryPeer::ID, $faqid);
$current_faq = FaqCategoryPeer::doSelectOne($c);
$current_order = $current_faq->getOrder();
switch ($direction) {
case 'UP':
$c = new Criteria();
$c->add(FaqCategoryPeer::ORDER, $current_order - 1);
$previous_faq = FaqCategoryPeer::doSelectOne($c);
$current_faq->setOrder($previous_faq->getOrder());
$current_faq->save();
$previous_faq->setOrder($current_order);
$previous_faq->save();
$this->redirect($this->generateUrl('general_faq_category'));
break;
break;
case 'DOWN':
$c = new Criteria();
$c->add(FaqCategoryPeer::ORDER, $current_order + 1);
$next_faq = FaqCategoryPeer::doSelectOne($c);
$current_faq->setOrder($next_faq->getOrder());
$current_faq->save();
$next_faq->setOrder($current_order);
$next_faq->save();
$this->redirect($this->generateUrl('general_faq_category'));
break;
break;
default:
$this->redirect($this->generateUrl('general_faq_category'));
break;
}
$this->redirect($this->generateUrl('general_faq_category'));
}
}
Another Func from actions.php
public function preExecute() {
self::$formName = "FaqCategoryAdminForm";
self::$modelPeer = "FaqCategoryPeer";
self::$model = "FaqCategory";
$this->redirectUrl = 'faq_category/index';
}
View Code:
<div class="pagead">
<div class="pagein"align="center">
<div align="center">
<h3>FAQ Category (Total: <?php echo $total ?>)</h3>
<span style="float: right;">
Back |
New FAQ Category
</span>
</div>
<br/>
<div align="center"><?php $pager->render() ?></div>
<table width="100%" id="category">
<tr>
<th width="30%">Name</th>
<th width="15%">Created At</th>
<th width="5%">Order</th>
<th width="12%">Action</th>
</tr>
<?php foreach ($pager->getResults() as $row): ?>
<tr id="category_<?php echo $row['id'] ?>" class="categories">
<td align="center"><?php echo $row['name']; ?></td>
<td align="center"><?php echo date(sfConfig::get('app_display_alternate_format_for_date'), strtotime($row['created_at'])); ?></td>
<td>
▲
▼
</td>
<td align="center">
<?php echo link_to(image_tag("/images/edit.png", array("title" => "Click to modify category")), $sf_params->get('module') . '/edit?id=' . $row['id']); ?>
<?php echo link_to(image_tag("/images/cross.png", array("title" => "Click to delete category")), $sf_params->get('module') . '/delete?id=' . $row['id'], array('confirm' => 'All related records will be deleted, are you sure ?')); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<br/>
<br/>
</div>
<div class="bot"></div>
I have a page that contains an ordering form, on this form it lists the vendor information and then each of the products for the vendor underneath and in front of the product is an input field that allows the user to input the quantity of each product that they want.
Upon submitting the information goes to a confirmation page where I need to be able to show the order information. On the form on the order page, I have a hidden field that contains the vendor id. and the vendor id is put once for each vendor. What I need to be able to do is not only echo out the quantity but also echo out the vendor id specific for each order. My code is below. The first block is the order page and then the block below that will be the confirm page.
As it stands right now underneath every quantity it displays all the vendor ids as opposed to just the one I need.
<?php defined('C5_EXECUTE') or die("Access Denied.");?>
<div class="ccm-ui">
<?php
$db= Loader::db(); //This loads the database helper.
Loader::model('user'); //This loads the user Model.
$user = new User();
$userInfo = UserInfo::getByID($user->getUserID()); //This gets the user info for the current user.
$userCostCenter = $userInfo->getAttribute('cost_center'); //This sets a variable equal to the attribute Cost Center for the current user.
//The if statement below checks if the user is an admin and then displays the info accordingly.
if ($userCostCenter === "Admin") {
?>
<form name="SelectCostCenter" action="/adminorder" method="POST">
<select name="CostCenter">
<option value="unitedilluminating">United Illumination</option>
<option value="clp">CL&P</option>
</select>
<input type="submit" value="Continue">
<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
<?php
} elseif ($userCostCenter === "United Illuminating") {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php
$query = 'SELECT * FROM Vendors WHERE costCenterID = 1';
$productQuery = 'SELECT * FROM Products WHERE costCenterID = 1';
$results = $db->getAll($query);
$productResults = $db->getAll($productQuery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category']; ?></td>
<td><?php echo $vendor['Vendor']; ?></td>
<td><?php echo $vendor['Address']; ?></td>
</tr>
<?php foreach ($productResults as $product) { ?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[]" size="1" /><?php echo $product['Product'];?></span></td>
</tr>
<?php } ?>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
else {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php $query = 'SELECT * FROM Vendors Where costCenterID = 2';
$productquery = 'SELECT * FROM Products WHERE costCenterID = 2';
$results = $db->getAll($query);
$productresults = $db->getAll($productquery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category'];?></td>
<td><?php echo $vendor['Vendor'];?> </td>
<td><?php echo $vendor['Address'];?></td>
</tr>
<?php
foreach ($productresults as $product){
?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[<?php echo $vendor['vendorID']; ?>]" size="1" /><?php echo $product['Product'];?></span></td>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
</tr>
<?php
}
?>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
?>
This is the confirm page below.
<?php defined('C5_EXECUTE') or die("Access Denied.");
$db= Loader::db();
$quantity = $_POST['quantities'];
$vendor = $_POST['vendor'];
$minimumorder = 25;
foreach($quantity as $num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
foreach($vendor as $vendors){
echo "$vendors";
echo "</br>";
}
}
}
?>
I appreciate any help anyone can give. This has had me stumped for a few days actually.
you might want to rearrange your array, and do something like:
$i = 0;
foreach ($productresults as $product) {
echo '<input name="product['.$i.'][quantity]" />';
echo '<input name="product['.$i.'][vendor_id]" value="'.$vendor['vendorID'].'" type="hidden" />';
++$i;
}
The resulting array in $_POST would have the quantities & their vendor separated into their own arrays.
In your code $vendor['vendorID'] seems the key of your $_POST['quantities'] so in your confirm page you could use:
foreach($quantity as $vendorid=>$num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
echo "$vendorid";
}
}