SieRobin Posted March 4, 2009 Share Posted March 4, 2009 Hi I need help with an application that needs to be created for school. It all works aside from when it gets to the bottom of the script it will not calculate the total price including the shipping cost. I've been slaving over it for quite some time now and could really use some help. Here's the code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Ma and Pa - General Goods</title> </head> <body> <?php $snugQuant = $_POST["quantity1"]; $shamQuant = $_POST["quantity2"]; $hercQuant = $_POST["quantity3"]; $slapQuant = $_POST["quantity4"]; $mightyQuant = $_POST["quantity5"]; $snugCheck = $_POST["product1"]; $shamCheck = $_POST["product2"]; $hercCheck = $_POST["product3"]; $slapCheck = $_POST["product4"]; $mightyCheck = $_POST["product5"]; if (isset($snugCheck)) { $snugtotal = $snugQuant * 19.95; } if (isset($shamCheck)) { $shamtotal = $shamQuant * 19.95; } if (isset($hercCheck)) { $herctotal = $hercQuant * 9.95; } if (isset($slapCheck)) { $slaptotal = $slapQuant * 24.95; } if (isset($mightyCheck)) { $mightytotal = $mightyQuant * 19.95; } ?> <form action="project.php" method="post"> <p> <b>Snuggie - $19.95 ea.</b> <input type="checkbox" name="product1"> <input type="text" name="quantity1"> <br> <b>Shamwow - $19.95 5 pk.</b> <input type="checkbox" name="product2"> <input type="text" name="quantity2"> <br> <b>Hercules Hook - $9.95 30 pk.</b> <input type="checkbox" name="product3"> <input type="text" name="quantity3"> <br> <b>Slap Chop - $24.95 ea.</b> <input type="checkbox" name="product4"> <input type="text" name="quantity4"> <br> <b>Mighty Mendit - $19.95 ea.</b> <input type="checkbox" name="product5"> <input type="text" name="quantity5"> </p> <input type="submit" name="submit1" value="Submit"> </form> <?php $obj = new shipping; $obj->setSubTotal($snugtotal, $shamtotal, $herctotal, $slaptotal, $mightytotal); echo "Your subtotal for this order is: $" . $obj->getSubTotal() . "<br>"; if (isset($_POST['submit1'])) { ?> <form action="project.php" method="post"> <p> <b>1st Class Shipping - $19.99</b> <input type="radio" name="shipping1" value="1"><br> <b>2nd Class Shipping - $14.99</b> <input type="radio" name="shipping1" value="2"><br> <b>Standard Shipping - $5.95</b> <input type="radio" name="shipping1" value="3"> <input type="submit" name="back" value="Back"> <input type="submit" name="submit2" value="Submit"> </p> </form> <?php } if (isset($_POST['submit2'])) { $obj->setShipping($_POST['shipping1']); $obj->setTotal($obj->getSubTotal(), $obj->getShipping()); echo "Your total for this order is: $" . $obj->getTotal() . "<br>"; echo $obj->getShippingInfo(); } class shipping { private $subtotal; private $snugtotal; private $shamtotal; private $herctotal; private $slaptotal; private $mightytotal; private $shipping; private $shipping1; private $shippingInfo; private $total; public function setSubTotal($snugtotal, $shamtotal, $herctotal, $slaptotal, $mightytotal) { $this->snugtotal = $snugtotal; $this->shamtotal = $shamtotal; $this->herctotal = $herctotal; $this->slaptotal = $slaptotal; $this->mightytotal = $mightytotal; $this->subtotal = $snugtotal + $shamtotal + $herctotal + $slaptotal + $mightytotal; } public function getSubTotal() { return $this->subtotal; } public function setTotal($subtotal, $shipping) { $this->subtotal = $subtotal; $this->shipping = $shipping; $this->total = $subtotal + $shipping; } public function getTotal() { return $this->total; } public function setShipping($shipping1) { $this->shipping1 = $_POST["shipping1"]; if ($this->shipping1 == "1") { $this->shipping = 19.99; $this->shippingInfo = "Your order will arrive in 1 day."; } elseif ($this->shipping1 == "2") { $this->shipping = 14.99; $this->shippingInfo = "Your order will arrive in 2-3 days."; } elseif ($this->shipping1 == "3") { $this->shipping = 5.95; $this->shippingInfo = "Your order will arrive in 5-7 days."; } } public function getShipping() { return $this->shipping; } public function getShippingInfo() { return $this->shippingInfo; } } ?> </body> </html> Anyhow, if you'd like to plug the code in yourself and give it a run from localhost, please do. It won't calculate the total at all. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
WolfRage Posted March 4, 2009 Share Posted March 4, 2009 You made me have to think about this for awhile. Especially because I am still learning OOP; as well. But I found the problem or at least a problem. You can not calculate your total because it is getting lost. How you never re $_POST the subtotal of the products in a hidden field. When the script is recalled or submitted between tasks, it in affect creates a new class and the old $_POST values are flushed, there fore all of the old values are no longer in the new class. You have to import them into your code from a $_POST value, not your getSubTotal() function. Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 5, 2009 Author Share Posted March 5, 2009 Hmmn confused sorry. I'm used to scripting in Java that the class will retain those values until the end of the program and the garbage collector comes around and picks it up. However, I'm not understanding what you're saying I guess. If the subtotal is stored in a variable, why wouldn't I be able to just access it still? Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 5, 2009 Author Share Posted March 5, 2009 Ok so I kind of got what you said and made progress. If I use one form instead of two it works fine, since it's not posting from two different forms just one. However, how do I do this with two? Problem is when the second submit button is pressed quantity1, quantity2, etc.. have no values, therefore it can't calculate anything. So how would I do this? Quote Link to comment Share on other sites More sharing options...
WolfRage Posted March 5, 2009 Share Posted March 5, 2009 Alright there are actually several things that need to be corrected in the code. One this is set up as a single self referring page, yet the first form is always displayed and the second some times. That is OK but you could at least refill it with the values submitted by the user. So if this is supposed to be two separate scripts please break them out so I can tell. Or if it is going to be self referring try to make it display one form then the other by checking if your submit value has been activated. Below I have given you an example of how to pass your data in a form using a hidden field. Once you do this that means you have to set your script up to receive that $_POST variable, so you can calculate your actual totals. OK on your second form you need to do this to keep the data going. Although there is another more secure option I will explain later. <form action="project.php" method="post"> <p> <b>1st Class Shipping - $19.99</b> <input type="radio" name="shipping1" value="1"><br> <b>2nd Class Shipping - $14.99</b> <input type="radio" name="shipping1" value="2"><br> <b>Standard Shipping - $5.95</b> <input type="radio" name="shipping1" value="3"> <input type="hidden" name="subtotal" value="<?php echo $obj->getSubTotal(); ?>" /> <input type="submit" name="back" value="Back"> <input type="submit" name="submit2" value="Submit"> </p> </form> If you are going to use the $_POST method then make sure you clean the variables, even if you are expecting the information to come from your form, some one can modify that information. Your second options is to use sessions, with sessions you can store the data in $_SESSION and then recall it for use on the second and third pages. Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 5, 2009 Author Share Posted March 5, 2009 I tried sessions and still on the next submit to project.php it will still throw the variables out of the session, which makes no sense at all. However, I have it set to show the next form on an isset that the submit1 button was hit. This means nothing however, since when you hit the second submit button, the action is for project.php again, deleting all the variables from the first spin. Like I said, I'm used to Java where Java always initiates space in memory to fill in variables and holds them till the app is done. I'm not used to programming in PHP without a database, which would make this problem 10 times easier. Anyhow, if I use one form, it works perfectly but the object of the assignment is to use multiple forms. Also, I'm not interested at this point in what way the user can alter the information, it doesn't even work yet so why would I care at this point? I will try the hidden text field as you presented although I didn't want to do that. If that's the only way however then I'll use it. Thank you though :] Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 5, 2009 Author Share Posted March 5, 2009 Ok quick update. This hidden field doesn't work.. it's still deleting all variables when the action is called from the second form. This is obnoxious and quite frankly I think almost impossible to be done. Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 5, 2009 Author Share Posted March 5, 2009 Anyone know how this can be done by using two different forms? What's the easiest way to do it? Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 5, 2009 Author Share Posted March 5, 2009 Nevermind, solved I got it. I used a GET to pull the subtotal out of the address. It works, not the exact way I wanted it to but it did what I wanted it to. Only problem with this is, GET can be altered very easily. Thank you for the help though. Quote Link to comment Share on other sites More sharing options...
revraz Posted March 5, 2009 Share Posted March 5, 2009 Use a session instead Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 5, 2009 Author Share Posted March 5, 2009 As I stated before, sessions don't work. When the next submit button is activated it reloads project.php, therfore creating a new session? Quote Link to comment Share on other sites More sharing options...
revraz Posted March 5, 2009 Share Posted March 5, 2009 Then you need to trouble shoot why sessions are not working. You don't explain why it's not working really. Post the code where you use sessions at and let's see why they don't work. Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 6, 2009 Author Share Posted March 6, 2009 I tried to use sessions, but deleted the code because it didn't work. If you think you can make it work why don't you just try it with one of the variables? I couldn't get it to work, however perhaps you're right I wasn't using them correctly but I believe I was lol. If you can post me a quick tutorial on sessions of what I don't know, that'd be cool too. Thank you in advance. Edit: Also, when I used sessions, I posted an array of what was in each session variable.. before and after the second submit button was hit. The variables were in session when the first was hit.. NATURALLY, when the second was hit, the variables were null. Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 6, 2009 Author Share Posted March 6, 2009 Just to show you, maybe you can get it to work. I tried controlling the subtotal by a session, here it is. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Ma and Pa - General Goods</title> </head> <body> <?php session_start(); echo session_id(); $snugQuant = $_POST["quantity1"]; $shamQuant = $_POST["quantity2"]; $hercQuant = $_POST["quantity3"]; $slapQuant = $_POST["quantity4"]; $mightyQuant = $_POST["quantity5"]; $snugCheck = $_POST["product1"]; $shamCheck = $_POST["product2"]; $hercCheck = $_POST["product3"]; $slapCheck = $_POST["product4"]; $mightyCheck = $_POST["product5"]; if ($snugCheck) { $snugtotal = $snugQuant * 19.95; } if ($shamCheck) { $shamtotal = $shamQuant * 19.95; } if ($hercCheck) { $herctotal = $hercQuant * 9.95; } if ($slapCheck) { $slaptotal = $slapQuant * 24.95; } if ($mightyCheck) { $mightytotal = $mightyQuant * 19.95; } if ($_POST['submit1']) { $hidden = "hidden"; } else { $hidden = "Submit"; } ?> <form name="transaction" action="project.php" method="post"> <p> <b>Snuggie - $19.95 ea.</b> <input type="checkbox" name="product1" <?php if ($_POST['product1']) echo ' checked="checked"'; ?>> <input type="text" name="quantity1" value="<?php echo $snugQuant; ?>"> <br> <b>Shamwow - $19.95 5 pk.</b> <input type="checkbox" name="product2" <?php if ($_POST['product2']) echo ' checked="checked"'; ?>> <input type="text" name="quantity2" value="<?php echo $shamQuant; ?>"> <br> <b>Hercules Hook - $9.95 30 pk.</b> <input type="checkbox" name="product3" <?php if ($_POST['product3']) echo ' checked="checked"'; ?>> <input type="text" name="quantity3" value="<?php echo $hercQuant; ?>"> <br> <b>Slap Chop - $24.95 ea.</b> <input type="checkbox" name="product4" <?php if ($_POST['product4']) echo ' checked="checked"'; ?>> <input type="text" name="quantity4" value="<?php echo $slapQuant; ?>"> <br> <b>Mighty Mendit - $19.95 ea.</b> <input type="checkbox" name="product5" <?php if ($_POST['product5']) echo ' checked="checked"'; ?>> <input type="text" name="quantity5" value="<?php echo $mightyQuant; ?>"> </p> <input type="<?php echo $hidden; ?>" name="submit1" value="Submit"> </form> <?php $obj = new shipping; $obj->setSubTotal($snugtotal, $shamtotal, $herctotal, $slaptotal, $mightytotal); $_SESSION['subtotal'] = $obj->getSubTotal(); print_r($_SESSION); echo "<br>"; if ($_POST['submit1']) { echo "Your subtotal for this order is: $" . $_SESSION['subtotal'] . "<br>"; ?> <form name="shipping" action="project.php?subtotal=<?php echo $obj->getSubTotal(); ?>" method="post"> <p> <b>1st Class Shipping - $19.99</b> <input type="radio" name="shipping1" value="1"><br> <b>2nd Class Shipping - $14.99</b> <input type="radio" name="shipping1" value="2"><br> <b>Standard Shipping - $5.95</b> <input type="radio" name="shipping1" value="3"> </p> <input type="submit" name="submit2" value="Submit"> <input type="submit" name="back" value="Back"> </form> <?php } if ($_POST['submit2']) { $obj->setShipping($_POST['shipping1']); $obj->setTotal($_SESSION['subtotal'], $obj->getShipping()); echo "Your total for this order is: $" . $obj->getTotal() . "<br>"; echo $obj->getShippingInfo(); } class shipping { public $subtotal; public $snugtotal; public $shamtotal; public $herctotal; public $slaptotal; public $mightytotal; public $shipping; public $shipping1; public $shippingInfo; public $total; public function setSubTotal($snugtotal, $shamtotal, $herctotal, $slaptotal, $mightytotal) { $this->snugtotal = $snugtotal; $this->shamtotal = $shamtotal; $this->herctotal = $herctotal; $this->slaptotal = $slaptotal; $this->mightytotal = $mightytotal; $this->subtotal = $snugtotal + $shamtotal + $herctotal + $slaptotal + $mightytotal; } public function getSubTotal() { return $this->subtotal; } public function setTotal($subtotal, $shipping) { $this->subtotal = $subtotal; $this->shipping = $shipping; $this->total = $subtotal + $shipping; } public function getTotal() { return $this->total; } public function setShipping($shipping1) { $this->shipping1 = $_POST["shipping1"]; if ($this->shipping1 == "1") { $this->shipping = 19.99; $this->shippingInfo = "Your order will arrive in 1 day."; } elseif ($this->shipping1 == "2") { $this->shipping = 14.99; $this->shippingInfo = "Your order will arrive in 2-3 days."; } elseif ($this->shipping1 == "3") { $this->shipping = 5.95; $this->shippingInfo = "Your order will arrive in 5-7 days."; } } public function getShipping() { return $this->shipping; } public function getShippingInfo() { return $this->shippingInfo; } } ?> </body> </html> Anyway, if you can tell me if I did something wrong let me know. But when you hit the first button, the subtotal is in $_SESSION['subtotal'] and when the second button is pressed the variable is set to 0. Don't ask me why or how it's occurring but it is. Please try it out, copy my code and plug it in and give it a run, that's what it does for me lol. Edit: Also you can notice that the session ID is never changed therefore it's the same session, but the variable keeps resetting lol. Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 6, 2009 Author Share Posted March 6, 2009 Here are some screenshots of the process. I even serialized and unserialized my object, that way it would store EVEN the object's variables. After submit 2, they're magically set to 0.. and I know why? As you can clearly see it's a new session. As you can see the variables are clearly put into the session. So they ARE stored, right? Now as you can see the variables that WERE recorded are at 0, really like what is that? If anyone knows the problem, please shout it out because it's frustrating. Quote Link to comment Share on other sites More sharing options...
revraz Posted March 6, 2009 Share Posted March 6, 2009 First thing you need to do is put session_start(); before any output gets to the browser. If you enable error display and reporting, you would see a HEADER error on your seesion_start() line. Google "session tutorial php" Quote Link to comment Share on other sites More sharing options...
WolfRage Posted March 7, 2009 Share Posted March 7, 2009 Your code is at fault, you need to protect the session variables with an if statement. ?>php if(!isset($_SESSION['subtotal'])) { $_SESSION['subtotal'] = $obj->getSubTotal(); } else { /* Don't reinitialize it with the class!!!!!! And don't reinitialize any of the other variables that you have stored in $_SESSION, like your product totals or else they will equal zero!*/ } ?> PHP is different from JAVA, it does not stay active or save things in memory, it changes from page to page it is a stateless programing language, and there are only a few ways to give it state. You don't get to save things to memory or use client resources, instead you only get to use server resources and those are limited especially when every request looks just like any other. $_SESSION , $_COOKIE , $_POST , and $_GET . Nothing else is saved, and if you reinitialize a class and do not protect these variables then yes they will be reset, and $_POST and $_GET variables are flushed on the very next page load, so if you load one page using get and you still want to pass information to the next page you need to re submit these variables to pass this information on again to the third page. Excuse my outburst but you need to separate the way that these two languages operate if you intend to be successful in using php. Quote Link to comment Share on other sites More sharing options...
SieRobin Posted March 8, 2009 Author Share Posted March 8, 2009 What? Your code is at fault, you need to protect the session variables with an if statement. ?>php if(!isset($_SESSION['subtotal'])) { $_SESSION['subtotal'] = $obj->getSubTotal(); } else { /* Don't reinitialize it with the class!!!!!! And don't reinitialize any of the other variables that you have stored in $_SESSION, like your product totals or else they will equal zero!*/ } ?> PHP is different from JAVA, it does not stay active or save things in memory, it changes from page to page it is a stateless programing language, and there are only a few ways to give it state. You don't get to save things to memory or use client resources, instead you only get to use server resources and those are limited especially when every request looks just like any other. $_SESSION , $_COOKIE , $_POST , and $_GET . Nothing else is saved, and if you reinitialize a class and do not protect these variables then yes they will be reset, and $_POST and $_GET variables are flushed on the very next page load, so if you load one page using get and you still want to pass information to the next page you need to re submit these variables to pass this information on again to the third page. Excuse my outburst but you need to separate the way that these two languages operate if you intend to be successful in using php. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.