Solved

dynamically populate a drop down based on another drop down

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 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

21 Experts available now in Live!

Get 1:1 Help Now