Session creation issue in shopping cart - php

In my code for shopping cart, sessions are not formed dynamically. I want to create session in this page and use sessions on the other page for item cart details.
How do I solve this problem?
<form method="post" action="men.php?action=add&id=<?php echo
$product['id']; ?>">
<img src="<?php echo $product['image'];?>">
<img class="hover-img" src="<?php echo $product['h_image'];?>" >
<p><?php echo $product['price'];?></p>
<h6><?php echo $product['name'];?></h6>
<input type="hidden" name="id" value="<?php echo $product['id']; ?>" />
<input type="hidden" name="hidden_name" value="<?php echo
$product['name']; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo
$product['price']; ?>" />
<input type="hidden" name="hidden_image" value="<?php echo
$product['image']; ?>" />
Quantity <input type="text" class="form-control" name="qty" value="1" /><br>
<input type="submit" name="cart" value="Add to Cart" />
</form>
<?php
session_start();
if(isset($_POST['cart']))
{
// not working
if(isset($_SESSION['shopping_cart']))
{
$count= count($_SESSION['shopping_cart']);
$product_ids= array_column($_SESSION['shopping_cart'],'id');
if(!in_array(filter_input(INPUT_GET,'id'), $product_ids))
{
$_SESSION['shopping_cart'][$count]=array
(
'id'=> filter_input(INPUT_GET,'id'),
'name'=> filter_input(INPUT_POST,'hidden_name'),
'price'=> filter_input(INPUT_POST,'hidden_price'),
'quantity'=> filter_input(INPUT_POST,'qty'),
'image'=> filter_input(INPUT_POST,'hidden_image')
);
}
else
{
for($i=0; $i<count($product_ids); $i++)
{
if($product_ids[$i]==filter_input(INPUT_GET,'id'))
{
$_SESSION['shopping_cart'][$i]['quantity']+=
filter_input(INPUT_POST,'quantity');
}
}
}
}
else
{
}
}
?>

Sara. The session_start(); should be the first line in your document.
So, it should look like:
<?php session_start(); ?>
and then the rest of your work
NOTE : IF IT STILL NOT WORKING PLEASE PROVIDE THE ERROR.

Related

Trying to make a shopping cart in php, form isnt defining indexes correctly

Just that, here's some of he code i use
<?php
include('lib_carrito.php');
session_start();
?>
<form action="mete_producto.php" method="POST">
<input type="hidden" name="producto" value="<?php echo $nombre ?>">
<input type="hidden" name="precio" value="<?php echo $precio ?>">
<input type="hidden" name="producto" value="<?php echo $Id ?>">
<?php
if ($stock == 0 || !(isset($_SESSION['usuario'])) || $_SESSION['autoridad'] == 1) {
echo "<input type='submit' class='disabled' value='Añadir al carrito'>";
}
else{
echo "<input type='submit' class='añadir' value='Añadir al carrito'>";
}
?>
<select name="cantidad" style="height:35px; font-size: 25px">
<?php
for ($i=1; $i <= $stock; $i++) {
echo "<option value='".$i."'>".$i."</option>";
}
?>
</select>
</form>
That's on the page with the item the customer would buy,
then this is mete_producto.php
<?php
include("lib_carrito.php");
require('config.php');
session_start();
$_SESSION['ocarrito']->introduce_producto($_REQUEST["Id"], $_REQUEST["Nombre"], $_REQUEST["Precio"], $_REQUEST['Cantidad']);
//header("Location:index.php");
?>
I have the header as a comment so it would show me the errors,
for all of them is the same error 'Undefined index: Id'
You haven't defined either Id or Nombre as name attributes to be sent with your form. You have, however, assigned two producto names which have corresponding PHP variables. I assume this is how you are intending to use these variables.
Instead of:
<input type="hidden" name="producto" value="<?php echo $nombre ?>">
<input type="hidden" name="precio" value="<?php echo $precio ?>">
<input type="hidden" name="producto" value="<?php echo $Id ?>">
You're looking for:
<input type="hidden" name="nombre" value="<?php echo $nombre ?>">
<input type="hidden" name="precio" value="<?php echo $precio ?>">
<input type="hidden" name="Id" value="<?php echo $Id ?>">

