aveeva Posted April 26, 2019 Share Posted April 26, 2019 (edited) The Business Logic is: If product is already purchased, upgrade the product I have a customer input form to get information, along with some fields which should be auto populated based on what the customer chooses. Screen Shot: https://i.stack.imgur.com/Fkyim.png Note: Price and Shipping Weight are done with Ajax, When we come to the shipping cost I am getting it from Magento, using a PHP function. form : https://paste.ofcode.org/335FVUhpBGbazQtrcPLVQUs PHP : sp_cost.php https://paste.ofcode.org/hvG2sP9TW9CEPgMMuKXNuw From the above code I am given the predefined value, $results = getShippingEstimate('14419','1',"IN","642001"); How can i get country and zipcode from the user entry and return the shipping cost? Edited April 26, 2019 by aveeva Quote Link to comment https://forums.phpfreaks.com/topic/308644-how-to-get-html-form-data-into-php-function-arguments-using-ajax/ Share on other sites More sharing options...
requinix Posted April 26, 2019 Share Posted April 26, 2019 First step is to create (or reuse) a PHP script that can output the value, in the browser, if you put the country and zipcode in the query string. In other words, make a working URL like this: /path/to/script.php?country=Some+Country&zipcode=12345 When you have that done it can be adjusted to work through AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/308644-how-to-get-html-form-data-into-php-function-arguments-using-ajax/#findComment-1566321 Share on other sites More sharing options...
aveeva Posted April 26, 2019 Author Share Posted April 26, 2019 (edited) <?php ob_start(); // require_once(__DIR__ . '/app/Mage.php'); require_once('./../app/Mage.php'); umask(0); ini_set('display_errors',true); ini_set('memory_limit', '1024M'); Mage::app()->loadArea('frontend'); function getShippingEstimate($productId,$productQty,$countryId,$postcode ) { // $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId()); $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId()); $_product = Mage::getModel('catalog/product')->load($productId); $_product->getStockItem()->setUseConfigManageStock(false); $_product->getStockItem()->setManageStock(false); $quote->addProduct($_product, $productQty); $quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode); $quote->getShippingAddress()->collectTotals(); $quote->getShippingAddress()->setCollectShippingRates(true); $quote->getShippingAddress()->collectShippingRates(); $_rates = $quote->getShippingAddress()->getShippingRatesCollection(); $shippingRates = array(); foreach ($_rates as $_rate): if($_rate->getPrice() > 0) { $shippingRates[] = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice()); } endforeach; return $shippingRates; } // echo "<pre>"; // product id, quantity, country, postcode // print_r(getShippingEstimate('14419','1',"IN","642001")); // echo "</pre>"; $results = getShippingEstimate('14419','1',"IN","642001"); // $results = getShippingEstimate('14419','1',"US","99501"); $count = -1; echo "<select>"; foreach ($results as $result): $count++; ?> <option value="<?php echo $count; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"]?> </option> <?php endforeach; echo "</select>"; ?> Above is my PHP function to return shipping cost, by using predefined value, $results = getShippingEstimate('14419','1',"IN","642001"); How can i pass my country (IN) & zip_code (642001) from the customer input form, and return the value to, <tr> <th>Shipping Cost (Rs) : </th> <td id="findata"></td> <input type="hidden" name="shipping_cost" id="shipping_cost"/> </tr> <tr> Ajax : <script> $(document).ready(function(){ $('#new').on('change',function(){ var old_val = $("#old option:selected").val(); var new_val = $("#new option:selected").val(); $.ajax({ type: "POST", // url: "ajax_ship_cost_data.php", url: "sp_cost.php", dataType: "text", data: { old: old_val, new: new_val}, success: function(data) { // Check the output of ajax call on firebug console //console.log(data); $('#findata').html(data); $('#shipping_cost').val(data); } }); }); }); </script> I am in learning stage in PHP & Ajax, i successfully return a couple of value using ajax (pls run my code) i am stuck with run time passing value to PHP function. How to do correct my code? Edited April 26, 2019 by aveeva added more information Quote Link to comment https://forums.phpfreaks.com/topic/308644-how-to-get-html-form-data-into-php-function-arguments-using-ajax/#findComment-1566324 Share on other sites More sharing options...
requinix Posted April 26, 2019 Share Posted April 26, 2019 Not sure how you can have written that without understanding what you're doing... Dump out the contents of $_POST to see if anything looks useful. Quote Link to comment https://forums.phpfreaks.com/topic/308644-how-to-get-html-form-data-into-php-function-arguments-using-ajax/#findComment-1566325 Share on other sites More sharing options...
aveeva Posted April 26, 2019 Author Share Posted April 26, 2019 As i already said i am in learning stage, i did what i want in my code, additionally i want to add the feature like getting data from html form and pass to PHP function arguments ( $results = getShippingEstimate('14419','1',"IN","642001");) using ajax, here ajax i copy from working ajax (as you see from last to second Ajax actually its working, logic like if drop down field select and return th value by ajax , this is simple right ) <!-- <button id="btn_check_value">Check for value</button> --> <!-- Ajax Script --> <script> $(document).ready(function(){ $('#new').on('change',function(){ var old_val = $("#old option:selected").val(); var new_val = $("#new option:selected").val(); $.ajax({ type: "POST", url: "ajax_data.php", dataType: "text", data: { old: old_val, new: new_val}, success: function(data) { // Check the output of ajax call on firebug console // console.log(data); $('#result').html(data); $('#price').val(data); } }); }); }); </script> <script> $(document).ready(function(){ $('#new').on('change',function(){ var old_val = $("#old option:selected").val(); var new_val = $("#new option:selected").val(); $.ajax({ type: "POST", url: "ajax_ship_weight.php", dataType: "text", data: { old: old_val, new: new_val}, success: function(data) { // Check the output of ajax call on firebug console //console.log(data); $('#show').html(data); $('#shipping_weight').val(data); } }); }); }); </script> and tried to modify as per my new feature like getting value from html form and passing to php function arguments. I am sure my code for the second feature ( getting value from html form and passing to php function arguments ) Can i get help to solve my issue, I saw your last comment $_POST I did already but no luck, workout : if(isset($_POST['zip_postal_code']) && isset($_POST['country'])) { $zip_postal_code = $_POST['zip_postal_code']; $country = $_POST['country']; } //$results = getShippingEstimate('14419','1',"IN","642001"); $results = getShippingEstimate('14419','1',"$country","$zip_postal_code"); Any help, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/308644-how-to-get-html-form-data-into-php-function-arguments-using-ajax/#findComment-1566326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.