SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    HTML:

    HTML Code:
    <form>
    <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>
            <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> 
    	</li>
    </ul>
    <button>Buy</button>
    </form>
    jQuery:

    Code:
    $.ajax({
    		
    	url: 	'/cart/add/'+product_id+'/'+quantity,
    	type: 	'GET',
    	success: function( data ){
    		if ( data != false ){
    						
    			window.location.reload();
    						
    		}
    	}		
    
    });
    My PHP:

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


  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,710
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Felipe Turquize View Post
    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.
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know the risks, but I need to do so. Can you help me?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,710
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Felipe Turquize View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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"?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,710
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Felipe Turquize View Post
    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.

    HTML Code:
    <form action="addtocart.php" method="post">
        ...
        <p><input type="submit" value="Buy"></p>
    </form>
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,710
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Felipe Turquize View Post
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to add products to the shopping cart.

    That is the current PHP:

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


  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,710
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Felipe Turquize View Post
    I want to add products to the shopping cart.

    That is the current PHP:

    PHP Code:
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •