Create a list from SQL database using HTML/PHP - php

Im trying to create a drop down list/menu of users from my SQL database table. Could anyone help me with the code please? The code below is just for a normal list with options 1 & 2. How do i make the list retrieve all the items in a specific table using html/php/sql. Sorry im not very experienced with this, thanks in advance.
<select name="user" id="user">
<option value="a" selected="selected">1</option>
<option value="b">2</option>
</select>

there are several ways but i'll show mine
First, take the data you want to retrieve from query:
$query = mysql_query("SELECT name, id FROM users");
Then echo a select followed by a while cycle to iterate the various options:
<?php
echo "<select name='user'>";
while ($temp = mysql_fetch_assoc($query) {
echo "<option value='".$temp['id']."'>".$temp['name']."</option>";
}
echo "</select>";
?>
The result should be:
<select name="user">
<option value='1'>User name 1</option>
<option value='2'>User name 2</option>
</select>
Hope this was useful for you :)

PHP Dynamic Drop-down

Related

Select value from the dropdown in which the data are taken from database

I need to post the value from the drop down, in which the dropdown contains items which are retrieved from the database. To display the item of the table I use echo in the option. But then, I need to get that value of item selected to be updated in the database. As be seen below, I've tried the code which (surely) will not work. How is it possible to get the the value of selected item?
Your suggestion will be appreciated.
Thank you.
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option name="user_name"><?php echo $hasil['user_name'] ?></option>
<option name="user_name" value="<?php echo $hasil['user_name'] ?>" hidden></option><?php }?>
</select>
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option><?php echo $hasil['user_name'] ?></option>
<?php } ?>
</select>
This code solves your problem, Check at your side and let me know in case of any issue.
We need to give name only once in select tag and no need to add a extra option only for value.
You must fill your dropdown from base table after that you must check value or id from second table that stored in database. If id or value is equal you must set selected='selected' in your option element
Hope it is help you to resolve you problem
" >
" >
" >
*

Populating html select box by data from mysql database depending on another select box

I've been struggling with this problem for a while. I have read tons of posts and tutorials who do something simmilar but cant really get it working for me.
I need to make php form which contains 2 html select boxes : school and class.
I can easly get it to show all schools from my database but cant get my head around how to depending on selected school show classes from that school.
I know this question is more time consuming then others might be, but I will be very happy if you chose to help me!
Kristaps
Since, you have not provided database structure of your database. I
assumed from myself and wrote query accordingly. Change your query as
per your need.
<div>
<select class='Schools' name='SchoolName'>
<option value="">Select School</option>
<?
$Query="SELECT SchoolIDColumnName,SchoolNameColumn FROM SchoolsTable";
foreach($pdo->query($Query) as $row)
{?>
<option value="<?echo {$row['SchoolIDColumnName']};?>"><?echo {$row['SchoolNameColumn']};?></option>
<?}?>
</select>
</div>
<div class='ShowClassName'>
<select>
<option value="">Select Class Name</option>
</select>
</div>
<script>
$('.Schools').change(function(){
var SchoolID=$('.Schools').val();
$.ajax({url:"Ajax-ShowClassName.php?SchoolID="+SchoolID,cache:false,success:function(result){
$('.ShowClassName').html(result);
}});
});
</script>
Ajax-ShowClassName.php
<?
$SchoolID=$_GET['SchoolID'];
$QueryClassName="SELECT * FROM ClassTable WHERE SchoolIDColumnName='$SchoolID'";
?>
<select name='ClassName'>
<?
foreach($pdo->query($QueryClassName) as $row)
{?>
<option value="<?echo {$row['ClassIDColumnName']};?>"><?echo {$row['ClassColumnName']};?></option>
<?}?>
</select>

What is my error when creating 4th AJAX populated dropdown using mySQL and PHP?

