Loading combobox from database in PHP - php

<?php
function __autoload($classname) {
$filename = "./". $classname .".php";
include_once($filename);
}
class empidload{
//function for load combo box
public function loadCombo(){
$connection=new connectdb();
$con=$connection->getCon();
$query = "SELECT * FROM department";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result)){
echo "<option value='".$row["dno"]."'>".$row["detail"]."</option>";
}
}
}
?>
<html>
<body>
<select name="department" id="department" style="width:204px;" onchange="change(this);">
<?php
$emp = new empidload();// calling function for load combo boxes
$emp.loadCombo();
?>
</select>
</body>
</html>
Here I try to load combobox from MySQL database. I wrote function and call it. But it doesn't work.can anyone help me please? actually what i want to load comboboxes from database table. And also I want to change another combobox according to the value of first combobox. Please help me.

You're mixing MySQL functions, you can't do that.
You're using mysql_query and other mysql_ functions but with a mysqli_ DB connection.
Change $result = mysql_query($query) or die(mysql_error()); to
$result = mysqli_query($con,$query) or die(mysqli_error());
and while($row=mysql_fetch_assoc($result)){
to
while($row=mysqli_fetch_assoc($result)){
Plus, as stated in Notulysses' answer:
change $emp.loadCombo(); to $emp->loadCombo();

Related

How to populate select dropdowns on html using database data

I'm a complete newbie with PHP/html stuff so please bear with me.
I've been trying to populate a select box using data from a myslq database and I can't get it to work, all I have is just a blank textBox.
This is what I have for now:
<select name="cargo">
<?php
require("conectadb.php");
$ok = conecta_db() or die ("Failure");
$sql = mysqli_query($ok, "SELECT descCargo FROM tbcargo");
while ($row = mysqli_fetch_array($sql)){
$c = $row['descCargo'];
echo("<option value=\"$c\">$c</option>");
}
?>
</select>
This is my database structure for now, with all the relevant rows:
Database name: tbcargo
PkCodCargo (primary key, AUTO_INCREMENT)
descCargo (what I want to fill the dropdown with)
I've tried everything I could think of to no avail, unfortunately.
Can someone help to point out exactly what am I doing wrong here?
Thanks in advance!
Update the part of your code to:
<select name= 'cargo'>
<?php
require("conectadb.php");
$ok = conecta_db() or die ("Failure");
$sql = mysqli_query($ok, "SELECT descCargo FROM tbcargo");
while ($rows = $sql->fetch_assoc())
{
echo '<option value="'.$rows['descCargo'].'">'.$rows['descCargo'].'</option>';
}
?>
</select>
I've finally found out the issue.
My file was named .html instead of .php and that's why nothing ever worked.
Hope this is able to help another newbie like myself out there
Cheers!
Here is the solution.
# here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT descCargo FROM tbcargo";
$result = mysql_query($sql);
echo "<select name='cargo'>";
while ($row = mysql_fetch_array($result)) {
echo '<option value="'. $row['descCargo'] .'">'.$row['descCargo'] .'</option>';
}
echo "</select>";

having trouble getting selected value from php dynamic selection option

I want to show options from my database for users to check, but having trouble getting user's choice.
So, I write two php files,
the first one doing things like: getting data from database, displaying in select option, then submit value by post to and the second php file.
And the second php file just display the recieved value.
Here's the first php file:
<html>
<body>
<form method="post" action="second.php">
<Select name=”select_value”>
<?
//connect to server
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Error " . mysqli_error($con));
$query = "SELECT * FROM MYTABLE" or die("Error in the consult.." . mysqli_error($con));
$result = $con->query($query);
//display result in select option
while ($row = mysqli_fetch_array($result)) {
echo "<Option value=".$row['ENTRY_ID']."> ".$row['ENTRY_NAME']."</Option><br>";
}
mysqli_close($con);
?>
</Select>
</form>
</body>
</html>
And the second php file:
<?
$option = isset($_POST['select_value']) ? $_POST['select_value'] : false;
if($option) {
echo $_POST['select_value'];
} else {
echo "not getting value of select option";
exit;
}
?>
If this works fine, I should see the selected value by the second php file, but I keep recieving my echo "not getting value of select option".
There must be something wrong between select option and my recieving file.
Can someone help?
try this double quotes
<Select name="select_value">
instead of <Select name=”select_value”>

No data displayed from DB in drop down menu

Echo 'Hello programmers' ;
I'm scratching my head about a pair of drop down menus. They are supposed to display all strings from the ename and mid rows. However this isn't happening and the drop down is only displaying one result from each row. There are multiple strings of test data in the actual rows.
I have some code here and perhaps you could lend a hand. Let me explain.
First off these are the methods from class dbme. To keep the clutter down the second function is exactly the same, except the SQL query for getResult() is obviously different (SELECT * from member as opposed to memberevent)
function openDB() {//creating database connection
$conn = mysqli_connect("localhost", "root", "", "mydb");
if (!$conn) {
$this->error_msg = "connection error could not connect to the database:! ";
return false;
}
$this->conn = $conn;
return true;
}
function getResult($sql){
$result = mysqli_query($this->conn , "SELECT * from memberevent" );
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysqli_error($this->conn));
}
}
Second, this is the web-side data.
$db1 = new dbme();
$db1->openDB();
$sql="select mid from member";
$result=$db1->getResult($sql);// get the ids from the tables for the select
$sql1="select ename from event";
$result1=$db1->getResult($sql1);// get the ids from the tables for the select
if (!$_POST) //page loads for the first time
{
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return validateForm( );">
Select Member ID: <select name="mid">
<?php
while($row = mysqli_fetch_assoc($result))
echo "<option value='{$row['mid']}'>{$row['mid']} </option>";
?>
</select>
<br />
Select Event name : <select name="ename">
<?php
while($row = mysqli_fetch_assoc($result1))
echo "<option value='{$row['ename']}'>{$row['ename']} </option>";
?>
</select>
<br />
I have a sneaking suspicion that a variable is getting over written, OR an extra loop is needed somewhere. But I'm not really sure, thus I post this question for advice.
Thanks.
You need to iterate through your results inside your getResults() function, otherwise you will always only get one row.
You are passing in the SQL but never using it:
Change
function getResult($sql){
$result = mysqli_query($this->conn , "SELECT * from memberevent" );
To
function getResult($sql){
$result = mysqli_query($this->conn , $sql );

SQL Query results into dropdown list

I am trying to get the names of the events from a table in a database, i need to insert this data in a dropdown list, so then someone clicks them and then the information for this specific event displays...
Here is my code up to now..
<?php
require "config.php"; // Your Database details
?>
<?PHP
$SQL = "SELECT title_en_US FROM civicrm_event";
$result = mysql_query($SQL);
// Write out our query.
$query = "SELECT title_en_US FROM civicrm_event";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
print $db_field['title_en_US'] . "<BR>";
}
?>
Can you tell me how to put first the event names into a dropdown list?
Thanks!
Please use the following code
$options='';
while($db_field = mysql_fetch_assoc($result))
{
$options .= "<option>".$db_field['title_en_US']."</option>";
}
For the html part,
<select name="titles"><? echo $options; ?></select>

php drop down list

i’m new to codeigniter and i’m working on a project. i have to create a dynamic drop down menu with values from my database, when a selection is made in the drop down as soon as you click on the submit button a new page has to occur where all the cities associated with the province selected in the drop menu appear, the cities are also in my database .My database consists of an id field, province field and a cities field.The drop menu is fine but cant seem to make the cities appear in the next page. your help will be highly appreciated
ok here's my code
this is from my view file which displays my drop menu this side is ok
<?
function writeCities($id)
{
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("msansi", $con);
$query = "SELECT cities FROM provinces WHERE id =";
$query .= $id;
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row[0];
}
function populateDropBox()
{
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("msansi", $con);
$result = mysql_query("SELECT id,title,cities FROM provinces");
while($row = mysql_fetch_array($result))
{
echo "<option value=$row[0]>" . $row['title']."</option>";
}
}
?>
<form name="myform" action="http://localhost/CodeIgniter_1.7.3/index.php/ndivhuho/submit" method="post">
<select name = "province" onChange="onChangeDropBox();"/>
<? populateDropBox(); ?>
<input type="submit" value="submit"; />
</form>
and here's my other view file which is supposed to display the cities in a text area
<?
function writeCities($id)
{
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("msansi", $con);
$query = "SELECT cities FROM provinces WHERE id =";
$query .= $id;
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row[0];
}
?>
<script type="text/javascript">
function onChangeDropBox()
{
var selected =0;
selected = document.myform.province.value;
var t = "<? writeCities(1);?>";
document.myform.textArea.value = t;
}
</script>
<form name=myform>
<textarea name="citites" readonly="true";></textarea>
</form>
i'm sure theres something i need to do in my controller which i don't know of
thanxx in advance!!!
Take a look at the following two guides on how to do what you're talking about doing:
http://php-ajax-code.blogspot.com/2007/07/ajax-triple-dropdown-with-states-cities.html
http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html
There are a few problems here.
The code you have provided is using native php functions to connect to mysql. You should be using the proper CodeIgniter libraries. Start by reading this.
http://codeigniter.com/user_guide/database/examples.html
Once you've read that..
"this is from my view file which displays my drop menu"
Take the code out of your view file! The database calls should be in a model, and that should be called by a controller, which passes the data through to your view file.
Probably read this too:
http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller

Categories