Value passing as null from view to controller

The id is passing correctly but the post value is returning as empty , Please suggest with a solution what mistake i am doing here
Here is my view
<?php for($i=0;$i<count($array['value']);$i++) { ?>
<?php $id= $room['value'][$i]['Index']; ?>
<ul >
<li>
<?php echo form_open('cont/arraylist/'.$id); ?>
<input type="hidden" name="<?php echo 'foo'.$i ?>" value="<?php echo $room['value'][$i]['foo']?>" />
<input type="hidden" name="<?php echo 'boo'.$i?>" value="<?php echo $room['value'][$i]['boo']?>" />
<input type="hidden" name="<?php echo 'bar'.$i?>" value="<?php echo $room['value'][$i]['bar']?>" />
<input type="hidden" name="<?php echo 'baba'.$i?>" value="<?php echo $room['value'][$i]['baba']?>" />
<input type="submit" />
<?php echo form_close(); ?>
</li>
</ul>
<?php } ?>
Here is My controller
function arraylist($id)
{
echo $id;
echo $this->input->post('foo'.$id);
echo $this->input->post('boo'.$id);
echo $this->input->post('bar'.$id);
echo $this->input->post('baba'.$id);
}
You are trying get the input name 'foo'.$id but in view you are concatenating 'foo'.$i. This occurs to all. Do this:
<input type="hidden" name="<?php echo 'foo'.$id ?>" value="<?php echo $room['value'][$i]['foo']?>" />
<input type="hidden" name="<?php echo 'boo'.$id?>" value="<?php echo $room['value'][$i]['boo']?>" />
<input type="hidden" name="<?php echo 'bar'.$id?>" value="<?php echo $room['value'][$i]['bar']?>" />
<input type="hidden" name="<?php echo 'baba'.$id?>" value="<?php echo $room['value'][$i]['baba']?>" />

Paypal option issue on Opencart

