Solved

database dynamic listboxes in php and mysql

Posted on 2004-08-16
34
462 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
  • 17
  • 14
34 Comments
 

Author Comment

by:banurekhap
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Expert Comment

by:thecode101
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, I mean 1 of those after each menu, two total on the page.
0
 

Author Comment

by:banurekhap
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I think I got it, Thankyou for your time, will get back to you.
0
 
LVL 3

Expert Comment

by:thecode101
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:banurekhap
Comment Utility
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
Comment Utility
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
Comment Utility
Change
if($result>=1){
      echo "inside if";
 }

TO

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

Author Comment

by:banurekhap
Comment Utility
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
Comment Utility
thanks I got the problem solved.
0
 

Author Comment

by:banurekhap
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
And do you have a mail server set up on your computer correctly?
0
 

Author Comment

by:banurekhap
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now