LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2006, 06:37 AM   #1
Manashi
LQ Newbie
 
Registered: Jan 2006
Posts: 27

Rep: Reputation: 15
Question Javascript:Combobox creation taking data from DB On button click


hi...
i am working on PHP with PostgreSQL as DB..OS is LINUX
Both r very New to me...
how to show options in a combobox by takein data from database
+
same combobox along with the data in it should get created every time a button is clicked

Expecting REply....
 
Old 03-30-2006, 11:54 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
What you need to do is:
  1. Connect to the database
  2. Make you database call, selecting the data that you want
  3. open an HTML select tag
  4. Loop through this data inserting the appropriate HTML: option tags for each database entry
  5. close the select tag

But take it in small steps, checking that at each stage it is working correctly.

graeme.
 
Old 03-30-2006, 12:52 PM   #3
tangle
Senior Member
 
Registered: Apr 2002
Location: Arbovale, WV
Distribution: Slackware
Posts: 1,761

Rep: Reputation: 78
Are you wanting to make a drop down box that lists all data in a column in a certin table? If so, do something like this (it is an example that uses MySQL).
Code:
$database = "db";
$connection = "localhost";
$username = "dbadmin";
$password = "password";

echo "<form action=search_result.php method=POST>";

@ $db = MYSQL_PCONNECT($connection, $username, $password);
mysql_select_db($database);
if (!$db)
  {
    echo "Could not connect to database!";
    exit;
  }

echo "<table width='100%'>";

// Select Data
echo "<tr><td width='25%'>";
echo "<font size=4>Data: </font></td>";
echo "<td>";
$query = "SELECT DataName FROM DataType";
$result = mysql_query($query);
if(mysql_num_rows($result))
  {
    echo "<select name=Dname>";
    while($row = mysql_fetch_row($result))
      {
        print("<option value=\"$row[0]\">$row[0]</option>");
        echo "<br>";
      }
  }
else
  {
    print("<option value=\"\">No data created yet</option>");
  }
echo "</td></tr>";
?>
 
Old 03-30-2006, 04:19 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Just a couple of comments to the code tangle provided.

The <br> is not necessary, although a \n would made the generated html easier to read.

The select tag should be closed, after while, giving...

Code:
if(mysql_num_rows($result))
  {
    echo "<select name=Dname>";
    while($row = mysql_fetch_row($result))
      {
        echo "<option value=\"$row[0]\">$row[0]</option>\n";
      }
    echo "</select>";
  }
 
Old 03-31-2006, 03:02 AM   #5
Manashi
LQ Newbie
 
Registered: Jan 2006
Posts: 27

Original Poster
Rep: Reputation: 15
hi...
thanks 4 the reply...
i am also doing the same thing...
but when i am clicking a button to create another select box its not containing the same data....
already i hav select box with the options from DB in the table...
its like drop down list
i hav used javascript for the button name "MORE"...
The code is as follows :-

Quote:
<script language="Javascript">
var theTable,where;
function addRow(form)
{
theTable=document.getElementById("mytab");
where=form.sub_cnt.value;
var newRow = theTable.insertRow(where);
var y=newRow.insertCell(0);
var z=newRow.insertCell(1);
var txt=document.createElement("input");
var sel= document.createElement("select");
txt.size="8";
txt.maxlength="5"
++where;
form.sub_cnt.value=where;
z.appendChild(txt);
y.appendChild(sel);
}
</script>
Inside the form tag the code is as follows :-

Quote:
<form name="criteria_frm" action="file.php?group=criteria_frm.grp_id.value" method="POST">

<tr><td bgcolor="#FFFFFF">
<select name="subject_id[]">
<option value="-1">[All Subjects]</option>
<?php
$s = "select id, title from category order by title";
$conn = getConnection();
$rs = pg_query($s);
if($rs!=null)
{
while($row = pg_fetch_row($rs))
{
echo "<option value=\"$row[0]\">".addslashes(chop($row[1]));
pg_free_result($rs);
}
pg_close($conn);
?>
</select>
</td>
<input type="button" onclick="return addRow(document.criteria_frm)" value="More">

Last edited by Manashi; 03-31-2006 at 03:05 AM.
 
Old 03-12-2015, 02:38 AM   #6
nickwelhar
LQ Newbie
 
Registered: Mar 2015
Posts: 2

Rep: Reputation: Disabled
Javascript Combobox

Try with.......Javascript Combobox

Nick
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to create box with button click ...using Javascript Manashi Programming 7 03-29-2006 11:48 PM
make thumb button/third button double-click taboom Linux - Hardware 0 03-20-2006 02:39 PM
PHP image-creation while loop taking forever benrose111488 Programming 9 07-09-2005 07:04 PM
can javascript automatically populate login fields and click button for me? opioid Programming 1 02-16-2005 03:58 PM
Unwanted border on button click Rachel Carter Programming 1 02-11-2003 12:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration