如何使用数据库中的值填充HTML 元素?(How can I populate an HTML element with values from a database?)

编程入门 行业动态 更新时间:2024-10-22 15:22:39
如何使用数据库中的值填充HTML element with values from a database?)

我试图从数据库中获取值并将它们放在HTML <select>标记内的下拉列表中。

我能够在一个长字符串中获取值并在一个选项中显示所有值,但我想将每个值放在一个单独的<option>标记中。 我只是不知道我可以使用什么逻辑来做到这一点。

这是我到目前为止所拥有的:

#!c:\perl\bin\perl.exe use CGI; require ("data_eXchangeSubs.pm"); $query = new CGI; print $query->header(-expires=>'-1d'); print $query->start_html( -title=>'Dex Vendor Testing', -bgcolor=>'white' ); $user = $query->param("user"); my $dataX = ${ConnectToDatabase($main::DBone, $main::dataENV)}; $resultSet = $dataX->Execute("select vendor from dex_vendor_info group by vendor"); while(!$resultSet->EOF) { $vendors .= $resultSet->Fields("vendor")->Value."\n"; $resultSet->MoveNext; } print <<ONE; <table width=75% border=0> <th colspan=2 align=left><strong><font size=5pt color=#FF6633 face=garamond>Vendor Information</strong</font><hr size=4pt color=midnightblue></th> <tr> <td align=left nowrap><font size=4pt face=garamond><label id=lVendor for=vendor><strong>Company Name</strong></font></label></td> <td align=left nowrap><font size=4pt face=garamond><label id=lVendor for=vendor><strong>Contact's Name</strong></font></label></td> </tr> <tr> <td align=left nowrap><select id="vendors"> <option>$vendors</option> </td> </td> <td align=left nowrap><input type=text name="contact" id=contact value="" size=25></td> </tr> </table> <br> ONE print $vendors; print $query->end_html;

I am trying to get values from a database and place them in a dropdown list within an HTML <select> tag.

I'm able to get the values in a long string and display all of them within a single option but I want to put each value in a separate <option> tag. I just don't know what logic I could use to do this.

Here's what I have so far:

#!c:\perl\bin\perl.exe use CGI; require ("data_eXchangeSubs.pm"); $query = new CGI; print $query->header(-expires=>'-1d'); print $query->start_html( -title=>'Dex Vendor Testing', -bgcolor=>'white' ); $user = $query->param("user"); my $dataX = ${ConnectToDatabase($main::DBone, $main::dataENV)}; $resultSet = $dataX->Execute("select vendor from dex_vendor_info group by vendor"); while(!$resultSet->EOF) { $vendors .= $resultSet->Fields("vendor")->Value."\n"; $resultSet->MoveNext; } print <<ONE; <table width=75% border=0> <th colspan=2 align=left><strong><font size=5pt color=#FF6633 face=garamond>Vendor Information</strong</font><hr size=4pt color=midnightblue></th> <tr> <td align=left nowrap><font size=4pt face=garamond><label id=lVendor for=vendor><strong>Company Name</strong></font></label></td> <td align=left nowrap><font size=4pt face=garamond><label id=lVendor for=vendor><strong>Contact's Name</strong></font></label></td> </tr> <tr> <td align=left nowrap><select id="vendors"> <option>$vendors</option> </td> </td> <td align=left nowrap><input type=text name="contact" id=contact value="" size=25></td> </tr> </table> <br> ONE print $vendors; print $query->end_html;

最满意答案

如果您正在使用CGI,请使用CGI。

print $query->popup_menu( -name => 'vendors' , -values => \@list_of_vendors , -default => $default_vendor );

你在行处理循环中得到@list_of_vendors :

my @list_of_vendors; while(!$resultSet->EOF) { push @list_of_vendors, $resultSet->Fields("vendor")->Value; $resultSet->MoveNext; }

如果您希望标签与值包含-labels标签中的值不同, -labels其指向包含您想要的文本的数组引用。

If you're using CGI, then use CGI.

print $query->popup_menu( -name => 'vendors' , -values => \@list_of_vendors , -default => $default_vendor );

And you get @list_of_vendors in your row processing loop:

my @list_of_vendors; while(!$resultSet->EOF) { push @list_of_vendors, $resultSet->Fields("vendor")->Value; $resultSet->MoveNext; }

If you want labels to be a different text value from values include -labels tag in the call and point it to an array ref containing the text you want visible.

更多推荐

本文发布于:2023-08-01 02:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   如何使用   元素   values   database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!