Jump to content

POST value shows only after the second form submession? (Ignore last post)


eldan88
Go to solution Solved by eldan88,

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.