Accessing "THIS" variable outside of string - php

I have adapted a shopping cart program which builds a series of items based on POSTED form data and combines that data to form the shopping cart. Each item is given a line in the cart using the $this command:
foreach($this->get_contents() as $item) {
echo tab(7) . "<input type='hidden' id='jcartItempartnumber-{$item['id']}'
name='jcartItempartnumber[]' value='{$item['partnumber']}' />
Part Number: {$item['partnumber']} \n";
}
So each line item accepts and removes the Part Numbers without issue. The issue becomes That I can't put these part numbers down in the footer of the cart itself with the subtotal.
echo tab(7) . "<span id='jcart-subtotal'>Cost: <strong>$currencySymbol" .
number_format($this->subtotal, $priceFormat['decimals'], $priceFormat['dec_point'],
$priceFormat['thousands_sep']) . "</strong></span>\n";
Is there an easy way to display the unique part numbers down below? How do I reference a $this variable outside of the $this string?
----EDIT----
Item 1 has several values.
<input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
<input type="hidden" name="my-item-id" value="2" />
<input type="hidden" name="my-item-name" value="Instrumentation Enclosure" />
<input type="hidden" name="my-item-price" value="0" />
<input type="hidden" name="my-item-partnumber" value="In" />
<input type="hidden" name="my-item-qty" value="1" size="3" />
For each cart option there is only a few values
for any item with an item ID of '2' there are only two possibilities; "R" and "In"
I just want to create a variable which echo's either R or In when the ID=2

Related

PHP Paypal express 10413

I'm using Paypal Express Checkout on my website and i receive this error:
SetExpressCheckout API call failed. Detailed Error Message: The totals of the cart item amounts do not match order amounts.Short Error Message: Transaction refused because of an invalid argument. See additional error messages for details.Error Code: 10413Error Severity Code: Error
When i enter a quantity of 1 everything is working fine but when i enter 2 or more i get the error.
I'm using SMARTY Templating.
<form class="pull-right" action="../Checkout/paypal_ec_redirect.php" method="POST">
<input type="hidden" name="L_PAYMENTREQUEST_0_NAME0" value="{$product_naam}"></input>
<input type="hidden" name="L_PAYMENTREQUEST_0_AMT0" value="{$product_productprijs}"></input>
<input type="hidden" name="L_PAYMENTREQUEST_0_QTY0" value="{$product_aantal}"></input>
<input type="hidden" name="PAYMENTREQUEST_0_ITEMAMT" value="{$product_subtotaal}"></input>
<input type="hidden" name="PAYMENTREQUEST_0_TAXAMT" value="{$product_btw}"></input>
<input type="hidden" name="PAYMENTREQUEST_0_AMT" value="{$product_totaalprijs}"></input>
<input type="hidden" name="currencyCodeType" value="EUR"></input>
<input type="hidden" name="paymentType" value="Sale"></input>
<input type="image" src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" alt="Check out with PayPal"></input>
</form>
</div>
</div>
</div>
Here is the PHP:
foreach ($getproduct as $row) {
$productaantal = $aantalbesteld;
$productnaam = $row['naam'];
$productprijs = $row['prijs'];
$btwtarief = $row['btw_tarief'];
$btwbedrag = ($productprijs*$aantalbesteld)/100*$btwtarief;
$subtotaal = ($productprijs*$aantalbesteld)-$btwbedrag;
$totaalprijs = $productprijs*$aantalbesteld;
}
//Product
$smarty->assign('product_naam', $productnaam);
$smarty->assign('product_aantal', $productaantal);
$smarty->assign('product_prijs', money_format('%.2n', $productprijs));
$smarty->assign('product_subtotaal', money_format('%.2n', $subtotaal));
$smarty->assign('product_btw', money_format('%.2n', $btwbedrag));
$smarty->assign('product_totaalprijs', money_format('%.2n', $totaalprijs));
$smarty->assign('product_bestelknop', 'Bestellen');
EDIT: Even this isn't working:
<form class="pull-right" action="../Checkout/paypal_ec_redirect.php" method="POST">
<input type="hidden" name="L_PAYMENTREQUEST_0_NAME0" value="{$product_naam}"></input>
<input type="hidden" name="L_PAYMENTREQUEST_0_AMT0" value="5.00"></input>
<input type="hidden" name="L_PAYMENTREQUEST_0_QTY0" value="2"></input>
<input type="hidden" name="PAYMENTREQUEST_0_ITEMAMT" value="10.00"></input>
<input type="hidden" name="PAYMENTREQUEST_0_TAXAMT" value="2.10"></input>
<input type="hidden" name="PAYMENTREQUEST_0_AMT" value="12.10"></input>
<input type="hidden" name="currencyCodeType" value="EUR"></input>
<input type="hidden" name="paymentType" value="Sale"></input>
<input type="image" src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" alt="Check out with PayPal"></input>
</form>
It says when they multiply quantity and amount it doesn't add up to what you specify as totalamount
$btwbedrag = ($productprijs*$aantalbesteld)/100*$btwtarief;
$subtotaal = ($productprijs*$aantalbesteld)-$btwbedrag;
$totaalprijs = $productprijs*$aantalbesteld;
Your VAT is calculated by taking 1% of the total price amount multiply by VAT rate. This assumes your product amounts are exclusive VAT.
Then in the next line you remove VAT from $subtotaal, why? It wasn't included in the first place. Or if it was then you should divide by 121 instead of 100.
$totaalprijs seems to be calculated assuming VAT is included already.
Did you check the values of your hidden fields in the source of your page?
When I changed the qty to 2, this is the request that is sent to PayPal.
paymentrequest_0_currencycode "EUR"
paymentrequest_0_amt "89.90"
paymentrequest_0_itemamt "71.02"
paymentrequest_0_taxamt "18.88"
paymentrequest_0_paymentaction "Sale"
paymentrequest_0_name "Sweaters van Superdry"
paymentrequest_0_qty "2"
Ideally, the request should be like this:
paymentrequest_0_itemamt=71.02
paymentrequest_0_taxamt=18.88
paymentrequest_0_amt=89.90
L_PAYMENTREQUEST_0_NAME0 = Sweaters van Superdry
L_PAYMENTREQUEST_0_AMT0= 35.51
L_PAYMENTREQUEST_0_QTY0= 2
This is causing the problem.
There is no paymentrequest_0_qty parameter.
You can test it here :
https://api-3t.sandbox.paypal.com/nvp?&user=us-30_api1.cri.com&pwd=EYFNSNUSV85CT34Z&signature=AH57zE.nAaElaFFAysViNA9TIte1AxtSpBjx2HLqHJOiu2js3l1Kd48i&version=70.0&METHOD=SetExpressCheckout&RETURNURL=http://www.paypal.com/test.php&CANCELURL=http://www.paypal.com/test.php&PAYMENTACTION=Sale&paymentrequest_0_itemamt=71.02&paymentrequest_0_taxamt=18.88&paymentrequest_0_amt=89.90&L_PAYMENTREQUEST_0_NAME0=Sweateranuperdry&L_PAYMENTREQUEST_0_AMT0=35.51&L_PAYMENTREQUEST_0_QTY0=2
Just click/copy-pasate the link above and see the reponse