index.php
Hello community. I am using a dropdown list populated from mySQL using AJAX. I got this script as my base from a website that a member of this community suggested and you can see the demo here
In the demo there are 3 dropdowns and I am trying to make it 4, without luck as you can see in the screenshot here
I can not get the 2010 and pass it to the year so the 4th dropdown to show the selected rows, so it is shown as undefined.
One note though, if from the file findEngine.php remove the AND model_year='$yearId' and leave it with only 1 AND, it shows all the rows found for AUDI, not with the selected model or year of course.
I added the source codes below and the index.php to an external service because of it's length.
Any help is appreciate for this, I guess it is a beginners problem but I can not see it..
index.php
http://codepad.org/k3n7HJ4G
findModel.php
<?
$make=$_GET['make'];
mysql
$query="SELECT distinct model_name FROM cars WHERE model_make_id='$make' order by model_name asc";
$result=mysql_query($query);
?>
<select name="model" onchange="getyear('<?=$make?>',this.value)">
<option>Select Model</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['model_name']?>><?=$row['model_name']?></option>
<? } ?>
</select>
findYear.php
<? $makeId=$_GET['make'];
$modelId=$_GET['model'];
echo $modelId;
mysql
$query="SELECT distinct model_year FROM cars WHERE model_make_id='$makeId' AND model_name='$modelId' order by model_year asc";
$result=mysql_query($query);
?>
<select name="year" onchange="getEngine('<?=$makeId?>',this.value)">
<option>Select year</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$modelId?>><?=$row['model_year']?></option>
<? } ?>
</select>
findEngine.php
<? $makeId=$_GET['make'];
$modelId=$_GET['model'];
$yearId=$_GET['year'];
echo $yearId;
mysql
$query="SELECT distinct model_trim FROM cars WHERE model_make_id='$makeId' AND model_name='$modelId' AND model_year='$yearId' order by model_trim asc";
$result=mysql_query($query);
?>
<select name="engine">
<option>Select year</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['model_trim']?></option>
<? } ?>
</select>
As per the definition of getEngine function, it takes 3 parameters but you are passing only 2.
Change below line in findEngine.php
<select name="year" onchange="getEngine('<?=$makeId?>',this.value)">
To,
<select name="year" onchange="getEngine('<?=$makeId?>', '<?=$modelId?>', this.value)">
Also, avoid using mysql extension as it is deprecated in PHP 5.5.0. Read http://www.php.net/manual/en/intro.mysql.php for the details and alternatives.

JQM selectmenu first option blank when echoing php-based select items

