Jump to content

Recommended Posts

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

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.

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?

 

 

 

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']
}

?>

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.