Jump to content

problem with Tools::getValue() Get value of select menu


vitochueng

Recommended Posts

Hello,

 

I tryed with Tools::getValue() to get value from select menu in product page, but it doesn't work...

 

-----------------------         codes in the add to cart form at product.tpl :

 

                                    <select name="char_color" id="attr_char_color">

                                                <option value="None"  selected="selected">Select  Color</option>
                                                <option value="Black">Black</option>
                                                <option value="Ivory">Ivory</option>
                                                <option value="White">White</option>
                                                <option value="Brown">Brown</option>
                                                <option value="Burgundy">Burgundy</option>
                                    </select>
 
-----------------------         and codes in CartController.php:
 
protected $char_color;
 
public function init()
{
parent::init();
 
// Send noindex to avoid ghost carts by bots
header("X-Robots-Tag: noindex, nofollow", true);
 
// Get page main parameters
$this->id_product = (int)Tools::getValue('id_product', null);
$this->id_product_attribute = (int)Tools::getValue('id_product_attribute', Tools::getValue('ipa'));
$this->customization_id = (int)Tools::getValue('id_customization');
$this->qty = abs(Tools::getValue('qty', 1));
$this->id_address_delivery = (int)Tools::getValue('id_address_delivery');
$this->char_color = (string) Tools::getValue('char_color');
 
}

 

 

But the value $this->char_color is empty, does anyone here can help me?

Link to comment
Share on other sites

Tools::getValue() only get the values after a POST or GET, to be clear, in this case you will get these value, only when the form is submitted, and I think you are in the wrong function.

Link to comment
Share on other sites

Tools::getValue() only get the values after a POST or GET, to be clear, in this case you will get these value, only when the form is submitted, and I think you are in the wrong function.

 

I have submitted the form. But the Tools::getValue()  method cached nothing...

Link to comment
Share on other sites

I think you are trying to catch it in the wrong function, you need provide more code to help you.

 

I changed the code :

 

               $this->char_color = (string) Tools::getValue('char_color');

 

to :

 

                        if (Tools::getValue('char_color'))
                                    $this->char_color = (string) Tools::getValue('char_color');
                        else
                                    $this->char_color = 'none';
 
Always get the value 'none'. As you said, maybe I caught it in wrong function.  
I just want to catch the value of select memu when click the 'add to cart' button. Do you know which function should I use? In controllers\front\CartController.php or classes\Cart.php ? Thanks
Link to comment
Share on other sites

are you sure that your form is posted?

try to use print_r($_POST); and see if there is an array of posted variables.

 

Thanks for your advice. I put the print_r($_POST); at  CartController.php: init() function.  And I got the information below:

 

Array ( [controller] => cart [add] => 1 [ajax] => true [qty] => 1 [id_product] => 1 [token] => 27b35e59bff8619e4e6fe90f423d97ac )

 

The select option was never posted.  Cloud you tell me how to solve this issue?

 

Thanks

Link to comment
Share on other sites

so as you can see your form isn't posted ;)

where is the post button / command? 

how you use this form to post it?

 

I only put the codes below in the add to cart form at product.tpl, nothing else was changed in product.tpl...  Am I right? 

 

    <select name="char_color" id="attr_char_color">

                                                <option value="None"  selected="selected">Select  Color</option>
                                                <option value="Black">Black</option>
                                                <option value="Ivory">Ivory</option>
                                                <option value="White">White</option>
                                                <option value="Brown">Brown</option>
                                                <option value="Burgundy">Burgundy</option>
    </select>
Link to comment
Share on other sites

so as you can see your form isn't posted ;)

where is the post button / command? 

how you use this form to post it?

 

Below is the html source, the bold word was added by myself.  Is there any error ?  

 

