Set select option using php when reloading the same page - php

I have a php select populated and given a matching value by using php to loop through the results of a sql query.
$result = mysqli_query($con, "select * from course")
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['Title'] ." '>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
That gives me a drop down list populated with all course titles,
upon submission I can access the selected value using $_POST['CourseSelect'];
However the drop down (select) resets itself to the default value when the page reloads.
How can I keep that option selected using php?
I know that I can use the selected keyword inside of an a select option to make that option the default selected option.
for example the second option would be selected when loading the page:
<select>
<option>One</option>
<option selected>Two</option>
<option>Three</option>
</select>

You can make it simple as
while($row = mysqli_fetch_array($result))
{
$select = '';
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] == $row['Title'] ) $select = 'SELECTED';
echo "<option value='".$row['Title']."' ".$select.">" . $row['Title'] . "</option>";
}

you can use like below..
$result = mysqli_query($con, "select * from course");
$selected = "";
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
$selected = $row['Title'] == $_REQUEST['CourseSelect'] ? "Selected" : "";
echo "<option value='" . $row['Title'] ." ' $selected>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";

try this:
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
$selected = $_POST['CourseSelect'] == $row['Title'] ? 'selected' : '';
echo "<option value='{$row['Title']}' {$selected}>{$row['Title']}</option>";
}

you can do like this
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] != "0")
{
echo "<option >".$_POST['CourseSelect']."</option>";
}
else
{
echo "<option value='0'> - Select Course - </option>";
}
while($row = mysqli_fetch_array($result))
{
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] != $row['Title'])
echo "<option value='" . $row['Title'] ." '>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";

PHP Function
# select box
/*
Example:
Parameter 1:
$options[1] = 'Course 1';
$options[2] = 'Course 2';
$options[3] = 'Course 3';
Parameter 2:
$selectedOption = 2; The dropdown need to be selected
*/
function buildOptions($options, $selectedOption)
{
foreach ($options as $value => $text)
{
if ($value == $selectedOption)
{
$Return .='<option value="'.$value.'" selected="selected">'.stripslashes($text).'</option>';
}
else
{
$Return .='<option value="'.$value.'">'.stripslashes($text).'</option>';
}
}
return $Return;
}
Function Call
$result = mysqli_query($con, "select * from course");
while ($row = mysqli_fetch_array($result))
{
$UniqueId = $row['Title'];
$Value = $row['Title'];
$optionsArray[$UniqueId] = $Value; // Store the values into an array
}
$CourseSelect = isset($_POST['CourseSelect']) ? $_POST['CourseSelect'] : '0';
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
echo buildOptions($optionsArray, $CourseSelect);
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
Notes
You can use buildOptions() for all of your projects to display select box. I have been using this for years.

Related

Multiple dropdowns from mysql

I want to make multiple dropdowns from data out of my mysql database. I want 4 dropdowns to be exact. This is what I have at this moment:
<?php
mysql_connect('#', '#', '#');
mysql_select_db('test');
$sql = "SELECT wie, waar, metwie, voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";
?>
For instance, this:
mysql_connect('#', '#', '#');
mysql_select_db('test');
$sql = "SELECT wie FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";
$sql = "SELECT waar FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
}
echo "</select>";
$sql = "SELECT metwie FROM data";
$result = mysql_query($sql);
echo "<select name='test2'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
}
echo "</select>";
$sql = "SELECT voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test3'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
}
echo "</select>";
?>
As stated by HawasKaPujaari, avoid using mysql. Use mysqli. You could use a conditional switch statement like this:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT wie, waar, metwie, voeruig FROM data")) {
printf("Select returned %d rows.\n", $result->num_rows);
while ($row = mysql_fetch_array($result)) {
switch ($row) {
case "wie":
echo "<select name='wie'>";
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
echo "</select>";
break;
case "waar":
echo "<select name='waar'>";
echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
echo "</select>";
break;
case "metwie":
echo "<select name='metwie'>";
echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
echo "</select>";
break;
}
case "voeruig":
echo "<select name='voeruig'>";
echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
echo "</select>";
break;
}
/* free result set */
$result->close();
}
?>
php and mysql are different softwares. what you are doing here is connecting php with mysql using mysql_*() functions
in your case what you get is an array in php. you can use this array for whatever you want. if you want to print array as is as for debugging use:
echo "<pre>";
print_r($row);
echo "</pre>";
From this you will get array structure
then you ca use different dropdowns for array elements
Note: mysql_*() is not safe. Use mysqli_* or PDO.
Try code below. hope it help you
<?php
$wie=array();
$waar =array();
$metwie =array();
$voeruig =array();
while ($row = mysql_fetch_array($result)){
$wie[]=$row["wie"];
$waar[]=$row["waar"];
$metwie[]=$row["metwie"];
$voeruig[]=$row["voeruig"];
}
?>
<select name="wie">
<?php
foreach($wie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?>
<php
}
?>
</option>
<select name="waar">
<?php
foreach($waar as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php
}
?>
</select>
<select name="metwie">
<?php
foreach($metwie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?></option>
<php
}
?>
</select>
<select name="voeruig">
<?php
foreach($voeruig as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php
}
?>
</select>

In the edit mode i need to show selected value from the dropdown in PHP

