Solved

database dynamic listboxes in php and mysql

Posted on 2004-08-16
34
499 Views
Last Modified: 2013-12-12
HI,
I anm creating a form where in I populate a list with the tablenames i have in the database and make the user select one tablename from the listbox and immediatly in the same page I've an other lsitbox which will be populated with the field names of the table that the user selected, I was successful in creating the tables listbox and was not sure how to get the selected value from the listbox and populate the same form, another dynamic list box with values : example

UPDATE TABLE (listbox-tablenames) SET (listbox -fieldname) = (textbox-value);
CAn anyone pls help me...............




$connectfile = "db.php";
require $connectfile;

$db="test";
@mysql_select_db("$db") OR die("Unable to select database $db");

<form method="POST" action="insertedit.php">
echo "UPDATE TABLE  ";
echo '<select name="tables">';

// Populate dropdown menu with tables from the specified database:
$result = mysql_list_tables($db);
    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $tbl_names[$i] = mysql_tablename($result, $i);
    echo "<option value=\"" . $tbl_names[$i] . "\" />" . ucfirst($tbl_names[$i]) . "</option>\n";

    }

<input type="submit" name="Submit" value="Submit">
mysql_close();
</form>
?>
0
Comment
Question by:banurekhap
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 14
34 Comments
 

Author Comment

by:banurekhap
ID: 11814874
HI,
I anm creating a form where in I populate a list with the tablenames i have in the database and make the user select one tablename from the listbox and immediatly in the same page I've an other lsitbox which will be populated with the field names of the table that the user selected, I was successful in creating the tables listbox and was not sure how to get the selected value from the listbox and populate the same form, another dynamic list box with values : example

UPDATE TABLE (listbox-tablenames) SET (listbox -fieldname) = (textbox-value);
CAn anyone pls help me...............




$connectfile = "db.php";
require $connectfile;

$db="test";
@mysql_select_db("$db") OR die("Unable to select database $db");

<form method="POST" action="insertedit.php">
echo "UPDATE TABLE  ";
echo '<select name="tables">';

// Populate dropdown menu with tables from the specified database:
$result = mysql_list_tables($db);
    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $tbl_names[$i] = mysql_tablename($result, $i);
    echo "<option value=\"" . $tbl_names[$i] . "\" />" . ucfirst($tbl_names[$i]) . "</option>\n";

    }

<input type="submit" name="Submit" value="Submit">
mysql_close();
</form>
?>
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11820155
The only way to make a dynamic text box is to use javascript. For example:

 Add this javascript  in between the <head></head> tags of the page
<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
   if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

And change each value of your 1st down menu to look like this
<select name="firstMenu" onChange="MM_jumpMenu('self',this,1)">    // add onChange to select

<option value="nameOfpage.php?tableName=$tbl_names">$tbl_names</option>      //change nameOfpage to the name of your page

</select>

Then when they select a option from the menu, the page will be refreshed with the variable the selected. You will have to edit the above to work with your script.
0
 

Author Comment

by:banurekhap
ID: 11820821
I prety much understood what you are trying to say, except for one thing, I am not sure what the MM_jumpMenu() funct does. Why do we need that, and also Where do I write the code inorder to populate the next list box, depending on the selection from the first listbox. As ?I mentioned

UPDATE TABLE (first listbox-tablenames) SET (Second listbox -fieldname) = (textbox-value);

Thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:thecode101
ID: 11821282
The MM_jumpMenu() function goes between then <head> </head> lines of your page, like:
<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
   if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

What this does is refresh the page, and pass through the value of the option you just selected.
So lets say the choose the following option in the first menu: (this is what it looks like in html)
<option value="nameOfpage.php?tableName=myTable">myTable</option>

The page would refresh and it would say nameOfpage.php?tableName=myTable. So now it is time to populate the second list box. You need something like this:
$tableName = $_GET['tableName'];
if (isset($tableName))
{
   // Write code to populate the next list, if you need to select from a table you can use something like
   mysql_query ("select * from $tableName",$db);
}
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11821321
By the way, you do not need to make any changes to the MM_jumpMenu javascript it will work as it is listed above, you will only need to change the values of your first select menu to be formatted differently.
0
 

