eldan88 Posted June 26, 2014 Share Posted June 26, 2014 Hey Guys. I am working with a form that shows the grand total on the checkout page. The value of the grand total is inside a hidden field. When click on submit, the _POST array doesn't get back the last value of the grand total. I need to hit the button twice to get the last value. The weird thing is when I echo the value of the grand total it display the latest value, but not with the POST array For example. If the grand total is $10.00 and I click on submit. It will show the POST['grand_total'] as empty. If I click on submit again it will show the grand total of $10.00. Below is my code that I am working with. Any help would be really appreciated. if(isset($_POST['submit'])) { /* Doesn't show if i put it after if($_POST['submit'] */ if(isset($_POST['grand_total'])) { echo $_POST['grand_total']; } } //A bunch of other html/php code. Another class calculates the subtotal assigns it the variable $subtotal $cart_totals = new cartTotals($subtotal, $discounted_amount,$post_values->tip); // Cart class is shown below /* Doesn't show if i put it before if($_POST['submit'] */ if(isset($_POST['grand_total'])) { echo $_POST['grand_total']; } echo "<input name='grand_total' type='hidden' value='$cart_totals->grand_total' />"; // Shows the grand total after second from submission echo "$cart_totals->grand_total"; // Shows grand total after the first submission Cart Totals Class class cartTotals { public $subtotal; public $sales_tax; public $tip; public $grand_total; public $discount_amount; public $href_page; public $invalidCouponMessage; const TEST_ENVIORMENT = FALSE; /** * [ Function gets constructed in the order summary where the [$discount_amount= ""] arg does need to be passed. * But does get passed in when called on the checkout.php page. Therefore we set the default value to an empty string.] * @param [float] $subtotal [subtotal get passed in from the parent class coreCartFunction] * @param string $discount_amount [The class checkCouponCode calculates this discount amount based on the * subtotal and the discount amount. It gets instantiated on the clients side and passed is this construction function. * This is all done on the checkout page.] */ /*The way the construct function works is by invoking all the methods the passed arguments When the methods get invoked the do all the work and set the properties its values. The properties then get echoed out on the client side. */ function __construct($subtotal="", $discount_amount= "", $tip=""){ $this->subTotal($subtotal, $discount_amount);//SubTotal method takes the discount amount and subtracts it from the subtotal. $this->salesTax($subtotal, $discount_amount); $this->tip = $tip; $this->grandTotal(); } private function subTotal($subtotal,$discount_amount) { $rounded_subtotal = round($subtotal-$discount_amount,2); $money_format_subtotal = money_format('%i',$rounded_subtotal); $this->subtotal = $money_format_subtotal; } private function salesTax($subtotal, $discount_amount =""){ $sales_tax = (STORE_SALES_TAX)?(float)STORE_SALES_TAX:8.875; $sales_tax =(($this->subtotal)*$sales_tax)/100; $sales_tax = round($sales_tax,2); $this->sales_tax = $sales_tax; } public function Tip() { //global $post_values; //$last_tip_selected = $post_values->tip > 0 ? $post_values->tip : "" ; $tip_output = "<select id='tip' name='tip'>"; for($tip=0.00; $tip<=11.75; $tip+=0.25){ if( $tip == "2") {$selected = " selected";} else {$selected ="";} $formatted_tip = money_format('%i',$tip); $tip_output .= "<option {$selected} id='selected_tip' value='$formatted_tip'>"."$".$formatted_tip ."</option>".PHP_EOL; } $tip_output .= "</select>"; return $tip_output; } private function grandTotal(){ $grand_total = round($this->sales_tax+$this->subtotal+$this->tip,2); $grand_total_formatted = money_format('%i',$grand_total); $this->grand_total = $grand_total_formatted; } Link to comment https://forums.phpfreaks.com/topic/289290-post-value-shows-only-after-the-second-form-submession-ignore-last-post/ Share on other sites More sharing options...
Jacques1 Posted June 26, 2014 Share Posted June 26, 2014 Why do you want to submit the grand total at all? Why not calculate it on the checkout page? And I hope the value is purely informational? Because obviously you cannot trust the user input. Link to comment https://forums.phpfreaks.com/topic/289290-post-value-shows-only-after-the-second-form-submession-ignore-last-post/#findComment-1483237 Share on other sites More sharing options...
eldan88 Posted June 26, 2014 Author Share Posted June 26, 2014 Because it needs to be validated to see if it is greater than the minumum delivery Link to comment https://forums.phpfreaks.com/topic/289290-post-value-shows-only-after-the-second-form-submession-ignore-last-post/#findComment-1483240 Share on other sites More sharing options...
Jacques1 Posted June 26, 2014 Share Posted June 26, 2014 My question is why you want to tunnel the grand total through a form instead of calculating it when needed. It makes no sense to let the user tell you the value. What if they're lying? Link to comment https://forums.phpfreaks.com/topic/289290-post-value-shows-only-after-the-second-form-submession-ignore-last-post/#findComment-1483241 Share on other sites More sharing options...
eldan88 Posted June 26, 2014 Author Share Posted June 26, 2014 I fixed the issue by constructing my object at the top of th page. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/289290-post-value-shows-only-after-the-second-form-submession-ignore-last-post/#findComment-1483247 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.