Solved

dynamically populate a drop down based on another drop down

Posted on 2004-08-23
7
229 Views
Last Modified: 2013-12-25
this is some of the code i have so far. what this does is goes to index.cgi i want it to go to index.cgi?action=step, which it says it does but it really goes to the index.cgi. there's also a submit button on this page which i want to go to index.cgi?action=addarticle

any ideas on how to help me??

thanks


$form_name = $query->param('name');

print start_html('Add Time', '#FFFFFF'), h3('Add Time');

print $query->startform(-method=>'post');

connect_db;

$sth = $dbh->prepare("select distinct client_name from clients") || die "can't prepare statement.\n";

$sth->execute;

print "<table><tr><td>Client Name: </td><td><select name = 'name' onChange=\"if (this.selectedIndex){f=this.form;f.action='index.cgi?action=step';f.submit()};\"><option value=\"\">Select a Client</option>";

if ($form_name ne "") {
print "<option selected value=\"$form_name\">$form_name</option>";
}

while (@fields=$sth->fetchrow_array)
{
        $dbname=$fields[0];
        print "<option value=\"$dbname\">$dbname</option>";
}

$sth->finish();

print "</select></td></tr>";

print "<tr><td>Client Project: </td><td><select name=\"client_project\"><option value=\"\">Select a Project
</option>";

if($name)
{
$sth_B = $dbh->prepare("select * from clients where client_name='$name'") || die "can't prepare statement.\n";

$sth_B->execute() || die "Couldn't execute query\n";

while (my @row = $sth_B->fetchrow_array())
{
$my_td1 = $row[3];
print "<option value=$my_td1>$my_td1</option>\n";
}
$sth_B->finish();
}
print "</select></td></tr>";

# .... more code form stuff

print "</form><form name=\"main\" method=\"POST\">";

print "<tr><td colspan=2><input type=\"hidden\" value=\"addarticle\" name=\"action\">";

print "<input type=\"submit\" name=\"Submit\" value=\"Submit\">";

print "</font></td></tr></table>";

print "</form>";


# ... more code....


if (!param()) {
single;
exit(0);
} elsif ($action eq "add") {
add;
} elsif ($action eq "step") {
add;
} elsif ($action eq "addarticle") {
addarticle;
} else {
single;
}
exit(0);


0
Comment
Question by:jmingo
[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
  • 4
7 Comments
 
LVL 8

Expert Comment

by:inq123
ID: 11874211
Hi jmingo,

The problem that you thought it went to index.cgi instead of index.cgi?action=step is that you were using "<form name=\"main\" method=\"POST\">".  If you had used method=GET then you would see index.cgi?action=step displayed on your browser address bar.  But either way your program will receive the param('action') correctly, which is why you said "which it says it does".

Cheers!
0
 

Author Comment

by:jmingo
ID: 11874346
ok this is what happens now though

index.cgi?name=Test&client_project=&job=&hours=&month=&day=&content=

when i change the drop down.

still goes to the index.cgi single function.

any ideas?

thanks for the help.
0
 
LVL 8

Expert Comment

by:inq123
ID: 11874438
jmingo,

I'm somewhat confused by your code.  Why did you write "f.action='index.cgi?action=step'" in your JS?  Did you mean "f.action='step'"?  So exactly what are you trying to do?  Are you trying to have user select one option, then go to a new page where two selects will be displayed?  This seems a bit overkill since you could simply populate the 2nd select using the first select through JS (javascript).  You don't have to choose to print the 2nd select only when you have a param('name').  If you get rid of the DB-related code that has no bearing on the question it would also make it easier to understand what you want to do.  I can give you an example if you want.
0
 
LVL 8

Expert Comment

by:inq123
ID: 11874461
I think I see what you're trying to do.  f.action part is right.  I'll give you an example soon.
0
 
LVL 8

Accepted Solution

by:
inq123 earned 100 total points
ID: 11874593
jmingo,

This code should do more than what you asked for and I highly recommend you follow my example below to rewrite your code:

<script>
  function hide(option)
  {
    if(option) { window.document.a.e.style.visibility='hidden'; }
    else { window.document.a.e.style.visibility='visible'; }
  }
 
  function populate()
  {
    var a = window.document.a.b.value;
    var b = window.document.a.e;
    hide(0);
    if(a == 'c') { b.value = 'f' }
    else if(a == 'd') { b.value = 'g' }
    else { hide(1) }
  }

</script>

<body onload=hide(1)>

<form name="a" action="index.cgi" method="get">
  <select name="b" onChange=populate()>
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="hide">hide</option>
  </select>
  <select name="e" hidden="true">
    <option value="f">f</option>
    <option value="g">g</option>
  </select>
</form>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

726 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