PHP: Adding multiple products to post order form

I'm building a sort of php shop that will allow a user to enter amounts for various products and then a form will process the order. There is no payment processing etc. What I am struggling to do is add each product to each amount to send through to the order form as a $_POST variable. Currently my ordered items are a standard form:
<input type="text" id="value" name="ordered-items[]" />
I thought I could add the product ID as a hidden variable, but that will just add it for every product.
<input type="hidden" id="value" name="products[<?php the_id(); ?>]" />
How can I marry up the product to the order quantity and then send that to the order form for processing, this part I can do myself.
Thanks In advance
You can name the product quantity based on it's ID like this:
<input type="text" name="item[<?php the_id(); ?>]" />
You can then access the product quantities on the server side using for each:
foreach($_POST as $index => $value){
// $index is product id and $value is product quantity
}
Why not put the quantity in a separate input field. In case of a normal text field the visitor can then even change the quantity:
<input type="text" id="item1" name="ordered-items[]" /><input type="text" id="qty1" name="quantity[]" />
<br>
<input type="text" id="item2" name="ordered-items[]" /><input type="text" id="qty2" name="quantity[]" />
<br>
<input type="text" id="item3" name="ordered-items[]" /><input type="text" id="qty3" name="quantity[]" />
In php you can process the data like this:
<?php
$items = $_POST['ordered-items'];
$quantities = $_POST['quantity'];
for($i = 0; $i < count($items); $i++) {
echo $items[$i] . ' has quantity ' . $quantities[$i];
}

Keep printing same form until submit button is clicked

