Endraca Posted March 12, 2008 Share Posted March 12, 2008 Firstly: Please excuse me if my post is a bit awkward, newer posted anything of this kind before. This is part of a university assignment I have to do,not very complicated stuff, basically we have created 3 arrays , one for $drinks,$desserts, and $meals, for example: $drinks= array ( 'Spring Water'=>'7', 'Coke'=>'6', 'Coffee'=>'4', 'Tea'=>'5', 'Hot Chocolate'=>'8' ); with this array we have created 3 drop down lists like this: <?php print "<tr><td>Select your drink</td><td><select name='drink'>"; foreach($drinks as $drink=>$price2) { print "<option value='$drink'>$drink - R $price2</option>"; } ?> print "</select> This information is submitted to my next page where I have to add the prices of the 3 items...and there is where my problem lies I seem to be able to use the "$drink" bit of the array and not the "$price2" bit of it. This is probably caused by my lack of understanding of arrays, but basically I want to know why, for example print "{$_POST['drink']} {$_POST['price2']} </br>"; doesn't print the price with the drink I chose and I can't seem to use $price2 in any calculations So my main question after all of this is how can I get "$price2" in my next php page so I can do calculations with it? Quote Link to comment Share on other sites More sharing options...
paul2463 Posted March 12, 2008 Share Posted March 12, 2008 if its part of a university assignment shouldnt you be working out for yourself instead of asking for help on here?? to help you on your way - a select box will only pass one piece of information, which is what you have told it is the "option value", in your case the value of $drink, not the value of $price2 Quote Link to comment Share on other sites More sharing options...
Endraca Posted March 12, 2008 Author Share Posted March 12, 2008 Our lecturer said if we get stuck we are allowed to ask here (he named this forum), though if I am still stuck by tomorrow I will go and ask him So what you are saying is that if I need both values (which I do) I can't use a select box to get both values? Personally I'd rather have used check boxes for this exercise, but we are told to use a select box though, so there has to be another way for me to get the other value ($price2). There was a point where I was able to print for example "Tea 5" but I still can't do calculations with "$price2". Guess I'll just ask the lecturer tomorrow, luckily it only needs to be in by Friday. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 12, 2008 Share Posted March 12, 2008 You can use a select that allows multiple selections. <SELECT NAME='selection1[]' MULTIPLE> <OPTION VALUE='option1'>Option 1</OPTION> <OPTION VALUE='option2'>Option 2</OPTION> <OPTION VALUE='option3'>Option 3</OPTION> </SELECT> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 12, 2008 Share Posted March 12, 2008 You can pass the select values as you are now but store the arrays as session variables so they available to the next page too. Quote Link to comment Share on other sites More sharing options...
paul2463 Posted March 13, 2008 Share Posted March 13, 2008 if you want to pass both pieces of information across using the select box you could use <?php print "<tr><td>Select your drink</td><td><select name='drink'>"; foreach($drinks as $drink=>$price2) { print "<option value='$price2/$drink'>$drink - R $price2</option>"; //value now equals both pieces of info separated by a / print "</select>"; ?> which when passed across requires the use of explode to get both pices away from each other such as <?php $food = $_POST['drink']; $pieces = explode('/',$food); //re-separate the data echo "$pieces[0] - $pieces[1]"; //print them out or use them where you need them ?> hope that helps a bit more 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.