Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Pass variable values from HTML to CGI using "onclick"

by newtoperl101 (Novice)
on Jul 05, 2013 at 20:34 UTC ( #1042795=perlquestion: print w/ replies, xml ) Need Help??
newtoperl101 has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks. I have a HTML file that has a form with different text input blocks (Add/Search/Delete)each with different submit buttons. There is a CGI script which takes all text box parameters from the HTML file. However, for each submit button, with "onclick" I want to pass a value(0,1,2 etc) to it so that I can have a switch case 1-Add, 2-Search etc in the CGI script for relevant Add/Search/Delete subroutines. Inside the html file I created a little javascript function as follows :

<script type="text/javascript"> var mysample = ''; function setval(varval) { mysample= varval; alert(mysample); } </script>
and posted this in the html for two submit buttons:
<input type="submit" value="Add Entry" onclick="setval(1)" /> <input type="submit" value="search" onclick="setval(2)" />
Also, I added a hidden input to pass the parameter to CGI :
<td align="left"><input type="hidden" name="mysample" size="15" maxlen +gth="50"></td>
However, on running the html file, it is not passing the mysample variable value to the CGI. Any ideas?

Comment on Pass variable values from HTML to CGI using "onclick"
Select or Download Code
Re: Pass variable values from HTML to CGI using "onclick"
by poj (Priest) on Jul 05, 2013 at 21:17 UTC
    You don't need javascript, just name values. Try this test cgi program.
    #!perl # test.cgi use strict; use CGI qw /:standard/; print header,start_html('test'); print h3("Parameters"); for (param){ print "$_ = ".param($_)."<br/>" } print qq!<form> <input type="submit" name="action" value="Add"/> <input type="submit" name="action" value="Edit"/> <input type="submit" name="action" value="Delete"/> <input type="hidden" name="hide" value="123"/> </form>!; print end_html;
    poj
      Poj I am using two different HTML and CGI files. I would prefer a way to pass the value from HTML to CGI. If you want to understand my problem better, I can paste the entire scripts.
        It was a demo of the html, I put it in a cgi so you see the parameters being sent. Try it with this html page
        <html> <form action="test.cgi" method="post"> <input type="submit" name="action" value="Add"/> <input type="submit" name="action" value="Edit"/> <input type="submit" name="action" value="Delete"/> <input type="hidden" name="hide" value="123"/> </form> </html>
        poj
Re: Pass variable values from HTML to CGI using "onclick"
by Anonymous Monk on Jul 06, 2013 at 19:42 UTC
    Why do you think that a javascript variable named mysample and a form input named mysample are related? How about something like this:
    function setval(varval) { mysample= varval; var inputs = document.getElementsByName("mysample"); inputs[0].value = mysample; alert(mysample); }
    ...there are probably more robust ways of achieving what you want, though. Modifying a bit poj's form sample, we can do this:
    <form action="test.cgi" method="post"> <input type="submit" name="action-add" value="Add"/> <input type="submit" name="action-edit" value="Edit"/> <input type="submit" name="action-delete" value="Delete"/> <input type="hidden" name="hide" value="123"/> </form> ... if ($cgi->param('action-add')) { # do something ... } elsif ($cgi->param('action-edit')) { ... etc
Re: Pass variable values from HTML to CGI using "onclick"
by sundialsvc4 (Abbot) on Jul 07, 2013 at 15:31 UTC

    I suggest that you start the page in a browser while running a debugger such as FireBug.   This will allow you to do two very useful things:

    1. You can set breakpoints in the JavaScript code, to see what it is doing and where/when it is being called.
    2. When the page finally submits something to the host, you can see the actual GET/POST that it sent.   Never Assume.™

    You don’t say whether the JavaScript code is known-good, so let’s assume, until demonstrated otherwise, that it is the actual source of the bug.   Let’s assume that the real problem is that the JavaScript either isn’t passing mysample, or that its value is empty.   Exercise the code and use FireBug to find the actual POST.   If you can see the correct value being passed, only then do you know (but now, you know), that the error is in the Perl software.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1042795]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-12-27 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls