Obtaining values in a list box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Obtaining values in a list box

Post by SirChick »

I have a list box as shown below:

Code: Select all

<?php
$soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC";
$soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error());
while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
	echo "<option value=\"{$soldhousesrow['Price']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
}
?>

And im trying to work out how to obtain the choice the user has picked and to put them into a variable ready for inserting into the database.

Firstly there are two values in the list box that are included these are "Price" and "House Type". How would i get the user's choice in to two variables as $Price and $HouseType?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

page1.php - Form with selectbox

Code: Select all

<?php
$soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC";
$soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error());

echo '<form method="post" action="page2.php">';
echo '<select name="users_choice">';

while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
        //Separating price and house type with | symbol
        echo "<option value=\"{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
}

echo '</select>';
echo '<input type="submit" />';
echo '</form>';

?>
page2.php - Form is submitted to this page

Code: Select all

<?php

$price = 0;
$houseType = '';

if (isset($_POST['users_choice']))
{
  $val = $_POST['users_choice'];
  $pos = strpos($val, '|');

  if ($pos !== false)
  {
    //Price of the house is before | symbol
    $price = substr($val, 0, $pos);
    //House type is after | symbol
    $houseType = substr($val, $pos+1);
  }
}
?>
Edit:
On page2.php house price and type is received from user, so you can't be sure that these are the same values which you took from database.
Instead of putting in the select box price and house type, you should put primary (unique) key of the data from database.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

is there a way the process can occrus on the same page?


This is currently what i got for the form:

Code: Select all

<form name="Form1" method="POST" action="" enctype="multipart/form-data" id="Form1" onsubmit="return ValidateForm1(this)">



<select name="BuyCombo" size="2" id="Combobox3" style="position:absolute;left:343px;top:520px;width:223px;font-family:MS Shell Dlg;z-index:25">

<?php
$soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC";
$soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error());


echo '<select name="houselist">';

while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
        echo "<option value=\"{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
}

echo '</select>';
echo '<input type="submit" />';
echo '</form>';

?>

</select>
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Yes you can add the processing code in the same script.

@kaszu preg_match_all() is much useful and fast than strpos/substr.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

copy-paste your code

Post by yacahuma »

just paste your page2 on top of page one and change the action to be page1
Post Reply