I am displaying list of items(multiple entries) from database on one page. I am trying to post all those values fetched from the database to another php file to add item to cart.
But when I press 'add to cart' button for any item in the list, it is only sending last item through POST. I know it is because of while loop, but I don't know correct way to implement it?
<form action="php/addToCart.php" method= "POST">
<?php
while($product=mysqli_fetch_assoc($featured)):
?>
<div class="col-md-5">
<h4><?= $product['title'] ?></h4>
<image src="php/marketplace_images/<?= $product['image'];?>" alt="<?= $product['title'];?>" height="300" width="300"/>
<p class="lprice"> Price $ <?= $product['price'];?></p>
<p class="desc"> Description: <?= $product['description'];?></p>
<p class="bname"> Brand Name: <?= $product['brandname'];?></p>
<input name="title" type="hidden" value="<?= $product['title'] ?>" />
<input name="price" type="hidden" value="<?= $product['price'] ?>" />
<input name="brandname" type="hidden" value="<?= $product['brandname'] ?>" />
<input name="image" type="hidden" value="<?= $product['image'] ?>" />
<input name="featured" type="hidden" value="<?= $product['featured'] ?>" />
<button type="submit" class="btn btn-success" data-toggle="modal" name="add">Add To Cart</button>
</div>
<?php endwhile;
?>
You're ending up with a single form with n (as many as items) fields with the same name, that are no longer separate. E.g. you'll have n fields named "title", n fields named "price" etc.
You'd never know which title belongs to which price.
Plus, the code you show only shows the rendering of the form, not what you're doing with it on submission (but that wouldn't make it better, as you can't distinguish between all the different inputs). If that code expects one title, it might take the last, or a random one.
So, if you want to send just a single item: Move your <form> and </form> into the while loop, so that you end up with a number of forms that can be individually submitted. In general I'd recommend against posting all of the data and just limit yourself to an id of an item, but that's beyond the scope of this question (but imagine what happens when I manipulate the price to 0 before submitting the item to the cart)
Related
I am working on a project where we have a few items in the database that are displayed on a website and that works fine code is below:
<form method="post" action="store.php?action=add&ItemNo=<?php echo $products['ItemNo']; ?>">
<a href="product.php"><div class="products">
<img src="<?php echo $products['img'];?>" class="img-responsive"/>
<!-- this connects to the database and gets the image of item -->
<h4><?php echo $products['ItemName'];?></h4>
<h3>$ <?php echo $products['Price'];?></h3>
<input type="text" name="Quantity" class="form-control" value="1"/>
<!-- This creates an box where the default qnt is set to 1 -->
<input type="hidden" name="ItemName" value="<?php echo $products['ItemName']; ?>" />
<input type="hidden" name="Price" value="<?php echo $products['Price']; ?>" />
<input type="submit" name="addtocart" style="margin-top:5px;" class="btn btn-info"
value="Add to Cart"/>
<!-- this creates the btn that adds item to cart -->
</div>
</a>
</form>
Now I am trying to show more details about each product. All I want is when the user clicks any item they will be sent to item.php page where there will be more details about that specific item.
At this moment I got all info displaying in item.php and I am stuck.
I do not know how to word this question, maybe that is why I cannot find any help out on Google. I am new to PHP as I just started with it few weeks ago.
firstly
<a href="product.php?id=<?php echo $products['ItemNo']; ?>">
then on product.php check the url for $_GET['id'] and use that in your query.
SELECT * from foo where id=$id
but dont use the raw id or a non parametrised query unless you like being hacked
So if someone is logged in my script reads the values and stores it and sets it as variables then I'm trying to make a registration page for something that members can register in.
The script is simple
<div id="english1"><p id="english1p">تأكد من بياناتك ثم اضغط رز الموافقة</p><br>
<form class="register2" action="includes/register2.inc.php" method="post" name="register2form"
<p class="username">اسم المستخدم: <?php echo htmlentities($_SESSION['username']); ?></p>
<p class="email">الايميل: <?php echo htmlentities($_SESSION['email']); ?></p>
<p class="age">السن: <?php echo htmlentities($_SESSION['age']); ?></p>
<p class="coursetype">الكورس المراد التسجيل فيه: <?php echo $coursename; ?></p>
<p class="paymethod">طرق الدفع:</p><ol><li>طريقة</li>
<li>طريقة</li>
<li>طريقة</li>
</ol>
<p class="sure">للتأكيد اضغط تسجيل</p>
<input class="register2button"
type="button"
value="تسجيل"
onclick="" />
</div>
Ignore the letters you don't understand, it's arabic.
So the script reads variables and then I want when the member clicks the submit button, the page POST the variables to includes/register2.inc.php without input as the variables are already set.
How can I do that?
If you want to keep your <p> tags you can store all the variables you want to pass to the next page in <input type="hidden" value="some value" /> These boxes will not be shown on the page, but will pass along data with any request you send to your server.
You can store your value in hidden element and pass it to next page.
<form action="page name" method="post">
<input type="hidden" name="username" value="<?php echo htmlentities($_SESSION['username']); ?>" />
<p class="username"> username : <?php echo htmlentities($_SESSION['username']); ?></p>
<input type="submit" name="submit" value="submit" >
</form>
Inside the form, instead of p you should use input for each parameter:
<input type="text" name="username" value="<?php echo htmlentities($_SESSION['username']); ?>"/>
You can kept this input hidden also like:
<input type="hidden" name="username" value="yourvalue"/>
These input field value won't pass in next page in $_POST if you will use p you have to use input for this.
The buy now button on single product page as is presently, is redirecting straight to checkout after adding 1 product to the cart.
What is to be done is that the user selected quantity should ideally be fed to the buy now button dynamically and in real time (through ajax) so that the customer can select the quantity and then decide whether he wants to add the quantity to the cart and continue shopping or he wants to click buy now and add the selected quantity to the cart and being redirected straight to the checkout.
Please refer to the way system works now for better idea how this should work as currently the system works flawlessly apart from allowing to specify desired quantity.
here is the link of my website
http://www.merc-clinic.com/product/lockout-safety-valve-and-electrical-lockout-kit-large/
i am adding this code in themes/ls_new/woocommerce/single-product/add-to-cart/simple.php
<form id="qty" class="cart single-product" method="post" enctype='multipart/form-data' action="/checkout?set-cart-qty_<?php echo $product->id;?>=2">
<button id="buynow" type="submit" data-toggle="tooltip" data-placement="bottom" title="<?php echo get_option('tooltip_text');?>" class="single_product_buy_now single_add_to_cart_button button alt cart-buttton buy-now">Buy Now</button>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
I do see you have some format errors and the form ending is missing. I don't use tooltip, but if this works then try adding your title.
Try this:
<form id="qty" class="cart single-product" enctype="multipart/form-data" method="post">
<div class="quantity"><input class="input-text qty text" title="Qty" max="94" min="1"
name="quantity" pattern="[0-9]*" size="4" step="1" type="number" value="1" />
</div>
<input name="add-to-cart" type="hidden" value="2" />
<button id="buynow" class="single_add_to_cart_button button alt"
type="submit"> Buy Now</button>
</form>
I have a user form that creates a drop down menu in Wordpress php. The form clears after it's submitted. How can I re-populate the form, when the edit pencil icon is clicked so the original info can be edited? As it is, because the form is cleared, it starts another menu.
There is one category and multiple names.
Thank you...if you need info, I will post it. I'm trying...
-A
I'm having a problem with this...as I mentioned, there is an "edit button" that creates a stupid edit form that some guy designed. I would like to repopulate the original form users filled out. I'm almost there. The info is is filled in boxes but they randomly overlay the drop down menu. It's not hitting inside the original form. "pp-website-edit" is the name of the "edit button".
<div class="pp-website-edit">
<form id="cat-edit-<?php echo $x; ?>" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="edit-category" value="<?php echo $indicat; ?>">
<INPUT TYPE="image" SRC="<?php bloginfo('url'); ?>/wp-content/themes/nebula/neb-style/images/arrow_045_small.png" HEIGHT="14" WIDTH="14" BORDER="0" ALT="Submit Form">
</form>
</div>
<div class="pp-website-edit">
<form id="wat-edit" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="edit-website" value="1">
<input type="hidden" name="ed-w-cat" value="<?php echo $indicat; ?>">
<input type="hidden" name="ed-w-label" value="<?php echo $finalresult[$y][name]; ?>">
<input type="hidden" name="ed-w-url" value="<?php echo $finalresult[$y][url]; ?>">
<INPUT TYPE="image" SRC="<?php bloginfo('url'); ?>/wp-content/themes/nebula/neb-style/images/arrow_045_small.png" HEIGHT="14" WIDTH="14" BORDER="0" ALT="Submit Form">
</form>
============================================================================
Site Category
Site Titles
Website url's
Forms that keep their values utilize some sort of peristance, either cookies, or a combination of querying the database for the values.
Either way you would need to then echo our the value inside the <input> tag.
The thing is that I would like to implement the following functionality to my site:
In the account settings - order details I would like to add a button that would add all the products from that very order to cart.
Now, I pretty much get it how the add to cart functionality is implemented on the flypage and browse page. I can't really get how should I pass multiple items to the add to cart function on order details page and how it'll be processed by ps_cart if an item is out of stock or is unpublished.
Here's the code of the add to cart form from browse page:
<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;"'; } ?>>
<?php echo $ps_product_attribute->show_quantity_box($product_id,$product_id); ?><br />
<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>
I wonder if I'd need to pass all of these to ps_cart?
I don't expect from any of you a completely ready extension, it would be great if you could point me in the right direction.
Thanks!
EDIT:
I was able to set up the add to cart button, now I have to pass the needed data.
It would be great if it would work with at least one item, then I'd start thinking how to alter the code.
EDIT 2:
I was able to make it work flawlessly for one product. But how can I pass multiple Product Id's and their quantities to the add to cart function?
The current code in ps_cart already has a loop to iterate through multiple product IDs that are sent to the cart. From the look of the code there, I would simple put all of the product IDs you want to add to the cart in to prod_id array. You will notice that prod_id, set_price, adjust_price, and master_product are all arrays in the form. My guess is that you will need to put in those values for each product you want to add and ps_cart will do the rest.