2

This is my script

<script>
function callAjax(){
    var customoptionhtml = document.getElementById("customoption-dynamic").innerHTML; //alert(test);//alert(jQuery(test).text());
    var subtotalhtml = jQuery('.price-wrap').html();//alert(innerHtml);
     jQuery.ajax({
        type    : "POST",
        url     : "<?php echo Mage::getUrl('module/controller/action'); ?>",
        data    : jQuery('#product_addtocart_form').serialize(),
        data    : { customoption: customoptionhtml,formser:jQuery('#product_addtocart_form').serialize(),subtotal:subtotalhtml } ,
        dataType: "json",
        complete: function(response) {   
            //action        
        },
    });
}
</script>

This is my controller

public function testAction() {
        $product = $this->getRequest()->getParam('formser');
        $customoption = $this->getRequest()->getParam('customoption');


        $pdctname = $product['product'];
        Mage::log($product, null, '1.log');
        Mage::log($customoption, null, '2.log');

}

I got $product in log file as :

form_key=ZWdfUerRFgl9oR4r&product=1&related_product=&options%5B9%5D=tryty&options%5B10%5D=85&options%5B11%5D=132&options%5B12%5D=156&options%5B13%5D=181&options%5B16%5D=&options%5B17%5D=&options%5B19%5D=&options%5B18%5D=&options%5B14%5D=183&options%5B15%5D=&name=ry&companyname=ty&email=test%40test.com&phnmbr=7894561233

I want to get "product=1"(marked in bold) from the above data(that is the product id).

I tried as $pdctdet = $product['product']; .But not working.

Please give a solution.

4
  • You can try with json_decode($product) and let me know which output is display. – Rakesh Jesadiya Jul 5 '17 at 5:04
  • I have used $decodedata =json_decode($product);Mage::log("test".$decodedata, null, 'today1.log'); Gives output as "test".Decode array is not getting – Vindhuja Jul 5 '17 at 5:28
  • you can try with unserialize method of php and let me know – Rakesh Jesadiya Jul 5 '17 at 5:35
  • $unserializedData = array(); Mage::log(parse_str($unserializedData,$product), null, 'today5.log'); But not working.Please give a solution. – Vindhuja Jul 5 '17 at 5:55
1

Try this:

Use this in your controller:

$data = $this->getRequest()->getPost();
parse_str($data['formser']);
$productid = $product;

"product=1" will be in $productid variable.

0

As per your log $product contains as a string. if you need $productId then you need to convert this string like below.

$product = "form_key=ZWdfUerRFgl9oR4r&product=1&related_product=&options%5B9%5D=tryty&options%5B10%5D=85&options%5B11%5D=132&options%5B12%5D=156&options%5B13%5D=181&options%5B16%5D=&options%5B17%5D=&options%5B19%5D=&options%5B18%5D=&options%5B14%5D=183&options%5B15%5D=&name=ry&companyname=ty&email=test%40test.com&phnmbr=7894561233";

$products = explode("&",$product);
foreach($products as $string){
    list($k, $v) = explode('=', $string);
    $result[ $k ] = $v; 
}
echo $productId = $result['product']; // 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.