Jump to content

How to get HTML form data into PHP function Arguments using AJAX?


aveeva

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.