whitespace in select dropdown, pulled from mysql - php

I'm using this to populate a dropdown:
$result = mysql_query("SELECT user from `users` order by user asc") or die(mysql_error());
echo '<select name="user" class="user">';
while ($row = mysql_fetch_array($result)) {
echo "<option value=".$row['user'].">".$row['user']."</option>";
}
echo '</select>';
But the source is this:
<select name="user" class="user"><option value=joe bloggs>joe bloggs</option>
So when i do this:
var user = $('.user').val();
It only sees "joe" not "joe bloggs"?? Any ideas

$result = mysql_query("SELECT user from `users` order by user asc") or die(mysql_error());
echo '<select name="user" class="user">';
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.htmlspecialchars($row['user']).'">'.htmlspecialchars($row['user']).'</option>';
}
echo '</select>';
Corrected for you :)

Wrap the value in quotes:
echo "<option value=\"".$row['user']."\">".$row['user']."</option>";
Also, make sure to htmlspecialchars() in order to escape possible quotes in the name. E.g.,
$user = htmlspecialchars($row['user']);
printf('<option value="%s">%s</option>', $user, $user);

inside the while loop try:
echo "<option value=\"".$row['user']."\">".$row['user']."</option>";
basically adding a quote to the value inside the option

Use quotes " around attribute values..
your html should look like
<option value="joe bloggs">joe bloggs</option>

Related

How to add an additional option to an option form which is filled with a db query

How can I add an extra <option> with a value of all to:
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
$query = "SELECT name,id,path FROM categories ORDER BY ID DESC LIMIT 0,6";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="categories">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['path']."'>'".$row['name']."'</option>";
}
?>
</select>
I would be grateful for any suggestions
Edit
echo "<th>Tierart";
$query = $pdo->query("select sp_term from species");
//Abfrage der Tabelle Tierart
echo '<select name="sp_term">';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="all">all</option>';
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';
Since you're populating the values of option in a select tag section with a database query doing something as follows would do the trick.
echo '<select name="sp_term">';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';
However, when you want to add an additional item like All as an option, place an explicit option value before the while/after the while depending on your requirement and you'll have the select > option with the new All option added to the options from the database.
echo '<select name="sp_term">';
echo '<option value="all">all</option>';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';

Fetch data from MSSQL in dropdown using php

In this code i am trying to fetch city names into the html dropdown. Kindly corrct me if i am wrong anywhere, it give me an error
<?php
$query = Run("select city_name from City");
echo "<select name="city-name" style="width: 210px;">";
while ($row = mssql_num_rows($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
?>
use mysqli_fetch_array($query) insted of mssql_num_rows($query)
try this one
echo "<select>";
while ($row =mysqli_fetch_array($query))
{
echo "<option value='".$row['city_name']."'>".$row['city_name']."</option>";
}
echo "</select>";
mssql_num_rows returns the number of rows in the result set, it doesn't iterate and return individual rows. Try using mssql_fetch_object instead.
If you start a string with " then you have to escape all occurrences of " inside your string or use '.
For example:
echo "<select name=\"city-name\" style=\"width: 210px;\">";
or
echo '<select name="city-name" style="width: 210px;">';
so you don't accidently close the string.
Also like the others pointed out, you have to use
mysqli_fetch_array($query).
Use this
$query = Run("select city_name from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_fetch_object($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
Use This
$query = Run("select `city_name` from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_num_rows($query))
{
echo "<option value='.$row->city_name.'>".$row->city_name."</option>";
}
echo "</select>";

How to get selected value from dropdown list after submitting form in php

I would like to remain my drop down value which I select for submitting after posting the form. My form posts to the same page.
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
echo "Country: <select name='country' value=''>";
while($r = mysql_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id.">".$cname."</option>";
}
echo "</select>"; ?>
Remove your current echo inside the loop and replace it with the following:
if($_POST["country"]==$id)
echo "<option value='".$id."' selected='selected'>".$cname."</option>";
else
echo "<option value='".$id."' >".$cname."</option>";
This will check if the current option being displayed is the one that was submitted and it will select it in that case.
If I understand what you are looking for correctly you need to use the $_POST value of your select to set the selected item...
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
$country = '';
echo "Country: <select name='country'>";
while($r = mysqli_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id;
echo ($_POST["country"]==$id) ? ' selected="SELECTED"' : '';
echo ">".$cname."</option>";
}
echo "</select>"; ?>
Setting selected="SELECTED" for the $id that matches $_POST['country'] will make it the selected item in your dropdown.
And, get rid of mysql* functions and use mysqli* functions instead...

Javascript Dropdown List

I need to populate a drop down list from a Mysql table
my table will be like below
Symbol values
G.n0000 10
P.n0000 20
W.n0000 40
G.n0000 50
P.n0000 60
I need to appear the Symbol values in the drop down box, but only the distinct values
eg: drop down list should show only G.n0000,P.n0000,W.n0000
What I have tried is below but it is not working
<?php
$conn = new mysqli('localhost', 'root', '', 'offlinesurv')
or die ('Cannot connect to db');
$result = $conn->query("select distinct symbol from tab");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
unset($id);
$id = $row['symbol'];
echo '<option value="'.$id.'"></option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>
Values do not show in select options.
It's been a while since I did any PHP so I can't recall the syntax, but with straight html you need to put:
<option>Text You Want To See</option>
Value is an optional identifier for each option
So you could have
<option value='id'><Text you want to see</option>
EDIT
while ($row = $result->fetch_assoc()) {
unset($id);
$id = $row['symbol'];
$value = $row['value'];
echo "<option value= '$value' >$id</option>";
}

How to prevent echoing same value twice in php

So I have this drop down list in my form which pull "tags" from database as value for drop down options:
<select name="cartags">
<?php $result = mysql_query("SELECT * FROM Products WHERE ID > '0'");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['Tag']; echo "\""; echo ">"; echo $row['Tag']; echo "</option>";
}
?>
</select>
What is my problem? My problem is that I am adding a lot of products into my databas and my code make dropdown list with tags for all this producst even if they have same tag. So what I need is solution how to prevent that same tag appear twice in my drop down.
I am pretty new to PHP and this is my first question here so I really hope that I explained my problem well.
Thanks in advance!
What is the purpose of WHERE ID > '0'? If ID is an auto-increment then it will always be positive. If not, it should be.
Why are you using mysql_fetch_array and then only using the associative keys? You should use mysql_fetch_assoc instead.
Why are you using a new echo every time you want to output a variable? Just concatenate.
Why are you setting the same string in value as the option's text? Without a value, it defaults to the text anyway.
Why are you not using backticks around your column and table names?
Try this instead:
<select name="cartags">
<?php
$result = mysql_query("SELECT DISTINCT `Tag` FROM `Products`");
while(list($tag) = mysql_fetch_row($result)) {
echo "<option>".$tag."</option>";
}
?>
</select>
Try this
<select name="cartags">
<?php $result = mysql_query("SELECT Tag, COUNT(Tag) tg Products WHERE ID > '0' GROUP BY Tag HAVING COUNT(Tag)>0 ORDER BY tg DESC");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['tg']; echo "\""; echo ">"; echo $row['tg']; echo " </option>";
}
?>
</select>
It will also display the top tags that have the most first.

Categories