Author Comment

by:banurekhap
ID: 11833557
Thankyou, I tried the way you mentioned, the problem I got is that when the page is refreshed I get back the same menu and the next line of code which is the code for the 2nd menu is never executed.
Here's the code............


<html>
<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
   if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>
<body>
<?
$connectfile = "db.php";
require $connectfile;

$db="test";
@mysql_select_db("$db") OR die("Unable to select database $db");
echo "UPDATE TABLE  ";

 ?>
  <select name="tablenamemenu" ONCHANGE="MM_jumpMenu('self',this,1)">
 <?

  // Populate dropdown menu with tables from the specified database:
  $result = mysql_list_tables($db);
    for ($i = 0; $i < mysql_num_rows($result); $i++) {
      $tbl_names[$i] = mysql_tablename($result, $i);
      echo "<option value=\"editinfo.php?tableName=$tbl_names" . $tbl_names[$i] . "\" />" . ucfirst($tbl_names[$i]) . "</option>\n";
    }


$tableName = $_GET['tableName'];
echo "$tableName";
echo "  SET  ";
if (isset($tableName))
{
   // Write code to populate the next list, if you need to select from a table you can use something like
   $result = mysql_query("select * from $tableName",$db);
   if ($result){
   ?>
   <select name="fieldnamemenu" ONCHANGE="MM_jumpMenu('self',this,1)">
   <?  for ($i = 0; $i < mysql_num_fields($result); $i++) {
      //print "<th>".mysql_field_name($result, $i)."</th>\n";
      $field_names[$i] = mysql_field_name($result, $i);
       echo "<option value=\"editinfo.php?fieldName=$field_names" . $field_names[$i] . "\" />" . ucfirst($field_names[$i]) . "</option>\n";
   }
}
}
mysql_close();
?>
</form>
</body>
</html>
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11841529
Your missing a
</select>
after each of the menus. Try adding two of those after each menu and see if that works.
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11841535
Sorry, I mean 1 of those after each menu, two total on the page.
0
 

Author Comment

by:banurekhap
ID: 11841928
Yes that was right, sorry for asking so manu questions, I am new and learning php, The problem is the value that is passed from the first menu is  Arrayft_familytree , so I think I need only the value familytree I think I need to split it and get the value after the _ , I'm trying to do that. then I'll know if the next menu will popup. meanwhile anysuggetions are welcome......
thankyou
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11842049
Change this
echo "<option value=\"editinfo.php?tableName=$tbl_names" . $tbl_names[$i] . "\" />" . ucfirst($tbl_names[$i]) . "</option>\n";

TO

echo "<option value=\"editinfo.php?tableName=".$tbl_names[$i]."\" />" . ucfirst($tbl_names[$i]) . "</option>\n";
0
 

Author Comment

by:banurekhap
ID: 11842464
NO the 2nd menu box line of code is not executed, and also the first item in the first menu box could not be selected. here's the code

<html>
<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
   if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>
<body>
<?
$connectfile = "db.php";
require $connectfile;

$db="test";
@mysql_select_db("$db") OR die("Unable to select database $db");
echo "UPDATE TABLE  ";

 ?>
  <select name="tablenamemenu" ONCHANGE="MM_jumpMenu('self',this,1)">
 <?

  // Populate dropdown menu with tables from the specified database:
  $result = mysql_list_tables($db);
    for ($i = 0; $i < mysql_num_rows($result); $i++) {
      $tbl_names[$i] = mysql_tablename($result, $i);
      //echo "<option value=\"editinfo.php?tableName=$tbl_names" . $tbl_names[$i] . "\" />" . ucfirst($tbl_names[$i]) . "</option>\n";
      echo "<option value=\"editinfo.php?tableName=".$tbl_names[$i]."\" />" . ucfirst($tbl_names[$i]) . "</option>\n";
    }
?>
</select>
<?
$tableName = $_GET['tableName'];
echo "$tableName";
echo "  SET  ";

