[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cgiapp] Javascript to set multiple form params


Hi all. I ve been having problems when I had forms that would have multiple
submit buttons (or links) such as user lists with multiple edit/delete
buttons. Its fine if I wanted to use GETs but I real pain if I used POSTs.

I have written a little Javascript function that takes the form name as it's
first parameter and key, value pairs of the input field name and value I
want to set it to.

In order to use it, you create all your fields into hidden (or normal)
fields in the form and then when you want to submit the form you have an
onClick action for the button.

I also provided a proof of concept script that takes the parameters and just
prints them out on a page.

There are many possibilities with C::A especially if you are obsessed with
POSTs like me.

You can have a single form for all of your template and change the runmode
dynamically onClick (I have forms that will call Edit/Delete depending on
the button you press)

I hope it helps and people find it useful. I m also open to comments and
suggestions on this and any other ways people use to do similar stuff.

<html>
<head>
<title>Hash function</title>
<script type="text/javascript" language="javascript">

function hashprint() {  // Take any number of parameters and return the sum
var myHashtable = new Object();
var formName = arguments[0];
// alert("Formname: " + formName);

for (var i = 1; i < arguments.length - 1; i = i + 2)
myHashtable[arguments[i]] = arguments[i+1];
//for (var inpName in myHashtable) alert('document.' + formName + '.' +
inpName + '.value = "' + myHashtable[inpName] + '"');
for (var inpName in myHashtable) eval ('document.' + formName + '.' +
inpName + '.value = "' + myHashtable[inpName] + '"');

eval ("document." + formName + ".submit()");
return false;
}

</script>
</head>
<body>
	Hi. Press the button
	<form name="testform" action="/cgi-bin/checkvars.pl" method="POST">
		<input type="hidden" name="action" value="">
		<input type="hidden" name="silent" value="">
		<input type="hidden" name="level" value="">
		<input type="hidden" name="help_topic" value="">
		<input type=button value="Click Here" onClick="hashprint('testform',
'action', 'actionfield1_value', 'silent', 'silentfield2_value', 'level',
'levelfield3_value', 'help_topic', 'help_topicfield4_value'); return
false"><br>
	</form>
</body>
</html>

----------------------------------------------------------------------------
--------------

#!/usr/bin/perl -w
use strict;

# System includes
use CGI;

# Echo back what we have
my $CGI = CGI->new();
my %params = $CGI->Vars();

# Prep the server
print "Content-Type: text/html\n\n";

my $username = $params{'username'};
my $action = $params{'action'};
my $silent = $params{'silent'};
my $root_level = $params{'level'};
my $help_topic = $params{'help_topic'};

# Header
print "Hello<br>";
print "The variables are:<br>";
print "\$action = [$action]<br>";
print "\$silent = [$silent]<br>";
print "\$root_level = [$root_level]<br>";
print "\$help_topic = [$help_topic]<br>";
print "Thank you<br>";

exit(0);


Dimitris Papaharisis


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.