I'm currently outputting a select menu using the following code;
while($row = mysqli_fetch_array($result) ) {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
I'm wondering how I can make the one that is selected in the database appear as selected, seeings as the options are dynamically drawn and added?
Edit: I still want the other options that aren't selected to appear in the list so it can be changed.
You try this way:
while($row = mysqli_fetch_array($result) ) {
$selected=$row['id']==$current_id? "selected": ""; //$current_id is which you want to selected..
echo '<option value="'.$row['id'].'" '. $selected.' >'.$row['title'].'</option>';
}
Make an if condition and then use selected Attribute
while($row = mysqli_fetch_array($result) ) {
if (your condition) {
echo '<option value="'.$row['id'].'" selected>'.$row['title'].'</option>';
} else {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
}
Related
I need to get the values of the selected select
like the orderno of the changed row
The table looks like this
if I change order no 1002 into sold
$query = mysql_query("select * from orders");
while($row = mysql_fetch_assoc($query))
{
echo '<tr>';
echo '<td>'.$row['orderno'].'</td>';
echo '<td>'.$row['total'].'</td>';
echo '<td>';
if($row['status'] == "sold")
{
echo '<select name = "status[]">';
echo '<option value = "sold" selected>Sold</option>';
echo '<option value = "cancelled">Cancelled</option>';
echo '</select>';
}
else
{
echo '<select name = "status[]">';
echo '<option value = "sold">Sold</option>';
echo '<option value = "cancelled" selected>Cancelled</option>';
echo '</select>';
}
echo '</td>
echo </tr>';
}
if(isset($_POST['status']))
{
//sample only
echo "You change orderno ".$orderno_here." with the
total of ".$total_of_order." into ".$selected_status_of_orderno_here;
}
There are many records in the table and the selects are the same
how do I get the values of the selected select
Following your question, whatever i understood, according to that i am supposing you are getting data from form like tags in your page. If that is the case, you know that which value is changed to be reflected in db. So you can get that selected select from this.
you can check the change through this code:
if(isset($_POST['element'])){
//if this block executed, the <element> named element is changed
}
ignore this answer if this is not the case you are using in your code..
I've set up a php form that registers a project to our database, it has a drop down that populates from our customer/supplier databases.
I've also set up a function to edit these projects, the problem I have is that when I go to my edit page it just displays the customer/supplier name and not in the drop down but a value box - is there a way to have the edit page display the dropdown but also be selected on the original supplier/customer?
Register project page
<?php
$result = mysql_query('SELECT name FROM customers ORDER BY name ASC');
echo '<select name='client'>';
while($row = mysql_fetch_assoc($result))
{ `
echo '<option value = ''.$row[name].''>'.$row[name].'</option>';
}`
echo '</select>';
?>
Edit page
<input type='text' name='client' value='<?php echo $client; ?>'/>
I tried a few tutorials and code tweaks but kept getting errors. I am aware of my sql injection problem, at the moment this site is internal.
Any help would be appreciated.
thanks
instead of $row[name] you should use $row['name']
$client= "<select name='client'>"; // you had error here also.
while($row = mysql_fetch_assoc($result))
{
$client.= "<option value = '".$row['name']."'>'".$row['name'].'</option>';
}
$client.= '</select>';
now echo $client to get dropdown.no need of constructing separate select tag now.
for selected use like this:
$client1= "<select name='client'>";
while($row = mysql_fetch_assoc($result))
{
if($row['name'] == $clientValue){
$client.= "<option selected='selected' value = '".$row['name']."'>'".$row['name'].'</option>';
}else{
$client1.= "<option value = '".$row['name']."'>'".$row['name'].'</option>';
}
}
$client1.= '</select>';
on echo of $client1 you will get selected based on the value $clientValue which you have to pass.
On your edit page:
<?php
$result = mysql_query('SELECT name FROM customers ORDER BY name ASC');
echo "<select name=\"customer\">";
while($row = mysql_fetch_assoc($result))
{
if ($row['name'] == $client)
{
echo "<option selected value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
}
else
{
echo "<option value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
}
}
echo "</select>";
?>
I also suggest that you change the old extension for mysql. I can't see no SQL Injection problem for now, but you should take care of it even if it is internal, because, from different reasons you will forget to sanitize it later. If you are writing it, then write it correctly.
Now for the problem, you are not using the quotes correctly, hence the errors. Do not use the same type of quotes, but change them, like so:
echo '<select name="client">';
Or if you use double quotes for concatenation, use single inside.
In case you have to use the same, escape them with \
For starters, you have a syntax error here:
echo '<select name='client'>';
(There are probably more quoting errors throughout the code, but I digress...)
As for using a drop-down, what you're looking for is the selected attribute. When you're building the page elements to display the form on the "edit" page, presumably you have the values that you're looking to display. When your loop finds an element which matches the value, select it:
while($row = mysql_fetch_assoc($result))
{
if ($knownValue == $row[name]) {
echo '<option selected value = ''.$row["name"].''>'.$row["name"].'</option>';
} else {
echo '<option value = ''.$row["name"].''>'.$row["name"].'</option>';
}
}
I'm trying to create a default value for my dynamic drop down list. After the user selects one of the options, they submit and that value is stored as a variable in the next page that I can access using $_POST['land']
I have created the same dynamic list in the next page and want that 'land' to appear first in the dynamic drop down menu. So far this is just the main code to show the dynamic drop down list. Any help would be appreciated. Thanks!
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
echo "<option value='$place'>$place</option>\n";
}
As far as I understand you want the item chosen on first page to appear as the default item in the second page.
Use this
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
echo "<option value='$place'";
echo ((isset($_POST['land']) && $_POST['land']==$place)?'selected="selected"':'');
echo ">$place</option>\n";
}
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
echo "<option value='$place' ";
if($_POST['land'] == $place) {
echo "selected='selected'";
}
echo ">$place</option>\n";
}
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
$select = (isset($_POST['land']) && $_POST['land'] == $place)?"selected='selected'":"";
echo "<option value='$place' $select >$place</option>\n";
}
I am trying to populate a selection box (dropdown) from a mysql table in php, but I get blank results, Can someone look in my code and tell me what is wrong?
I need to populate the select with the dates available from my sql query, so they show up as seletion options....
<?php
echo JText::_('Please select the date:');
$database= &JFactory::getDBO();
$database->setQuery('SELECT training_id,training,trainingDate FROM training');
$result = $database->loadObjectList();
echo '<select name="whatever">';
foreach ($result as $row) {
echo '<option value="'.$row->training_id.'" />';
}
echo '</select>';
?>
You assign an option value, but you don't provide human-readable option text:
echo '<select name="whatever">';
foreach ($result as $row) {
<!-- here we go -->
echo '<option value="'.$row->training_id.'">'.$row->training.'</option>'; // always close options!!!
}
echo '</select>';
echo '<option value="'.$row->training_id.'">'.$row->trainingDate.'</option>';
If that doesn't work then your $row->training_id isn't set and so you'll need to debug that (e.g. doing a print_r($row) just before that line to see what is inside the $row object)
Try this:
while($row = mysql_fetch_object($result)) {
instead of this:
foreach($result as $row) {
I have read tutorials about how to populate an entire Drop down list with MySQL, but the problem I am running into is that I only want to grab the one from the database and have that be the selected one. So I would have like a drop down with three items (Item1, Item2, Item3) in the database its stored in a column called itemschoice which has a value of 'Item2'. How do I go about getting item2 to be selected when I load the drop down box?
In your <option> element add the selected attribute for the value that is in itemschoice.
Crude example using a made up function to get the choice:
$choice = get_items_choice();
$results = mysqli_query($sql);
echo '<select name="whatever">';
while($row = mysqli_fetch_array($results)) {
if ($row['choice'] === $choice) {
echo '<option value="' . $choice . '" selected="selected" />';
} else {
echo '<option value="' . $choice . '" />';
}
}
echo '</select>';
This is just an example, don't copy & paste this without adding some kind of error verification!