Selected Value for MySQL generated Drop Down List - php

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>";

Related

How to echo selected option value. Not select name

<select name= "NEE_category">
<?php
$sql = mysqli_query($dbConn, "SELECT catDesc FROM NEE_category");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"NEE_category\">" . $row['catDesc'] . "</option>";
}
?>
</select>
I want to echo the selected option in PHP however using this:
$NEE_category = isset($_REQUEST['NEE_category']) ? $_REQUEST['NEE_category'] : null;
echo "\t<p>Event Category: $NEE_category</p>\n";
Returns: Event Category: NEE_category
How do i return the selected option value and not the name?
You're setting the value to "NEE_category" instead of the values you're pulling from your database.
echo "<option value=\"". $row['catDesc'] ."\">". $row['catDesc'] ."</option>";

HTML/PHP Form Select Option with link to MySQL

I followed a previous question on here to get a drop down with link to MySQL. It works without the form but with the form, all the room_id's just scatter across one line and don't go in a drop down box. Any ideas on how to fix it? Thank you
//Creates a form for room_id
echo "<form action=''>";
echo "<select name='room_id'>";
//Creates drop down box to show the current rooms vacant
$sql = "SELECT * FROM room";
$sql.= " WHERE room_vacant = 1";
$stmt = $dbh->query($sql);
echo "<select name='room_id'>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['room_id'] . "'>" . $row['room_id'] . "</option>";
} //Closes drop down box
echo "</select>";
//Submit button
echo "<input type='submit' value='Submit'>";
//Closes form
echo "</form>";
The reason is that you are echoing your select twice. Remove one.
//Creates a form for room_id
echo "<form action=''>";
echo "<select name='room_id'>";
//Creates drop down box to show the current rooms vacant
$sql = "SELECT * FROM room";
$sql.= " WHERE room_vacant = 1";
$stmt = $dbh->query($sql);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['room_id'] . "'>" . $row['room_id'] . "</option>";
} //Closes drop down box
echo "</select>";
//Submit button
echo "<input type='submit' value='Submit'>";
//Closes form
echo "</form>";

Set select option using php when reloading the same page

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.

How to use a select option value as a PHP variable in the next query?

I have the following code which provides a drop down list of all the rows in that specific table, this works fine. The code is below:
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo "<select>";
echo "<option value=''>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
I now want a second drop down list that is determined by what ever is selected in the one above based on the ID. So, I want something like:
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID=ID_FROM_QUERY_ABOVE");
echo "<select>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
I basically want to get the ID from the first drop down to provide the results in the second dropdown. Can this be done?
You can't "only" do this with Ajax, but you should do it with Ajax.
PHP way (not suggested, and untested). Basically use isset and if it is, more will be added to the form. The POST from the select, is the select name. So change the plain select tag which I did in the example below. This also requires them to submit it.
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo '<form id="project_form" method="post">';
echo "<select id='select_your_project' name = 'select_your_project'>";
echo "<option value=''>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
if(isset($_POST['select_your_project'])){
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID='".$_POST['select_your_project']."'");
echo "<select id='select_your_album' name = 'select_your_album'>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result2))
{
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
}
echo '<input type="submit" value="Submit">';
echo '</form>';
if(isset($_POST['select_your_album'])){
//do form submitted stuff here
}
Ajax way (two separate files, untested but gives you the idea)
//Main page (view) START
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
//this will trigger automatically when they change the first select box
$('#select_your_project').on('change', function(event){
if($(this).val() == 'select_your_project'){
$("#ajax_reply_div").empty()
}else{
var values = $(this).serialize();
$.ajax({
url: "php_data_file.php",
type: "post",
data: values,
success: function(data){
$("#ajax_reply_div").empty().append(data);
},
error:function(){
$("#ajax_reply_div").empty().append('something went wrong');
}
});
}
});
</script>
<form id="id_of_form">
<?php
echo "<select id='select_your_project' name='select_your_project'>";
echo "<option value='select_your_project'>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
?>
</select>
<div id="ajax_reply_div">
</div>
<input type="submit" value="Submit">
</form>
//Main page (view) END
//php_data_file.php START
if(isset($_POST['select_your_project'])){
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID='".$_POST['select_your_project']."'");
//as a note it is better to only send an array back then build the HTML with jQuery, but this way is easier if you are new to jQuery/Ajax
echo "<select id='select_your_album' name = 'select_your_album'>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result2)){
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
}
//php_data_file.php END

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