By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

How to get HTML form data into PHP function Arguments?

aveeva
P: 28
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?
Apr 25 '19 #1
Share this Question
Share on Google+
8 Replies


gits
Expert Mod 5K+
P: 5,295
in the code here:

https://paste.ofcode.org/335FVUhpBGbazQtrcPLVQUs

you can easyly see that at the bottom are 3 script-elements that basically do assign an event-handler (onchange) to the element with the id="new" - and all 3 handlers invoke a request each with passing the exact same data to different endpoints:

Expand|Select|Wrap|Line Numbers
  1.     var old_val = $("#old option:selected").val();
  2.     var new_val = $("#new option:selected").val();
  3.  
  4.     // ...
  5.  
  6.     data: { old: old_val, new: new_val},
  7.  
this means that you simply need to retrieve the data from the fields correctly in your javascript sections and pass it in the data-object of the ajax call to the endpoint(s) that should receive it. For optimization of all that it would even be preferable to just send 1 request and not fire 3 in parallel.
Apr 25 '19 #2

aveeva
P: 28
@gits : Okay, with my code how can i use input form values in PHP function arguments,

as said,

sp_cost.php

https://paste.ofcode.org/hvG2sP9TW9CEPgMMuKXNuw
From the above code I am given the predefined value,

Expand|Select|Wrap|Line Numbers
  1. $results = getShippingEstimate('14419','1',"IN","642001");
  2.  
How can i pass my zip code & country from form and pass by using ajax.

i know the following is wrong, how can i write the right code,

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.         $(document).ready(function(){
  3.         $('#new').on('change',function(){
  4.  
  5.         var old_val = $("#old option:selected").val();
  6.         var new_val = $("#new option:selected").val();
  7.  
  8.         $.ajax({
  9.         type: "POST",
  10.         // url: "ajax_ship_cost_data.php",
  11.         url: "sp_cost.php",
  12.         dataType: "text",
  13.         data: { old: old_val, new: new_val},
  14.         success: function(data)
  15.         {
  16.             // Check the output of ajax call on firebug console
  17.              //console.log(data);
  18.             $('#findata').html(data);
  19.             $('#shipping_cost').val(data);
  20.  
  21.         }
  22. });
  23.  
  24. });
  25. });
  26. </script>
Apr 26 '19 #3

gits
Expert Mod 5K+
P: 5,295
as i said - you need to retrieve the correct values from the form elements and pass it to the data object in the jQuery ajax call. I suggest to read the jQuery API docs if you don't know how to do it - i probably won't write the code for you - basically its easy changes and considered as the minimum knowledge when working with jQuery. you can start from here:

http://api.jquery.com/val/

for example and then go on from there.
Apr 26 '19 #4

aveeva
P: 28
Using this -> http://api.jquery.com/val/ i did my couple of fields as you see [Price & weight]. Here shipping cost field is not predefined value, actually it gets from Magento [PHP CMS]. I need some help regarding how to pass my value which is getting from Magento and insert into shipping cost field, for me, it's quite tough,

workout :

