473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get HTML form data into PHP function Arguments?

aveeva
33
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
8 1633
gits
5,390 Expert Mod 4TB
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
33
@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
5,390 Expert Mod 4TB
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
33
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
33
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
5,390 Expert Mod 4TB
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
33
@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
5,390 Expert Mod 4TB
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

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

Similar topics

1
by: Joe Ray | last post by:
I am interested in passing encrypted HTML form data to a JSP page where it is than decrypted. Can someone suggest a technique to do this and the technology or encryption method I should use. If you...
1
by: Matt | last post by:
I have a html form, and I want to transform html form data to xml string. Given XSD (xml schema), how to transform the form data to xml string?
1
by: Clive Moore | last post by:
I have a database search web application in which the user is presented with a search page. The search page has a number of values by the body onload method. My problem lies when the user...
1
by: teknowbabble | last post by:
I would like to know how to convert HTML code that users input into a TEXTAREA box on a HTML FORM into a separate WEBPAGE. I have something like the diagram below on my webpage. The user is...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
0
by: bp_jobemail | last post by:
I'm trying to use PHP to wrap the output of an HTML form before it goes into a precompiled C cgi script. Essentially, the company that I work for uses a purchased precompiled c program for their...
0
by: burtell | last post by:
I have designed an HTML form. The name of each element corresponds to a heading in an Access database on my computer. Is there a way for me to program the HTML form to send the data from the form...
1
by: burtell | last post by:
Previously, I wrote: I have designed an HTML form. The name of each element corresponds to a heading in an Access database on my computer. Is there a way for me to program the HTML form to send the...
1
by: sumap | last post by:
hi i m trying to svave html form data into excel by save button but its not working and i can't findout the error where exactly where it is .
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.