I have to create a form that takes the select options from mySQL database so that all different values in one column are listed. The code that is not working is following:
<form class="form-horizontal" method="get" action="startlist.php">
<select id="selectbasic" name="klass" class="form-control">
<?PHP
$connection = mysqli_connect("link","dbuser","pass","dbname");
mysqli_set_charset($connection,"utf8");
$sql = "SELECT DISTINCT Klass FROM Voistlejad";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_array($connection,$result)) {
echo $row["Klass"];
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
mysqli_close($connection);
?>
<option value='32KK5B'>32KK5B</option><!--This is how it needs to be-->
</select>
<button type='submit' class="btn btn-primary">Move on</button>
</form>
What is wrong?
I think you are mixing up where to use single and double quotes..
Generally, for arrays/objects like $row, you get the value from the array with either no quotes, or single quotes, echo $row['Klass']
while ($row = mysqli_fetch_array($connection,$result)) {
echo $row['Klass']; // single quotes
echo "<option value='". $row['Klass'] ."'>". $row['Klass'] ."</option>";
}
// shows sql error ONLY if there is one to show
printf(" %s\n", mysqli_error($link));
P.S strongly recommend adding code to display MySQLi errors, because stuff happens!
Related
I have a simple table with staff names stored in the column f_operator_name.
I have a drop down menu in a php form with these staff names available for selection. Here is a snippet of the relevant the code:
<?php
echo "<h2>Operator: <select name=f_operator_id></h2>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['operator'];
}
?>
When someone selects the staff name I want it to be stored in a variable. I'm testing the submit form at the bottom which is intended to print out the name that has been selected ( in the variable operator), but it's not printing anything out. Can anyone see any issues?
EDIT *** Here's the updated code after some advice from Barmar with the variable information also, for some reason the echo statement still isn't working:
<?php
$connection = pg_connect("host=10.100.51.42 port=5432 dbname=reportingdb user=rai_gui password=password");
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['f_operator_id'];
}
?>
You can't put <select> inside <h2> and then put the <option>s and </select> outside it. HTML elements have to be nested properly, and <option> has to be inside <select>.
Change it to:
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
And the index in $_POST has to match the name of the <select>, so $_POST['operator'] should be $_POST['f_operator_id'].
I have a dynamic combobox and I have my Fetch button. When a user selects a value from combobox and clicks fetch button, all the other related values are displayed in a textbox for the user to edit and update records. And that works fine.
<form id="form1" method="post" action="edit.php">
<select name="ID" id="select">
<?php display_Id();?>
</select>
<input type="submit" name="Fetch" id="Fetch" value="Fetch" />
</form>
function display_Id() {
$query = "SELECT * FROM Flight";
$result = mysql_query($query) or die("Failed to fetch records");
confirm_query($result);
while($rows = mysql_fetch_array($result)) {
$flightNum = $rows['FlightNo'];
echo "<option value=\"$flightNum\" ";
echo " selected";
echo "> $flightNum </option>";
}
}
The problem is in the Fetch button. When user clicks Fetch, other values are displaying but the selected value from combobox is refreshing. How to make the values remain selected even after pressing the Fetch button?
Your question is incomplete in the sense, that you don't have your dislay_Id() code shown here. However, Generally speaking, you should add selected after <option value="something" programmatically,
Code should be something like this:
function displayId(){
if($value[x]== $currentValue) {
echo "<option value='$value[x]' selected>sth</option>";
}
else
{
echo "<option value='$value[x]'>sth</option>";
}
}
EDIT:: Your code adds a "selected" to each of the values, you must only add a "selected" to a current value.
So, your code must look like this:
echo "<option value=\"$flightNum\" ";
if($_POST['ID'] == $flightNum)
{
echo " selected";
}
echo "> $flightNum </option>";
while($rows = mysql_fetch_array($result))
{
$flightNum = $rows['FlightNo'];
echo "<option value=\"$flightNum\" ";
if($_POST['ID'] == $flightNum)
{
echo " selected";
}
echo "> $flightNum </option>";
}
I'm trying to add some space after the drop down but it's not working...
PHP
<?php
$connect = mysql_connect("server","username","") or die("Error connecting to MYSQL");
mysql_select_db("database") or die("Error connecting to database");
$result = mysql_query("SELECT * FROM table ORDER BY column ASC");
"<select name='drpdwn'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value=" .$row['col1'] . ">".$row['col1'] ."</option>";
}
echo "<br/>";
echo "";
?>
<br/>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()"> <input type="button" id="cls" class="cls" value="Clear">
This is the code i added <br> two times and blank space once but it doesn't work..
You never actually close the <select> (with a </select>).
- echo "<br/>";
+ echo "</select><br/>";
http://jsfiddle.net/LTXrd/
vs. http://jsfiddle.net/LTXrd/1/
I do find it a bit odd that it has that effect, but I guess that's good motivation to properly format HTML.
Some problems I can see:
You are not closing the select element
input elements are inline elements so if you want to add space around them using for example css margins, you should change that to something like: input { display: inline-block; }
In my drop down list, each row has 3 fields from a db. I somehow need to pass these to another form where I will reference them in another SQL statement.
I'm not sure how to pass them. Any help would be really appreciated.
<form action='obtainaprice.php' method='POST'>
<?php
echo "<select name='makes' id='searchtext'>\n";
$query1 = "SELECT DISTINCT make, type, model FROM device ORDER BY make, type, model";
$result1 = mysql_query($query1)
or die ("Couldn't execute query.");
while ($row1 = mysql_fetch_array($result1))
{
$i=0;
echo "<option value= \"" . $i . "\">" . $row1['make'] . " " . $row1['type'] . " " . $row1['model'] . "</option>";
$i++;
}
echo "</select>\n";
?>
<input type="image" id="buttons" alt="Search" img src="images/button.png">
</form>
In this case you would make the value of the options the row's id, which you need to select:
"SELECT DISTINCT id, make, type, model FROM device ORDER BY make, type, model"
assuming your primary index is called id...
<option value="<?php echo $row['id']; ?>">
Then on your next page you would run the next query based on the post value for makes...
"SELECT id, make, type, model FROM device WHERE id = ".$_POST['makes']
of course, you need to sanitize this. You should switch to mysqli or PDO.
Side note - you should break out of php for writing html. It's just cleaner. Run your query first so you don't die in the middle of a select box. I don't condone use of mysql_ functions but for the sake of example here is what I mean...
<?php
$query1 = "SELECT DISTINCT id, make, type, model FROM device ORDER BY make, type, model";
$result1 = mysql_query($query1)
or die ("Couldn't execute query.");
?>
<form action='obtainaprice.php' method='POST'>
<select name='makes' id='searchtext'>
<?php while ($row1 = mysql_fetch_array($result1)): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row1['make']; ?> <?php echo $row1['type']; ?> <?php echo $row1['model']; ?></option>
<?php endwhile; ?>
</select>
<input type="image" id="buttons" alt="Search" img src="images/button.png">
</form>
Your option element can only pass back 1 thing (a string), so you'd have to parse it first to get the make, type and model back out of it before running the next SQL statement.
Why don't you have 3 select elements? 1 for each of make, type, model.
What is the next SQL statement you want to use the values in?
i have a drop down list which is populated dynamically from database table i.e
<form method=post action='dropdown.php'>
<?php
$query="SELECT DISTINCT style FROM style";
$result = mysql_query ($query);
echo "<select name=style[] size=4 value='' multiple>Choose Style</option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[style]>$nt[style]</option>";
}
echo "</select>";// Closing of list box
mysql_close($conn);?>
<input type=submit> </form>
When i post the form to another page the value of dropdown after space is not shown i.e if i select (Micro Pave) from drop down list then it will only show Micro. my php code is
$style=$_POST['style'];
if( is_array($style)){
while (list ($key, $val) = each ($style)) {
echo $val."<br/>";
}
}
echo "<option value='$nt[style]'>$nt[style]</option>";
Missed your single quotes! :)
<?php
$query = "SELECT DISTINCT style FROM style";
$result = mysql_query($query);
echo 'Choose Style
<select name="style[]" size="4" multiple="multiple">';
while ($nt = mysql_fetch_array($result)) {
echo '<option value="'.$nt['style'].'">'.$nt['style'].'</option>';
}
echo '</select>';
mysql_close($conn);
?>
missing quotes in the option value:
$ntstyle = htmlspecialchars($nt['style'], ENT_QUOTES);
echo "<option value='{$ntstyle}'>".$ntstyle."</option>";
Well for a start any attributes should be contained in double quotes.
echo "<option value=$nt[style]>$nt[style]</option>";
Should be
echo '<option value="'.$nt[style].'">'.$nt[style].'</option>';
So many things wrong unfortunately... :(
Start with "{$nt['style']}" instead of just "$nt[style]".
(or better yet: echo 'Constant', $arr['item'];)
Fix this:
echo "<select name=style[] size=4 value='' multiple>**Choose Style</option>**;
It's a wrong place for a label and the tag is not open!
Also add "" to your params="" in HTML.