Mordero Posted December 1, 2006 Share Posted December 1, 2006 I am taking a php class in college as an elective...I have an assignment to take four numeric inputs from the user, assign them to session variables, echo them out on a new page within a table. What I can't figure out is how to keep the amounts from resetting..I need for the user to be able to "add more to cart" so to speak. here is the code.INPUT FIELDS (fruity.php)<h1>Welcome to the Client & Server Side Fruit Store</h1><div id="fixedcontainer"><form action="fruitbasket.php" method="post"><div class="fruit"><img src="bananas.jpg" alt="bananas mon!" /><p>Qty: <input type="text" size="2" maxlength="2" name="banana" id="banana" /></p></div><div class="fruit"><img src="apples.jpg" alt="apples mon!" /><p>Qty: <input type="text" size="2" maxlength="2" name="apple" id="apple" /></p></div><div class="fruit"><img src="papaya.jpg" alt="papayas mon!" /><p>Qty: <input type="text" size="2" maxlength="2" name="papaya" id="papaya" /></p></div><div class="fruit"><img src="pineapple.jpg" alt="pineapples mon!" /><p>Qty: <input type="text" size="2" maxlength="2" name="pineapple" id="pineapple" /></p></div><input type="submit" value="Add to Fruitbasket" name="fruitsub" /></form></div>FRUITBASKET.php (this is where I need to keep track of the number of fruits they want..has to be updated if they select continue shopping)<?phpsession_start();$getbanana=$_POST['banana'];$getapple=$_POST['apple'];$getpapaya=$_POST['papaya'];$getpineapple=$_POST['pineapple'];$_SESSION['totbananas'] = $getbanana;$_SESSION['totapples'] = $getapple;$_SESSION['totpapayas'] = $getpapaya;$_SESSION['totpineapples'] = $getpineapple;echo "<table border='1'>";echo "<tr>";echo "<td>Bananas</td><td>Qty=</td><td>" . $_SESSION['totbananas'] . "</td>";echo "</tr>";echo "<tr>";echo "<td>Apples</td><td>Qty=</td><td>" . $_SESSION['totapples'] . "</td>";echo "</tr>";echo "<tr>";echo "<td>Papayas</td><td>Qty=</td><td>" . $_SESSION['totpapayas'] . "</td>";echo "</tr>";echo "<tr>";echo "<td>Pineapples</td><td>Qty=</td><td>" . $_SESSION['totpineapples'] . "</td>";echo "</tr>";echo "</table";?><a href="fruity.php">Continue Shopping</a></body></html>--------I'm not trying to be a hardcore php programmer, I'm just trying to make it through this class...any help would be greatly appreciated! I know i know, i'm a n00b ; ( Link to comment https://forums.phpfreaks.com/topic/29155-new-to-php-quick-question/ Share on other sites More sharing options...
keeB Posted December 1, 2006 Share Posted December 1, 2006 what you need to do is use[code=php:0]if(isset($_SESSION['totbananas'])) { $temp = $_SESSION['totbananas']; $_SESSION['totbananas'] = $getbanana + $temp;}[/code]:) I would write a function for it personally, but if you don't want to be 'hardcore', why? :) Link to comment https://forums.phpfreaks.com/topic/29155-new-to-php-quick-question/#findComment-133657 Share on other sites More sharing options...
Mordero Posted December 1, 2006 Author Share Posted December 1, 2006 thanks for the quick reply. I'm guessing the if(isset($_SESSION['totbananas'])) statement should go in the fruitbasket.php correct? When I do that my amount is getting doubled...i enjoyed javascript so much more... Link to comment https://forums.phpfreaks.com/topic/29155-new-to-php-quick-question/#findComment-133666 Share on other sites More sharing options...
Caesar Posted December 1, 2006 Share Posted December 1, 2006 [quote author=Mordero link=topic=117036.msg477238#msg477238 date=1165008104]i enjoyed javascript so much more...[/quote]Heresy. Link to comment https://forums.phpfreaks.com/topic/29155-new-to-php-quick-question/#findComment-133669 Share on other sites More sharing options...
Mordero Posted December 1, 2006 Author Share Posted December 1, 2006 can someone please let me know what i'm doing wrong? I know it is something simple but I just can't figure it out. Oh, I don't like javascript anymore than php..its just that I could get javascript to work....please help! Link to comment https://forums.phpfreaks.com/topic/29155-new-to-php-quick-question/#findComment-133687 Share on other sites More sharing options...
Mordero Posted December 2, 2006 Author Share Posted December 2, 2006 bump Link to comment https://forums.phpfreaks.com/topic/29155-new-to-php-quick-question/#findComment-133767 Share on other sites More sharing options...
Caesar Posted December 5, 2006 Share Posted December 5, 2006 This might point you in the right direction:[code]<?php session_start(); if(isset($_POST['submit'])) { $banana = $_POST['banana']; $apple = $_POST['apple']; $papaya = $_POST['papaya']; $pineapple = $_POST['pineapple']; $_SESSION['totbananas'] = $banana_sess; $_SESSION['totapples'] = $apple_sess; $_SESSION['totpapayas'] = $papaya_sess; $_SESSION['totpineapples'] = $pineapple_sess; addfruit(); echo" <table align=\"center\"> <tr><td>Bananas = </td><td>$_SESSION['totbananas']</td></tr> <tr><td>Bananas = </td><td>$_SESSION['totapples']</td></tr> <tr><td>Bananas = </td><td>$_SESSION['totpapayas']</td></tr> <tr><td>Bananas = </td><td>$_SESSION['totpineapples']</td></tr> </table>"; } function addfruit() { $_SESSION['totbananas'] = ($bananas + $banana_sess); $_SESSION['totapples'] = ($apples + $apple_sess); $_SESSION['totpapayas'] = ($papaya + papaya_sess); $_SESSION['totpineapples'] = ($pineapples + $pineapple_sess); }?>[/code]And yes...those values will reset if the user closes their browser. I suggest using cookies if you'd like it to keep those values if they accidently close their browser. Link to comment https://forums.phpfreaks.com/topic/29155-new-to-php-quick-question/#findComment-135657 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.