Set selected item in combobox to "please select..." - php

I'd like to add "Please select..." as the first option in my combobox. See code below - any suggestions?
Regards,
Peter
<?php
mysql_connect ("localhost", "username", "password");
mysql_select_db('mysqldb');
$sql = "SELECT data_id FROM projects";
$result = mysql_query($sql);
echo "<select name='data_id '>";
$sql[0] = 'Please select...';
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>";
}
echo "</select>";
?>

replace
$sql[0] = 'Please select...';
to this
echo '<option value="" disabled>Please select...</option>';

Why not just replace your $sql[0] = 'Please select...'; line (which by the way does nothing useful whatsoever) by this line:
echo "<option selected disabled>Please select...</option>";
The selected keyword is even optional if this is the first option you specify.

Related

Retrieve value from DB and display in a drop down on edit form in php

This script does not display the DB value in a drop down on the edit form.
<?php
echo "<select name='assign' value=''><option>Select name</option>";
while ($r = mysql_fetch_array($result)) {
$value = $r['name'];
echo "<option value=" . $r['emp_id'] . ">" . $r['name'] . " if ($name=='$value') echo 'selected = 'selected''></option>";
}
echo "</select>";
It does not show any error. How it can write in a correct way.
You can try this :
$echoSting = '<select name="assign"><option value="">Select name</option>'.PHP_EOL;
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
$echoSting .= '<option value="'.$r['emp_id'].'" '.($name==$value ? 'selected' : '').'>'.$r['name'].'</option>'.PHP_EOL;
}
$echoSting .= '</select>'.PHP_EOL;
echo $echoSting;
a side note, try looking into PDO for your database stuff : http://php.net/manual/en/book.pdo.php
Try this:
echo "<select name='assign' value=''><option>Select name</option>";
while($r = mysql_fetch_array($result)) {
$value=$r['name'];
echo "<option value='.$r['emp_id'].'>'.$r['name'].' "; if ($name=='$value') echo "selected = 'selected'";echo">$value</option>";
}
echo "</select>";

HTML form <select> data from database not correctly displayed

<?php
$mysqli = new mysqli("localhost", "root", "", "voorraad");
$result = $mysqli->query("SELECT leverancier from leverancier");
echo "<select id='leverancier' name='leverancier' style='width: 30%', color='black'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['leverancier'] ."'></option>";
}
echo "</select>";
?>
This is the code that I am using, the data is correctly loaded in the <select> menu, but the problem is that it is not visible. Here you can see the problem, the 20 records in the database are loaded, but not visible, I can select them, and save them in the database. But there are not visible.
Change:
echo "<option value='" . $row['leverancier'] ."'></option>";
To
echo "<option value='" . $row['leverancier'] ."'>'" . $row['leverancier'] ."'</option>";
You simply forgot to give the option a name.

HTML form drop down select issue

I've got a login form which I'm trying to simplify. It all worked when you manually inputted your username and password. Now I am wanting a drop down for the username box from the MySql database.
This is the code that I have put into the form and it drops down and shows all the users but when you select it, put the password in and click login it doesn't pass the username.
<?php
mysql_connect('localhost', 'user', 'password.');
mysql_select_db('database');
$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select username='sub1'>";
while ($row = mysql_fetch_array($result)) {
echo"<option value'" . $row['username'] ."'>" . $row['username'] ."</option>"; } echo "</select>";?>
Any Ideas Anyone
Thanks
Tom, always check your generated HTML to investigate errors.
Change your code to:
<?php
mysql_connect('localhost', 'user', 'password.');
mysql_select_db('database');
$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select name='username'>"; // Note name attribute
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>"; // Note `=` sign after value
}
echo "</select>";
?>
replace your code with this:
<?php
mysql_connect('localhost', 'user', 'password.');
mysql_select_db('database');
$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select name='sub1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";
}
echo "</select>";
?>

populate drop down list from mysql database and don't repeat values

I am populating a drop down menu from mysql database. It works well, But I want it not to repeat values. (i.e if some value is N times in database it comes only once in the drop down list)
Here is my code:
<?php
mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");
$sql = "SELECT year FROM data";
$result = mysql_query($sql);
echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['year'] . "'>" . $row['year'] . "</option>";
}
echo "</select>";
?>
Use DISTINCT in your query.
"SELECT DISTINCT year FROM data";
just change Your query. is better
$sql = "SELECT distinct year FROM data";
Another technique:
select year from table group by year
in PHP side you have to do this
$all_data = array();
echo "<select name='year'>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
array_push($all_data,$row["column"]);
}
$all_data = array_unique($all_data);
foreach($all_data as $columns => $values){
print("<option value='$value'>$value</option>");
}
echo "</select>";
Here is a simple trick. Take a boolean array. Which value has not come in list print it in list and which value has come in list already once, set it as true through indexing the boolean array.
Set a condition, if boolean_array[ value ] is not true, then show value in list. Otherwise, don't.
mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");
$sql = "SELECT year FROM data";
$result = mysql_query($sql);
echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
if($bul[$row['year']] != true){
echo "<option value='" . $row['year'] . "'>" . $row['year'] . " </option>";
$bul[$row['year']] = true;
}
}
echo "</select>";
?>

dynamic dropdown filled from mysql data

What i'm trying to do is display a drop down with all field names from mysql database, once the user picks one and submits the form i want to display a second dropdown filled with all the rows from the submitted field name, this is my code so far:
$result = mysql_query("select * from `parts`") or die(mysql_error());
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
echo "<select name='field_names'>";
$i = 0;
while ($i < mysql_num_fields($result)) {
$fieldname = mysql_field_name($result, $i);
echo '<option value="'.$fieldname.'">'.$fieldname.'</option>';
$i++;
}
echo "</select>";
echo "<input type='submit' value='submit'></input>";
echo "</form>";
if($_POST) {
$fields = $_POST['field_names'];
$result1 = mysql_query("select '".$fields."' from `parts`") or die(mysql_error());
echo '<select name="fields">';
while ($row = mysql_fetch_array($result1)) {
echo "<option value=".$row[$fields].">".$row[$fields]."</option>";
}
echo '</select>';
}
Can anyone spot where i'm going wrong, thanks
there is a mistake on the line number 29
$result1 = mysql_query("select '" . $fields . "' from `parts`") or die(mysql_error());
you are using ' instead of `. Do as follows
$result1 = mysql_query("select `" . $fields . "` from `parts`") or die(mysql_error());
Hope your problem is solved.
As it stands now, the second set of selects will be issued OUTSIDE of your </form> tag, so will never get submitted with the rest of the form. At best, you should move the form closing tag to below the POST handler.
here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
echo "</select>";
here username is the column of my table(userregistration)
it works perfectly

Categories