We're using the built in Paypal Standard payment system on Opencart but it is not pulling the option price to Paypal.
Here is the code we have in our pp_standard.tpl file
<?php $i = 1; ?>
<?php foreach ($products as $product) { ?>
<input type="hidden" name="item_name_<?php echo $i; ?>" value="<?php echo $product['name']; ?>" />
<input type="hidden" name="item_number_<?php echo $i; ?>" value="<?php echo $product['model']; ?>" />
<input type="hidden" name="amount_<?php echo $i; ?>" value="<?php echo $product['price']; ?>" />
<input type="hidden" name="quantity_<?php echo $i; ?>" value="<?php echo $product['quantity']; ?>" />
<input type="hidden" name="weight_<?php echo $i; ?>" value="<?php echo $product['weight']; ?>" />
<?php $j = 0; ?>
<?php foreach ($product['option'] as $option) { ?>
<input type="hidden" name="on<?php echo $j; ?>_<?php echo $i; ?>" value="<?php echo $option['value']; ?>" />
<input type="hidden" name="os<?php echo $j; ?>_<?php echo $i; ?>" value="<?php echo $option['price']; ?>" />
<?php $j++; ?>
<?php } ?>
<?php $i++; ?>
<?php } ?>
I think the line that is the issue is this
<input type="hidden" name="os<?php echo $j; ?>_<?php echo $i; ?>"
value="<?php echo $option['price']; ?>" />
But I am not sure what to change it to for Paypal to pull the correct price
Here is the code that shows the price in the cart.tpl file
<?php foreach ($product['option'] as $option) { ?>
- <small><?php echo $option['name']; ?>: <?php echo $option['value']; ?>
- Price: <?php echo $option['price']; ?></small><br />
you can't use counter with paypal standard hidden inputs like amount, item_name and other hidden inputs inside foreach
<input type="hidden" name="amount_<?php echo $i; ?>" value="<?php echo $product['price']; ?>" />
in your case, paypal is looking request from input="amount" and you are posting request through input="amount_1"

How to restrict user to Upload only 3 images?

I'm creating a web app in Codeigniter. Here's the code in VIEW's.
<div class="bold light-gray">Add a photo or two!</div>
<p class="short">Or three, or more! Prospective Buyers love photos that highlight the features of your parts or gear.</p>
<form class="ajaxform" method="post" enctype="multipart/form-data" action='<?php echo site_url('UploadImage/upload_Image');?>'>
<input type="hidden" name="do" value="upload"/>
Upload your image <input type="file" name="Filedata" id="photo" onChange="abc()" />
<input type="hidden" name="timestamp" value="<?php echo $timestamp;?>" />
<input type="hidden" name="token" value="<?php echo md5('unique_salt' . $timestamp);?>" />
<input type="hidden" name="customer_id" value="<?php echo $customer['id'];?>" />
</form>
</div>
<!-- /.box.center -->
<?php echo form_open('/secure/add_item/'.$id, array('id'=>'listitem') ); ?>
<div id="img_cant"> <?php
if(isset($images) && !empty($images)) {
foreach($images as $key=>$img){
?>
<input type="hidden" value="<?php echo $img; ?>" name="images[]" id="hid_<?php echo $key; ?>">
<?php
}
}
?>
</div>
I want to allow the user to just add 3 or 4 images maximum. How to do that?
<?php $count=count($_FILES['name']);
if($count>3)
{
echo "Your error message";
}
else{
echo "your desired activity";
}?>
Using jquery you can call this function while submit your form
var file_nu = $("input:file")[0].files.length;
if(file_nu >3)//apply your condition here
{
return FALSE;
}else{
return TRUE;
}

Joomla 1.5 with Virtuemart 1.1.9

I want add changing on category page, I customize page websitelink.com/components\com_virtuemart\themes\default\templates\browse\browse_3.php
I add code
<div class="addtocart_buttonList">
<?php
$button_lbl = $VM_LANG->_('PHPSHOP_CART_ADD_TO');
$button_cls = 'addtocart_button';
if( CHECK_STOCK == '1' && ( $product_in_stock < 1 ) ) {
$button_lbl = $VM_LANG->_('VM_CART_NOTIFY');
$button_cls = 'notify_button';
$notify = true;
} else {
$notify = false;
}
?>
<form action="<?php echo $mm_action_url ?>index.php" method="post" name="addtocart" id="addtocart<?php echo $i ?>" class="addtocart_form" <?php if( $this->get_cfg( 'useAjaxCartActions', 1 ) && !$notify ) { echo 'onsubmit="handleAddToCart( this.id );return false;"'; } ?>>
<input type="submit" class="<?php echo $button_cls ?>" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" />
<input type="hidden" name="category_id" value="<?php echo #$_REQUEST['category_id'] ?>" />
<input type="hidden" name="product_id" value="<?php echo $product_id ?>" />
<input type="hidden" name="prod_id[]" value="<?php echo $product_id ?>" />
<input type="hidden" name="page" value="shop.cart" />
<input type="hidden" name="func" value="cartadd" />
<input type="hidden" name="Itemid" value="<?php echo $sess->getShopItemid() ?>" />
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="set_price[]" value="" />
<input type="hidden" name="adjust_price[]" value="" />
<input type="hidden" name="master_product[]" value="" />
</form>
</div>
After adding this line add to cart button appear on list items, but than i click on add to cart button error will come
Please enter a valid quatity for this item
How i can solve this?
You need to specify a quantity as an input field, hidden or otherwise. We only wanted the customer to be able to order one of an item, so added this code:
<input type="hidden" value="1" name="quantity">
Having that named input field satisifed the Virtuemart validation.

Categories