if (isset($tableName))
{

   // Write code to populate the next list, if you need to select from a table you can use something like

   if($tableName == "ft_familylogin")
   {
    $result = mysql_query("select * from $tableName",$db);
    if ($result){
         ?>
         <select name="fieldnamemenu" ONCHANGE="MM_jumpMenu('self',this,1)">
         <?  for ($i = 0; $i < mysql_num_fields($result); $i++) {
              //print "<th>".mysql_field_name($result, $i)."</th>\n";
              $field_names[$i] = mysql_field_name($result, $i);
               echo "<option value=\"editinfo.php?fieldName=" . $field_names[$i] . "\" />" . ucfirst($field_names[$i]) . "</option>\n";
             }
    }
  }
?>
</select>
<?
}
mysql_close();
?>
</form>
</body>
</html>

0
 
LVL 3

Expert Comment

by:thecode101
ID: 11842603
Ok I recommend adding a blank option first, so that you can select the table:
<select name="tablenamemenu" ONCHANGE="MM_jumpMenu('self',this,1)">
<option value=""></option>

Then when you select an option from the first menu, what does it return when you echo "$tableName";
0
 

Author Comment

by:banurekhap
ID: 11842757
yes the $tableName value is exactly the tablename that I do have in my database with data. But what I am more concerned is that not even the list box seems to be appearing. here's the sign of output, and by the by the the blank value is working so I can select even the first value from the dropdown list.  thankyou once again

UPDATE TABLE  "Dropdown list" ft_families SET

ft_families is the value that I selected from the list that is being echoed .
0
 

Author Comment

by:banurekhap
ID: 11842774
I think I got it, Thankyou for your time, will get back to you.
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11842807
It is not being displayed because you have
 if($tableName == "ft_familylogin")
   {
             
    }

This means that unless you select the familylogin table the second menu will not be displayed.
0
 

Author Comment

by:banurekhap
ID: 11843877
That was not the problem, that was the example, even when i selected familylogin nothing was displayed, but the line

$result = mysql_query("select * from $tableName",$db);

when I removed the $db then it worked beautifully.

Thankyou,
Will get back to you, verysoon
0
 

Author Comment

by:banurekhap
ID: 11845187
i've another problem the query when reun seperatly returns value but when in the php it doenst return anything

<?
include '../login/checklogin.php';
$connectfile = "../login/db.php";
require $connectfile;
$db = "test";
$familyusername = $_SESSION['username'];

echo "$familyusername";

echo "Select the family you wish to Modify";

