Add products on shopping cart

Friends, I am developing an e-commerce and need that after the customer fill out the amount of products and click “Buy”, only products with the field “quantity” to be entered in cart filled.


<ul class="products-list">
	<li class="product">
		<input type="hidden" class="product_id" value="1">
		<span class="product-name">Product 1</span>
		<span class="product-value">$ 100</span>
		<span>Qnt. <input type="text" class="quantity"></span>
        <li class="product">
		<input type="hidden" class="product_id" value="2">
		<span class="product-name">Product 2</span>
		<span class="product-value">$ 100</span>
		<span>Qnt. <input type="text" class="quantity"></span>


	url: 	'/cart/add/'+product_id+'/'+quantity,
	type: 	'GET',
	success: function( data ){
		if ( data != false ){



public function addItem( $product_id, $quantity){
	$product = $this->Produto_model->getById($product_id);
	$this->carrinho->setItem($product, $quantity);
	echo json_encode($this->carrinho);

With business-based applications, you can not rely on javascript to sanitise the information. This is because it’s far too easy for people to also use it to make changes that you do not want to occur.
There have been cases where people have used javascript to provide a negative charge on their credit card, for example. Don’t depend on javascript for your business. You’d be a fool to do so.

The first and most important thing for you to do is to have the server-side filter out the ones without a quantity. Once you have the server doing its correct and proper job, only then is when it is appropriate to have JavaScript help to improve the presentation of things. A nice way of doing that with JavaScript is to send the info to the server, and to then retrieve the sanitised list from the server.

That way your server-side code is doing its job of correctly protecting you, and the scripting is able to help you to display the updated information.

I know the risks, but I need to do so. Can you help me?

Yes, we can help you. Stay away from the cheap and nasty, and do things properly.

If you want us to help you with developing for your e-commerce situation, then please allow us to help you.

Thanks, I’ll do right with PHP without jquery, but anyway I need help.

How do I add the various products in the cart filling quantity and clicking “Buy”?

By making the Buy button a submit button instead, the form can then be submitted to server-side PHP code for processing.

<form action="addtocart.php" method="post">
    <p><input type="submit" value="Buy"></p>

You could also have a separate php script called validateproducts.php which the addtocart.php script uses. The validateproducts.php code is where sanity checks and filtering of items occurs.

That way, you could as a part of later web page development use ajax to request that the products be validated before they get submitted, or other forms of activities such as that.

But the amount of products is dynamic and I just want to be sent the products that have completed the quantity field. That is my biggest question.

You want to be sent the products? How are you intending to receive that list of products? By email?

I want to add products to the shopping cart.

That is the current PHP:

public function addItem( $product_id, $quantity){
    $product = $this->Product_model->getById($product_id);
    $this->cart->setItem($product, $quantity);
    echo json_encode($this->cart);

Hrm - that doesn’t look like JavaScript. I’ll shift this over to the PHP forum so that you can receive better help from there.