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.
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 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!
I have a problem right now [PHP]. I have a dropdown and its loading my database for the first page, when I proceed to the next page it also have a dropdown where its also loading the my database and also I can get the value of my dropdown in the first page using an echo only.
This is the scenario:
I choose in the dropdown first page the "Letter A" and when I click the button it will proceed to the next page. The dropdown in the second page loaded the items in the database but instead of "-select-" is the first index in the dropdown I want is "Letter A" will be the first index.
This is my code in first page for drowdown:
<select name="id">
<option value="" >- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
and this is my code in second page for dropdown:
if ($get_ID != "")
{
echo "<br/>";
echo $get_ID;
//echo "show()";
}
else
{
echo "No Network Selected";
echo "<br/>";
//echo "hide()";
}
?>
<option value="">- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
Thanks in advance!
On your second page, you need to check whether the value is the same as the one you received from the first page:
echo "<option value='".$row1[fldNetname]."' " . (($row1[fldNetname] == $get_id)?"selected":"") . ">".$row1[fldNetname]."</option>";
if $get_ID, is actually your $_GET['id'] value, then just do...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."'";
if($row1['fldNetname']==$get_ID){echo "selected='selected'";}
echo ">".$row1['fldNetname']."</option>";
}
Or as one line...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."' " . (($row1['fldNetname'] == $get_ID)?"selected='selected'":"") . ">".$row1'[fldNetname']."</option>"
}
Have you tried?
changing (on the second page)
<option value="">- select -</option>
to
<option value="<?php echo $yourVar; ?>"><?php echo $yourVar; ?></option>
Then in the while loop, skip the value selected to prevent a duplicate choice.
Something like:
while ($row1 = mysql_fetch_array($q))
{
if($yourVar != $row1[fldNetname]){
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
}
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 am using this code in my form to create a drop down menu. (the list of options loads corrects from my sql database). Once the user hits submit, I should be able to retrieve the value selected with $_POST['field'].
<form action="page2.php" method="post" name="form" id="form">
<?php
$query = sprintf("SELECT domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo "<select name=domaine value=''>Domain </option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[id]>$nt[domaine]</option>";
}
echo "</select>";
?>
...
On the second page, I use this code:
$domaine = strip_tags(substr($_POST['domaine'],0,32));
echo "You selected $domaine";
But I get nothing a blank value, what am I doing wrong?
Thanks!
In your query you didn't selected the id, only the domaine. Change it to be like this:
<form action="page2.php" method="post" name="form" id="form">
<?php
$query = sprintf("SELECT id, domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo '<select name="domaine">';
while($nt=mysql_fetch_array($result)){
echo '<option value="$nt[id]">$nt[domaine]</option>';
}
echo "</select>";
?>
This line is probably incorrect...
echo "<select name=domaine value=''>Domain </option>";
Should it be
echo "<select name=domaine value=''>";
You should also note that if none of the options are selected, then you won't get a value back. To ensure you get a value back, select one of them (eg the first one) by default, by adding selected="selected" to it...
I'd also recommend quoting values a little more clearly.
For the sake of completeness...
<?php
$query = sprintf("SELECT domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo '<select name="domaine" value="">';
$isfirst = true;
while ($nt=mysql_fetch_array($result)) {
echo '<option value="'.$nt[id].'"';
if ($isfirst)
echo ' selected="selected"';
echo '>'.$nt[domaine].'</option>';
$isfirst = false;
}
echo '</select>';
?>