Hi I'm quite new to php and I'm currently making a webpage similar to the ones used by supermarkets for stock management control as part of an assignment. I have the following form where the cashier would enter the product Id and the quantity of the item being purchased.
The form will then call another php file named cashsale.php which will take these inputs and update the tables in my database so that levels of stock on shelves in supermarkets are up to date with the new amounts (i.e. older qty - qty entered) and management can be advised when reorder is needed.
As it is the form works well, however I was advised to edit it in a way that a cashier can enter multiple products and quantities before submitting (i.e. the form will sort of show itself again) and allow the user to edit or remove any items before actually submitting the values to cashsale.php to manipulate the tables. I seem to be at a loss as to how this can be done.
I wanted to create a new button named "Add" which would display the form again, i.e. allow the user to check in more items, but I am confused as to how this can be done and also as to how I will be able to update tables then since I would be having more then just 2 inputs.
Can anyone help me on this please? Thanks in advance. The following is my html form:
center form action="cashsale.php" method ="post"
Product ID: <input name= "id" type="int" > <br>
Quantity:<input name="qty" type="int">
<input type="button" name = "Add" onclick="add">
<input type="Submit" name ="Submit" value = "Submit">
form center
I was not allowed to use html tags for form and center so I removed the < >. The following is some of the modifications done in the cashsale.php file just to give a clearer example.
$result = mysql_query("SELECT * FROM shelfingdetails where prodId =' " .$id. " '");
if (!$result){
die (mysql_error());
}
while ($row =mysql_fetch_array($result))
{
$qtyOnShelf= $row ['QtyOnShelf'];
$max=$row['max'];
$newQtyShelf=$qtyOnShelf-$qty;
}
$update=mysql_query("UPDATE shelfingdetails SET QtyOnShelf ='". $newQtyShelf. "' where prodId = '". $id. "';");
I hope someone can help. Thanks!
You just have to pass an array. For this you have to generate the inputs with PHP or javascript (I'm gona use jQuery to keep the code nice and simpe).
If you use PHP:
// PHP
<?php
if(isset($_POST['submit']) && $_POST['submit']){
//save data
} elseif(isset($_POST['Add']) && $_POST['Add']) {
$max = (int)$_POST['max']
} else { $max = 1; }
?>
<form action="" method="post">
<?php
for($i = 0;$i < $max;$i++){
?>
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int">
<?php
}
?>
<input type="hidden" name="max" value="<?= $i; ?>" />
<input type="submit" name="Add" />
<input type="submit" name="Submit" value="Submit" />
</form>
If yo use Javascript:
//HTML
<form action="" method="post" id="form">
<div id="add">
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int">
</div>
<input type="hidden" name="max" value="<?= $i; ?>" />
<input type="button" name="Add" onclick="addRow();" />
<input type="submit" name="Submit" value="Submit" />
</form>
// jQuery
function addRow(){
$("#add").append("<br />Product ID: <input name='id[]' type='int' >" +
"<br />Quantity: <input name='qty[]' type='int'>");
}
you have to use id[] and qty[] to pass them as an array and with the add button generate as many of them as you need. Like so:
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int"> <br>
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int"> <br>
Product ID: <input name="id[]" type="int" > <br>
Quantity: <input name="qty[]" type="int">
<input type="button" name = "Add" onclick="add">
Then on the backand use for loop to save all the data.
$max = count($_POST['id']);
$id = $_POST['id'];
$newQtyShelf = $_POST['qty'];
for($i = 0;$i < $max;$i++){
$update=mysql_query("UPDATE shelfingdetails
SET QtyOnShelf ='". (int)$newQtyShelf[i]. "'
WHERE prodId = '". (int)$id[i]. "';");
}
I just wanted to show you the idea, please don't use this specific code, because you should use mysqli instead of mysql and also mysqli_escape_string to make sure that the user not submits incorrect data.

PHP How to GET the Form variables on multiple items?

I need to collect the form elements of a cart. The items are in multiples. I wish to collect them as a session or easy to use Array - I think the Array would be best?
How can I collect this information in process.php?
I hope I have made it clear.
The code is like this for each item:
<div class="product" id="pId_'.$id.'">
<input type="hidden" name="productID" value="'.$id.'" />
<input type="hidden" name="productURL" value="'.$url.'" />
<input type="hidden" name="productQty" value="'.$qty.'" />
<input type="hidden" name="productPrice" value="'.$price.'" />
</div>
So the use cart could have :
<div class="product" id="pId_2">
<input type="hidden" name="productID" value="2" />
<input type="hidden" name="productURL" value="site.com" />
<input type="hidden" name="productQty" value="1" />
<input type="hidden" name="productPrice" value="750" />
</div>
<div class="product" id="pId_45">
<input type="hidden" name="productID" value="45" />
<input type="hidden" name="productURL" value="example.co.uk" />
<input type="hidden" name="productQty" value="2" />
<input type="hidden" name="productPrice" value="100" />
</div>
These details are submitted to the form.. but how can I collect when productID is called twice or more?
If you just need to collect the items for processing later, another way of doing it is to loop through all the POST variables. PHP will collect all information submitted into a reserved variable, $_POST.
From there you could use a foreach to loop through the information collected as follows;
foreach ($_POST as $key => $value)
{
echo "$key = $value";
}
Where $key would be your someitem_ and $value would contain the actual value submitted.
This would work easier provided you did not have any other inputs in your form other then those of the shopping cart items, if not you'll have to do some logic to determine which were the records that were associated with your cart items.
On a side note, if it is possible to combine the 3 related input values into just 1, with the values separated by a character you defined (maybe something like <item>|<name>|<url>), it might make your life easier when trying to get all the 3 values that are associated with the id.
In that case your code would just get the string value for the specific id and do a split() on the '|' to break it up back into its 3 values.
If you add square-brackets to your input names
<input type="hidden" name="productID[]" value="' . $id . '" />
<input type="hidden" name="productURL[]" value="' . $url . '" />
<input type="hidden" name="productQty[]" value="' . $qty . '" />
<input type="hidden" name="productPrice[]" value="' . $price . '" />
you can then collect and use all form data by adding the following PHP code to your process.php file
foreach ($_POST['productID'] as $key => $getid) {
$getqty = $_POST['productQty'][$key];
$getprice = $_POST['productPrice'][$key];
$geturl = $_POST['productURL'][$key];
// Example MySQL Query
mysql_query("INSERT INTO orders (id, url, quantity, price) VALUES ('" . $getid . "', '" . $geturl . "', '" . $getqty . "', '" . $getprice . "')");
}
What this does is that it automatically creates arrays for the form data so that you can, using the PHP code above, collect the correct data from each row by using the index number via the $key variable.
I believe in your process.php form you could just store them in an array when you pull in the post data. You will need to pass the largest value of $id as a value as well so you can loop through the appropriate amount of items.
The top of your process.php might look like
<?php
$processArray = array();
$id = $_POST["id"];
$i = 0;
while ($i <= $id) {
$processArray = ('item' => $_POST["someitem"_.$i], 'name' => $_POST["somename"_.$i], 'url' => $_POST["someurl"_.$i]):
//Now you can do what you want with each value. Display it somewhere, store it somewhere, etc.
$i++;
}
I did this off the top of my head but that's what comes to mind when I look at your issues.
I'm not entirely sure of what you're asking. But you can retrieve arrays from $_POST if the field names are appended with [].
<input name="myArray[1]" value="foo" />
<input name="myArray[37]" value="bar" />
Would give you
array(
1 => 'foo',
37 => 'bar'
)
<form action="process.php" method="post">
These elements are looped pending on cart items
<input type="hidden" name="someitem['.$id.']" value="1" size="1" maxlength="1" />
<input type="hidden" name="somename['.$id.']" value="1" size="1" maxlength="1" />
<input type="hidden" name="someurl['.$id.']" value="1" size="1" maxlength="1" />
</form>
Use:
<input type="hidden" name="someitem[]" value="1" size="1" maxlength="1" />
And then in PHP $_GET["someitem"] will be an array
EDIT: Use $_POST["someitem"]. I had assumed you needed GET, because of the title of the question.

How do you manipulate form values based off local input?

I have a form that has a series of check boxes and some line items have a text input next to them that defines quantity of the item.
<input type="checkbox" name="measure[][input]" value="<?=$item->id?>">
<input class="item_mult" type="text" name="measure[][input]" />
What is the best way to capture the integer from the input field and have it correspond to the check box so I can use it to calculate the total later on?
<input type="checkbox" name="measure[<?php echo $item->id; ?>][checked]" value="<?php echo $item->id; ?>">
<input class="item_mult" type="text" name="measure[<?php echo $item->id; ?>][input]" />
That should give the values $measure[1]['checked'] (only present if checked) and $measure[1]['input']
I corrected your short tags - they're a bad idea, as they are disabled in php by default so can cause issues if you move servers
You can give your array a name/id to associate them, just add it into the name attribute:
<input type="checkbox" name="measure[1][my_checkbox]" value="<?=$item->id?>">
<input class="item_mult" type="text" name="measure[1][my_text]" />

Categories