My PHP function :

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start();
  3. // require_once(__DIR__ . '/app/Mage.php');
  4. require_once('./../app/Mage.php');
  5. umask(0);
  6. ini_set('display_errors',true); 
  7. ini_set('memory_limit', '1024M');
  8. Mage::app()->loadArea('frontend');
  9.  
  10. function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {
  11.  
  12.     // $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
  13.     $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
  14.     $_product = Mage::getModel('catalog/product')->load($productId);
  15.  
  16.     $_product->getStockItem()->setUseConfigManageStock(false);
  17.     $_product->getStockItem()->setManageStock(false);
  18.  
  19.     $quote->addProduct($_product, $productQty);
  20.     $quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode); 
  21.     $quote->getShippingAddress()->collectTotals();
  22.     $quote->getShippingAddress()->setCollectShippingRates(true);
  23.     $quote->getShippingAddress()->collectShippingRates();
  24.  
  25.     $_rates = $quote->getShippingAddress()->getShippingRatesCollection();
  26.  
  27.     $shippingRates = array();
  28.     foreach ($_rates as $_rate):
  29.             if($_rate->getPrice() > 0) {
  30.                 $shippingRates[] =  array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
  31.             }
  32.     endforeach;
  33.  
  34.     return $shippingRates;
  35.  
  36. }
  37.  
  38. // echo "<pre>";
  39. // product id, quantity, country, postcode
  40. // print_r(getShippingEstimate('14419','1',"IN","642001"));
  41.  
  42. // echo "</pre>";
  43.  
  44. //$results = getShippingEstimate('14419','1',"IN","642001");
  45.  
  46. // $results = getShippingEstimate('14419','1',"US","99501");
  47.  
  48.  
  49.  
  50. <!-- if(isset($_POST['zip_postal_code']) && isset($_POST['country']))
  51.     {
  52.      $zip_postal_code = $_POST['zip_postal_code'];
  53.      $country = $_POST['country'];
  54.  } -->
  55.  
  56.  
  57. $results = getShippingEstimate('14419','1',"IN","642001");
  58.  
  59. //$results = getShippingEstimate('14419','1',"$country","$zip_postal_code");
  60.  
  61.  
  62. $count = -1;
  63. echo "<select>";
  64. foreach ($results as $result):
  65. $count++;
  66. ?>
  67.  <option value="<?php echo $count; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"]?>
  68.  </option>
  69.  
  70.  <?php
  71. endforeach;
  72. echo "</select>"; 
  73. ?> 

FYI -> In PHP function :

shipping cost calculated using :

$results = getShippingEstimate('14419','1',"IN","642001");

and passing the shipping cost value using,

<option value="<?php echo $count; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"]?>
</option>

Note: By using $results = getShippingEstimate('14419','1',"IN","642001"); its returned the value, i need if customer enter the zip_code and country, once the customer entered those values using ajax passing value to -> $results = getShippingEstimate('14419','1',"IN","642001"); and returned to the shipping cost.

I hope I explained well. Can I get some help?
Apr 27 '19 #5

aveeva
P: 28
Solved :

Actually error in Ajax & error in passing fields,

Expand|Select|Wrap|Line Numbers
  1.     <script>
  2.         $(document).ready(function(){
  3.         $('#new').on('change',function(){
  4.  
  5.         var zip = $("#zip_postal_code").val();
  6.         var country = $("#country").val();
  7.  
  8.         $.ajax({
  9.         type: "POST",
  10.         // url: "ajax_ship_cost_data.php",
  11.         url: "sp_cost.php",
  12.         dataType: "text",
  13.         data: {zip_postal_code : zip, country: country},
  14.         success: function(data)
  15.         {
  16.             // Check the output of ajax call on firebug console
  17.              //console.log(data);
  18.             $('#findata').html(data);
  19.             $('#shipping_cost').val(data);
  20.  
  21.         }
  22. });
  23.  
  24. });
  25. });
  26. </script>
Apr 27 '19 #6

gits
Expert Mod 5K+
P: 5,295
grats :) the only thing to note is that the requests are only send when the value of the select-element with id="new" is changed. so if a user only changes some other field like zip-code or something else - the request is not fired. If that's how it should work then all is good - else you would need to attach the the correct eventhandlers to the fields that are relevant for new calculations.
Apr 27 '19 #7

aveeva
P: 28
@gits I am facing error while select shipping cost, if i select anyone from dropdown all the values saves into db, how to modify my code.
Output : https://snag.gy/BIJZb7.jpg

Any help thanks.
Apr 30 '19 #8

gits
Expert Mod 5K+
P: 5,295
this screenshot doesn't tell much. your program in fact acts as you programmed it. in case the code didn't change much - you fire at least 3 requests in parallel always when you change the value of the select-element with id="new". So you need to check all entrypoints that you call and even if its necessary/correct to call them all at the same time - and see what values they get received and what they then do - i cant help much with that without seeing more of the actual code.
Apr 30 '19 #9

Post your reply

Sign in to post your reply or Sign up for a free account.