Page 1 of 1
Obtaining values in a list box
Posted: Mon Aug 27, 2007 10:29 am
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?
Posted: Mon Aug 27, 2007 10:56 am
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.
Posted: Mon Aug 27, 2007 11:19 am
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>
Posted: Mon Aug 27, 2007 11:42 am
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.
copy-paste your code
Posted: Mon Aug 27, 2007 1:08 pm
by yacahuma
just paste your page2 on top of page one and change the action to be page1