LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Javascript:Combobox creation taking data from DB On button click (https://www.linuxquestions.org/questions/programming-9/javascript-combobox-creation-taking-data-from-db-on-button-click-429954/)

Manashi 03-30-2006 06:37 AM

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....:)

graemef 03-30-2006 11:54 AM

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.

tangle 03-30-2006 12:52 PM

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>";
?>


graemef 03-30-2006 04:19 PM

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>";
  }


Manashi 03-31-2006 03:02 AM

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">

nickwelhar 03-12-2015 02:38 AM

Javascript Combobox
 
Try with.......Javascript Combobox

Nick


All times are GMT -5. The time now is 07:52 AM.