<form id="buy_block" action="http://localhost/ps16/index.php?controller=cart" method="post">
 
                                    <!-- hidden datas -->
                                    <p class="hidden">
                                                <input type="hidden" name="token" value="27b35e59bff8619e4e6fe90f423d97ac">
                                                <input type="hidden" name="id_product" value="1" id="product_page_product_id">
                                                <input type="hidden" name="add" value="1">
                                                <input type="hidden" name="id_product_attribute" id="idCombination" value="">
                                    </p>
                                    <select name="char_color" id="char_color">
                                                <option value="None" selected="selected">----------</option>
                                                <option value="Black">Black</option>
                                                <option value="Ivory">Ivory</option>
                                                <option value="White">White</option>
                                                <option value="Blushing Pink">Blushing Pink</option>
                                                <option value="Brown">Brown</option>
                                                <option value="Burgundy">Burgundy</option>
                                    </select>
                                    <div class="box-info-product">
                                                <div class="content_prices clearfix">
                                                                                                                                    <!-- prices -->
                                                                        <div class="price">
                                                                                    <p class="our_price_display" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
                                                                                                <link itemprop="availability" href="http://schema.org/InStock">
                                                                                                                                                                                                            <span id="our_price_display" itemprop="price">$16.51</span>
                                                                                                            <!---->
                                                                        <meta itemprop="priceCurrency" content="USD">
                                                                                                            </p>
                                                <p id="reduction_percent" style="display:none;">
                                                            <span id="reduction_percent_display">
                                                                                                                                    </span>
                                                </p>
                                                <p id="old_price" class="hidden">
                                                                                                                                    <span id="old_price_display"></span>
                                                                        <!--  -->
                                                                                                            </p>
                                                                                    </div> <!-- end prices -->
                                    <p id="reduction_amount" style="display:none">
                                                <span id="reduction_amount_display">
                                                                                                            </span>
                                    </p>
                                                                                                                                                 
                                    <div class="clear"></div>
                        </div> <!-- end content_prices -->
                        <div class="product_attributes clearfix">
                                    <!-- quantity wanted -->
                                                                                    <p id="quantity_wanted_p">
                                                            <label>Quantity:</label>
                                                            <input type="text" name="qty" id="quantity_wanted" class="text" value="1" style="border: 1px solid rgb(189, 194, 201);">
                                                            <a href="#" data-field-qty="qty" class="btn btn-default button-minus product_quantity_down">
                                                                        <span><i class="icon-minus"></i></span>
                                                            </a>
                                                            <a href="#" data-field-qty="qty" class="btn btn-default button-plus product_quantity_up ">
                                                                        <span><i class="icon-plus"></i></span>
                                                            </a>
                                                            <span class="clearfix"></span>
                                                </p>
                                                                        <!-- minimal quantity wanted -->
                                    <p id="minimal_quantity_wanted_p" style="display: none; color: rgb(55, 72, 83);">
                                                This product is not sold individually. You must select at least <b id="minimal_quantity_label">1</b> quantity for this product.
                                    </p>
                                                            </div> <!-- end product_attributes -->
                        <div class="box-cart-bottom">
                                    <div>
                                                <p id="add_to_cart" class="buttons_bottom_block no-print">
                                                            <button type="submit" name="Submit" class="exclusive">
                                                                        <span>Add to cart</span>
                                                            </button>
                                                </p>
                                    </div>
                        <!-- Productpaymentlogos module -->
<div id="product_payment_logos">
<div class="box-security">
    <h5 class="product-heading-h5"></h5> 
  <a href="" title=""><img src="/ps16/modules/productpaymentlogos/img/payment-logo.png" alt=""></a>
    </div>
</div>
  
<!-- /Productpaymentlogos module -->
 
<p class="buttons_bottom_block no-print">
<a id="wishlist_button" href="#" onclick="WishlistCart('wishlist_block_list', 'add', '1', $('#idCombination').val(), document.getElementById('quantity_wanted').value); return false;" rel="nofollow" title="Add to my wishlist">
Add to my wishlist
</a>
</p>
<strong></strong>
            </div> <!-- end box-cart-bottom -->
</div> <!-- end box-info-product -->
</form>
Edited by vitochueng (see edit history)
Link to comment
Share on other sites

  • 3 months later...

i got it upto this. i cannot insert posted custom value in database. (i've created custom field in ps_cart_product)

 

i tried custom field to db code in Cart.php at 966

$result_add = Db::getInstance()->insert('cart_product', array(
					'id_product' => 			(int)$id_product,
					'id_product_attribute' => 	(int)$id_product_attribute,
					'id_cart' => 				(int)$this->id,
					'id_address_delivery' => 	(int)$id_address_delivery,
					'id_shop' => 				$shop->id,
					'quantity' => 				(int)$quantity,
					'date_add' => 				date('Y-m-d H:i:s'),
					'pack_content' => 			$customPack_value
				));

// where 'pack_content' and $customPackValue are my codes. 

 if i press add to cart button, no value added to db field

 

please help!

Edited by mani313 (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...