I have a dropdown in php the value is coming from database. In the edit mode i need to show selected value in to drop down. My php is below:
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
while ($row_currency = mysql_fetch_array($result_currency)) {
echo "<option value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] .">" . $row_currency['currency'] ."</option>";
}
echo "</select>";
echo "</td>";
?>
Try this code...
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
/*
* selected value
*/
$selectedValue = ""; // assign that value to this variable
while ($row_currency = mysql_fetch_array($result_currency)) {
$selected = "";
if($row_currency['currency'] == $selectedValue){
$selected = ' selected="selected" ';
}
echo "<option ".$selected." value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] . ">" . $row_currency['currency'] . "</option>";
}
echo "</select>";
echo "</td>";
?>

Keep Selection visible after Submit

How can I make the filtered selection still appear after the request.
So if I have options 1,2,and 3. When I select 2 and data shows I still want 2 to display signifying that the data is filtered through option 2.
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' tabindex='1' >";
while($row = mysql_fetch_array($result))
{
echo " <option value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo "</select>";
echo "<input type='submit' value='Filter' />";
echo "</form>";
You can do something like this :
$country = isset($_POST['Country']) ? $_POST['Country'] : '';
while($row = mysql_fetch_array($result))
{
echo " <option value='". $row['Fund_Manager_Company_Code'] ."' ".(($row['Fund_Manager_Company_Code'] == $country) ? 'selected="selected"' : '').">". $row['Fund_Manager_Company_Name'] ."</option>";
}
What you need is to add selected attribute to option:
$Country = $_POST['Country'];
$sected = 'selected = "selected" ';
while($row = mysql_fetch_array($result))
{
echo " <option ".($row['Fund_Manager_Company_Code'] == $Country? $selected : '')."value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
The one that the value selected and posted then will be selected...
something like
echo" <option value='" . $row['Fund_Manager_Company_Code'] . "' " . ((isset($_POST['Country']) && $_POST['Country'] == $row['Fund_Manager_Company_Code'])
? 'selected="selected"' : '') . ">" . $row['Fund_Manager_Company_Name'] . "</option>";
After your submit, you need to catch the selection in your PHP code:
$selection = $_POST['Country'];
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' tabindex='1' >";
while($row = mysql_fetch_array($result))
{
$selected = "";
if ($row['Fund_Manager_Company_Code'] == $selection) {
$selected = "selected";
}
echo " <option value='". $row['Fund_Manager_Company_Code'] ."' ".$selected.">". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo "</select>";
echo "<input type='submit' value='Filter' />";
echo "</form>";

Selected Value for MySQL generated Drop Down List

I have a mysql generate dropdown list..need to add in a pre selected option such as "Please Choose Availability". Not too sure how to do this as the drop down content is generated from a database
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='property'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
?>
if you want static first value for dropdown then you can do it like this.
echo "<select name='property'><option value=''>Please Choose Availability </option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
echo "<select name='property'><option value=''>Please Choose Availability</option>";

How to submit a form which contains mysql generated dropdown lists

Ive got a property website im working on for a school project http://www.holidayaviemore.com
I have three dropdown lists which have all been generated from a mysql database.
I have added a submit button so that when the options have been selected the database info will display..this is where I am really stuck!
Im not sure what code of functions are needed to diplay the content on the page or on another page..any ideas? This is the code for the dropdownn boxes and form I have so far..
EDIT
Basically I want the options selected to be displayed on a webpage http://www.holidayaviemore.com shows the dropdown lists
<p>Select Options From Below to find property</p>
<form action="#" name="form" id="form" method="post">
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='availability'><option value=''>- Availability?--</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
?>
<?php
$sql = "SELECT bedrooms FROM properties LIMIT 10";
$result = mysql_query($sql);
echo "<select name='bedrooms'><option value=''>--Please Choose Bedrooms Bedrooms--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['bedrooms'] . "'> " . $count . "</option>";
$count++;
}
echo "</select>";
?>
<?php
$sql = "SELECT sleeps_min FROM properties LIMIT 12";
$result = mysql_query($sql);
echo "<select name='sleeps_min'><option value=''>--Please Choose Guests--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['sleeps_min'] . "'> " . $count . "</option>";
$count++;
}
echo "</select>";
?>
<input type="submit" name="" value="GO" />
</form>
I think you need this. You need to see if form already submitted and add selected in option tag where it needed.
<p>Select Options From Below to find property</p>
<form action="#" name="form" id="form" method="post">
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='availability'><option value=''>- Availability?--</option>";
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['availability']) and $row['availability'] == $_POST['availability']){
echo "<option value='". $row['availability']. "' selected>" . $row['availability'] ."</option>";
}
else{
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
}
echo "</select>";
?>
<?php
$sql = "SELECT bedrooms FROM properties LIMIT 10";
$result = mysql_query($sql);
echo "<select name='bedrooms'><option value=''>--Please Choose Bedrooms Bedrooms--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['bedrooms']) and $row['bedrooms'] == $_POST['bedrooms']){
echo "<option value='" . $row['bedrooms'] . "' selected> " . $count . "</option>";
}
else{
echo "<option value='" . $row['bedrooms'] . "'> " . $count . "</option>";
}
$count++;
}
echo "</select>";
?>
<?php
$sql = "SELECT sleeps_min FROM properties LIMIT 12";
$result = mysql_query($sql);
echo "<select name='sleeps_min'><option value=''>--Please Choose Guests--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['sleeps_min']) and $row['sleeps_min'] == $_POST['sleeps_min']){
echo "<option value='" . $row['sleeps_min'] . "' selected> " . $count . "</option>";
}
else{
echo "<option value='" . $row['sleeps_min'] . "'> " . $count . "</option>";
}
$count++;
}
echo "</select>";
?>
<input type="submit" name="" value="GO" />
</form>

Categories