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
Solved

dynamically populate a drop down based on another drop down

Posted on 2004-08-23
7
227 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

792 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