$result = mysql_query("SELECT ft_families.familyname,ft_families.familyid
                       FROM ft_families,ft_familylogin
                       WHERE ft_familylogin.user = $familyusername
                       && ft_familylogin.familyid = ft_families.familyid");
if($result>=1){
      echo "inside if";
 }
?>

it doenst even get into the if statement. the outpu tis


Notice: A session had already been started - ignoring session_start() in c:\my documents\poola.com\school\login\checklogin.php on line 2

banuSelect the family you wish to Modify

help
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11851301
Change
if($result>=1){
      echo "inside if";
 }

TO

if (mysql_num_rows($result) >= 1){
      echo "inside if";
 }
0
 

Author Comment

by:banurekhap
ID: 11884900
hi,
I have a problem. In a form i've aquery where i retrieve name and id . i have a listbox to display the name . once the user selects a name he submits the form. The problem i have here is in the called form how do i go about getting the id which i dont display in the list.

Help
Thanks
here's the code

// <html>
// <body>
<?
include '../login/checklogin.php';

$connectfile = "../login/db.php";
require $connectfile;

$db = "test";
$familyusername = $_SESSION['username'];

echo "$familyusername";

echo "Select the family you wish to Modify";
?>
<form name="" method="post" action="../family/searchtree.php">
<?
$query1 = "SELECT ft_families.familyname as fn,ft_families.familyid  as fid
                       FROM ft_families,ft_familylogin
                       WHERE ft_familylogin.user = '$familyusername'
                       && ft_familylogin.familyid = ft_families.familyid";
$result = mysql_query($query1);
// echo "$query1"."--".$result;
if($result){
         echo "inside if";

         ?>
         <select name="familyname">

         <?
           $i=0;
           while($row1 = mysql_fetch_array($result,MYSQL_ASSOC)){
                $db1_familyname[$i] =  $row1["fn"];
                $db1_familyid[$i] =  $row1["fid"];
                // "---". ucfirst($db1_familyname[$i]) .
                echo "<option value=\"". $db1_familyname[$i]."\" />" . $db1_familyname[$i] ."</option>\n";
                $i++;
           }
            ?>
            </select>
            <?
 }
 echo '<input type="submit" name="Submit" value="Submit">';
?>
</form>
0
 

Author Comment

by:banurekhap
ID: 11887123
thanks I got the problem solved.
0
 

Author Comment

by:banurekhap
ID: 11906411
I have a question. I have a table PERSON which has an fields(id,name address) ihave like 5 rows already int the table where i inserted everything the, id from 1-5 and now i came to know that the id field can be made auto increment as it is a primary and unique key, is there any way that i can make the field auto with erasing the data already present.

Please help
thanks
0
 

Author Comment

by:banurekhap
ID: 11906428
I have a question. I have a table PERSON which has an fields(id,name address) ihave like 5 rows already int the table where i inserted everything the, id from 1-5 and now i came to know that the id field can be made auto increment as it is a primary and unique key, is there any way that i can make the field auto
i meant without erasing the data already present
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11906855
Just set it to autoincrement and be primary key and unquie and then the next one you insert will be +1 whatever the last one that was already entered. It won't erase any data.
0
 

Author Comment

by:banurekhap
ID: 11907126
what would the syntax be:

person{
 id varchar(15) primary,
name varchar(25),
address varchar(25));

how would i modify this table;

thanks
0
 
LVL 3

Expert Comment

by:thecode101
ID: 11913831
to alter the table so that the ID will be autoincrement run the following SQL query:

ALTER TABLE `person` CHANGE `id` `id` INT( 15 ) NOT NULL AUTO_INCREMENT
0
 

Author Comment

by:banurekhap
ID: 12010340
Hi i am getting a error llike this when i use the mail in php what am i doing wrong?

mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()


the code is

  mail($email_address, $subject, $message,
        "From: MyDomain Webmaster<admin@mydomain.com>\n
        X-Mailer: PHP/" . phpversion());

help!
0
 
LVL 3

Accepted Solution

by:
thecode101 earned 125 total points
ID: 12018484
It was unable to connect to your mail server. Did you set up your mail server correctly in the php.ini?

http://us2.php.net/manual/en/ref.mail.php
0
 

Author Comment

by:banurekhap
ID: 12020092
yes i   checked the php.ini file

[mail function]
; For Win32 only.
SMTP = localhost ; for Win32 only
smtp_port = 25
sendmail_from= me@localhost.com ; for Win32 only

; For Win32 only.
;sendmail_from = me@example.com
0
 
LVL 3

Expert Comment

by:thecode101
ID: 12028376
And do you have a mail server set up on your computer correctly?
0
 

Author Comment

by:banurekhap
ID: 12057176
this question is not related to php, I would like to know if there is a way getting  the background color from a web page in java. if the color on the webpage changes then the program should be updated with the current color. is it possible
ex: suppose in the experts  exchange homepage the background color is blue so i would like to know what color it is and code that color in my program, in case if the blue color changes to someother color then my program should automatically update itself with the current color.
0
 
LVL 3

Expert Comment

by:thecode101
ID: 12067772
Banurekhap,

Your latest question is really better posted under the Javascript section. Also, in general it is really better to post new questions as new topics, instead of posting them as comments. Lets say someone comes by and answers your javascript question, and you accept their answer. That is unfair to me, since I answered your inital question. I recommend you accept my answer, and post your javascript comment as a seperate question under the JavaScript category. If you feel I did not adequately answer your intial question, please let me know.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question