I'm populating a jQuery Mobile select menu in a form from mySQL database using this php:
$OTsql = "SELECT * FROM OrderTypes";
$OTresult = mysql_query($OTsql) or die (mysql_error());
while ($row = mysql_fetch_array($OTresult))
{
$OTid=$row["id_ot"];
$OTname=$row["orderTypes"];
$OToptions.="<OPTION VALUE=\"$OTid\">".$OTname;
}
the html is this:
<label>Order Type:</label><br>
<select name="orderType" id="orderType" data-overlay-theme="e" data-native-menu="false" data-icon="arrow-d">
<option id="0" value=""> Select Order Type </option>
<option>
<? echo $OToptions ?>
</option>
</select>
While I can click on the selectmenu and see the options listed from the database, it shows a blank (no text listed on the selectmenu button itself) and when I click the menu the top spot is a blank placeholder.
I've tried a variety of jQuery options like variations of this which I've found here at SO:
$(document).bind('mobileinit',function(){
$.mobile.selectmenu.prototype.options.hidePlaceholderMenuItems = true;
});
The bigger problem (I could possibly live without there being the first option listed by default on the button or "Select Order Type" as the item listed on the button--which would be ideal), but when I go to save a pre-existing form, it changes whatever had been selected there to a blank in the database record.
Any insight would be greatly appreciated. I'm not even sure if I'm going about this the right way.
Can you try this,
$OToptions.="<option value=\"$OTid\">".$OTname."</option>";
<select name="orderType" id="orderType" data-overlay-theme="e" data-native-menu="false" data-icon="arrow-d">
<option id="0" value=""> Select Order Type </option>
<?php while ($row = mysql_fetch_array($OTresult))
{
$OTid=$row["id_ot"];
$OTname=$row["orderTypes"];
echo "<option value=\"$OTid\">".$OTname."</option>";
}
?>
</select>
You are constructing your options wrong. Try this:
$OTsql = "SELECT * FROM OrderTypes";
$OTresult = mysql_query($OTsql) or die (mysql_error());
while ($row = mysql_fetch_array($OTresult))
{
$OTid=$row["id_ot"];
$OTname=$row["orderTypes"];
$OToptions.="<option value=\"$OTid\">".$OTname ."</option>";
}
HTML
<label>Order Type:</label><br>
<select name="orderType" id="orderType" data-overlay-theme="e" data-native-menu="false" data-icon="arrow-d">
<option id="0" value=""> Select Order Type </option>
<?php echo $OToptions ?>
</select>
</label>
BTW, mysql_* functions are deprecated. Change to mysqli_* or _PDO functions
As I can see from your code, I expect the generated output to be:
...
<option>
<option>....</option>
...
<option>....</option>
</option>
...
which is wrong. Try to remove the outer tags by modifying your html markup:
<select name="orderType" id="orderType" data-overlay-theme="e" data-native-menu="false" data-icon="arrow-d">
<option id="0" value=""> Select Order Type </option>
<? echo $OToptions ?>
</select>
Additionally, just for an improved style, I suggest you to reformat the PHP part in a more readable way, e.g.:
$OToptions .= '<option value="'.$OTid.'">'.$OTname;

Can I pass Ajax var result to PHP code?

Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.
Greetings.
I am coding a basic form that uses a SELECT list I populate from my database.
However, my user needs the form to be dynamic and wants to be able to select either MasterTable1 or MasterTable2 or MasterTable3...
Instead of hardcoding the table name for the database query that populates the SELECT list, I attempted to implement a basic Ajax action (used example from w3schools)...and that's when I lost my sanity...
I can output <div id='txtHint'></div> in my page and it shows the correct table name that was picked.
But how do I pass the correct table name to my query that will populate my SELECT list???
I tried
<select name="DBFilename" id="DBFilename" size="0">
<option value="">Select Filename</option>
<?php
$sql="select distinct filename from "."<div id='txtHint'></div>";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option>
<?php } ?>
</select>
But to no avail. This is confusing since I can do this...
...
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gettable.php?q="+str,true);
xmlhttp.send();
}
</script></head>
<body><form><select name="SrcTbl" id="SrcTbl" size="0" onchange="showTable(this.value)">
<option value="">Select Data Table</option>
<option value=""> </option>
<option value="MasterTable1">up to 31 days old</option>
<option value="MasterTable2">62 days old</option>
</select>
</form><br /><div id="txtHint"><select name="tabList"><option></option></select> </div>
</body></html>
And the name of my table will be displayed in the SELECT list 'tablist'.
How do I pass the correct table name to my query that will populate my SELECT list? Thanks!!
(Pastebin =>form code)
m8, ajax is mainly used for user experience and populating a select list is so easy mode that it shouldnt be bothered with ajax in the first place!
You should use ajax if you want to use some methods on user submitted data and create an illusion of seamless data exchange between the server and the client, and based on the return results render a corresponding view or an element of the view.
unless you load every element of the view with ajax, you should populate your html with php from the start!
<select name="DBFilename" id="DBFilename" size="whatever since style belongs to css">
<option selected="selected">Select Filename</option>
<?php
$sql="SELECT DISTINCT filename from 'wherever'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename'];?>
</option>
<?php } ?>
</select>
Create a separate php script which returns a list of select options -> values depending on the table name given to it. You must remember to protect against sql injection. Then use ajax to retrieve the list and insert it into the select.

Categories