SirChick Posted August 31, 2007 Share Posted August 31, 2007 I have an option box which loads the options from my tables with queries. But im not sure how to put the user's choice into a variable. the option box has : housetype and price then i was trying to put $price and $housetype to store what ever the user chose into a session so it could be carried across the page. But im not sure how to do it.. i gave it a try this is what i did: This first bit works perfectly: <?php $soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC"; $soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error()); echo '<select name="houselist" size=10>">'; while($soldhousesrow = mysql_fetch_array($soldhousesresult)) { echo "<option value=\"{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>"; } echo '</select>'; ?> Then on the same page at the top i put : session_start(); $Price = $soldhousesrow['Price']; im assuming this is wrong as it wont echo' any thing when i try it. I was going to put it using an if isset with the buy button but because that will take the page to a different process page i could not do it that way as then the page would have changed before the $price was even set.. Link to comment https://forums.phpfreaks.com/topic/67490-obtain-option-into-session/ Share on other sites More sharing options...
pocobueno1388 Posted August 31, 2007 Share Posted August 31, 2007 Give this a try: <?php session_start(); if (isset($_POST['houselist'])){ $houselist = $_POST['houselist']; //set the session $_SESSION['houselist'] = $houselist; //check if it worked if (isset($_SESSION['houelist'])){ echo 'Session set!<br>'; echo $_SESSION['houselst']; } } $soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC"; $soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error()); echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"> <select name="houselist" size=10>">'; while($soldhousesrow = mysql_fetch_array($soldhousesresult)) { echo "<option value=\"{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>"; } echo '</select></form>'; ?> Remember to call session_start() on all the pages you want the session to continue to. Link to comment https://forums.phpfreaks.com/topic/67490-obtain-option-into-session/#findComment-338833 Share on other sites More sharing options...
micah1701 Posted August 31, 2007 Share Posted August 31, 2007 <?php session_start(); //this should be thefirst line of your page ... //the $_SESSION is an array $_SESSION['Price'] = $soldhousesrow['Price']; ?> ; next page: <?php session_start(); echo $_SESSION['Price'] ?> Link to comment https://forums.phpfreaks.com/topic/67490-obtain-option-into-session/#findComment-338835 Share on other sites More sharing options...
SirChick Posted August 31, 2007 Author Share Posted August 31, 2007 Echo $_SESSION['Price']; didn't echo. What i find strange is this. I have: $_SESSION['HouseType'] = $soldhousesrow['HouseType']; $_SESSION['Price'] = $soldhousesrow['Price']; Then: $Price = $_SESSION['Price']; $HouseType = $_SESSION['HouseType']; Yet only house type echo's when i have this: Echo $Price; Echo $_SESSION['Price']; Echo $HouseType; But i know for a fact that price exists cos the option box is loading it into the box. So why isnt it assigning it to the session from what the user chooses? Link to comment https://forums.phpfreaks.com/topic/67490-obtain-option-into-session/#findComment-338863 Share on other sites More sharing options...
darkfreaks Posted August 31, 2007 Share Posted August 31, 2007 try: <?php $price= $_SESSION['price']; print_r ($price); ?> Link to comment https://forums.phpfreaks.com/topic/67490-obtain-option-into-session/#findComment-338878 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 Maybe something like this: <?php session_start(); $soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC"; $soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error()); echo "<form action='{$_SERVER['PHP_SELF']}' method='post'><select name='houselist' size='10'>"; while($soldhousesrow = mysql_fetch_assoc($soldhousesresult)) { echo "<option value=\"{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>"; } echo "</select> <input type='submit' name='submit' value='Submit' /> </form>"; if ($_POST['submit']){ $option = $_POST['houselist']; $split = explode("|", $option); $_SESSION['housePrice'] = $split[0]; $_SESSION['houseType'] = $split[1]; echo $_SESSION['housePrice'] . ", " $_SESSION['houseType'] } ?> Link to comment https://forums.phpfreaks.com/topic/67490-obtain-option-into-session/#findComment-338882 Share on other sites More sharing options...
SirChick Posted August 31, 2007 Author Share Posted August 31, 2007 print_r ($price); didnt work. Ken - the echo's are on a separate page mind so the "if submit" cant be an option cos the form calls up a process.php.. which is a redirect in itself. Is it still work able some how on the next page :S Link to comment https://forums.phpfreaks.com/topic/67490-obtain-option-into-session/#findComment-338886 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 Then just put that function in the process.php file :S Link to comment https://forums.phpfreaks.com/topic/67490-obtain-option-into-session/#findComment-338893 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.