By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Newb requires help with javascript submit

P: n/a
Hi.

I am trying to create a CGI script that will use Perl to read a text file to
get a list of values, then turn that list into a menu on a website. The menu
will be a form with a JavaScript "submit" function. . When someone selects a
menu-item, the form will submit the value of that list-entry to another CGI
page.

The problem I'm encountering is that the script I've written submits every
value of the form rather than the one specific menu item's value. This means
that, on the receiving CGI page, I'm unable to tell which one value has been
selected because I've received them all.

As near as I can tell, the flaw in my scripting is in the JS line (but just
in case I'm wrong, I've CC'd this to PERL.MISC).

I am very new to JS and am having a very difficult time comprehending it.
Before I display the code I've written, I'd like to also ask for
recommendations for good beginner-guide-books for JS. Is O'Reilly's "Rhino"
book a good starting place?

Regards and thanks.

Here's the relevant code:
print "<FORM ACTION='/cgi-bin/sub_cat_view.cgi' NAME='myform' METHOD=
'get' target='splash'>";
open (CATDAT, "<categories.dat");

while (<CATDAT>){
@categories = split /#/, "$_";
$cat_length = @categories;
$loop=0;
print "<H4>";

while ($loop < $cat_length){
print "<A
HREF='javascript:document.myform.submit()'>$catego ries
[$loop]</A><BR>";
print "<INPUT TYPE='hidden' NAME='$categories[$loop]'
VALUE='cat
egory'>";
$loop++;
}
}
close (CATDAT);
print "</FORM>";
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Lee
javascript_noob12 said:

Hi.

I am trying to create a CGI script that will use Perl to read a text file to
get a list of values, then turn that list into a menu on a website. The menu
will be a form with a JavaScript "submit" function. . When someone selects a
menu-item, the form will submit the value of that list-entry to another CGI
page.

The problem I'm encountering is that the script I've written submits every
value of the form rather than the one specific menu item's value. This means
that, on the receiving CGI page, I'm unable to tell which one value has been
selected because I've received them all.

As near as I can tell, the flaw in my scripting is in the JS line (but just
in case I'm wrong, I've CC'd this to PERL.MISC).


First, you need to learn HTML.
There is no way in HTML to submit only specific elements of a form.

You shouldn't really post CGI code to a Javascript group.
It's better to post a sample of HTML that demonstrates the problem.

One thing that you do need to know about Javascript is that it's
a bad idea to submit a form via the javascript: pseudo-protocol in
the HREF attribute of a link. If there's a reason to use links
instead of buttons, use the onclick handler, not the HREF attribute.

It seems like what you really want is a form with a single hidden
element whose value is set by Javascript before the form is submitted
when you click the link.

Jul 23 '05 #2

P: n/a
javascript_noob12 wrote:
As near as I can tell, the flaw in my scripting is in the JS line


You are probably right. But the code you posted is Perl.

What you should do, if you want an answer to your question, is post the
HTML/JavaScript that the Perl script produces to comp.lang.javascript.
Make sure you follow whatever posting guidelines comp.lang.javascript has.

Jul 23 '05 #3

P: n/a
javascript_noob12 wrote:
The problem I'm encountering is that the script I've written submits every
value of the form rather than the one specific menu item's value.


That's the way a submit button works.
You could modify your perl program to output many "<FORM ACTION=" lines
instead of just one.
-Joe
Jul 23 '05 #4

P: n/a
[posted & mailed]

On Mon, 6 Sep 2004, javascript_noob12 wrote:
The problem I'm encountering is that the script I've written submits
every value of the form rather than the one specific menu item's value.
This means that, on the receiving CGI page, I'm unable to tell which one
value has been selected because I've received them all. while ($loop < $cat_length){
print "<A
HREF='javascript:document.myform.submit()'>$categ ories
[$loop]</A><BR>";
print "<INPUT TYPE='hidden' NAME='$categories[$loop]'
VALUE='cat
egory'>";
$loop++;
}


As a Perl note, I'd suggest rewriting that as a for statement:

for my $cat (@categories) {
print qq{
<a href="javascript:document.myform.submit()">$cat</a><br>
<input type="hidden" name="$cat" value="category">
};
}

Your probably is half JS, half HTML. A form will submit *all* the data in
it. You either need to make a separate form for EACH link (which I do NOT
suggest), or else come up with a new mechanism for submitting the
appropriate data.

I don't understand why you make the *name* of the field to be passed
variable, and the value constant. Here's how I'd do it:

print qq{
<form method="..." action="something.cgi" name="myform">
<input type="hidden" name="cat" value="">
};

for my $cat (@categories) {
print qq{
<a href="javascript:document.myform.cat.value='$cat';
document.myform.submit()">$cat</a><br>
};
}

Now, when you click on a link, it sets the value of the 'cat' hidden
field, and then submits the form.

I'm not sure why you *need* a form, when you can just make links like

<a href="something.cgi?cat=$cat">$cat</a>

but maybe there's more you're not telling us.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.