Solved

Problems processing check boxes

Posted on 2004-08-08
9
187 Views
Last Modified: 2010-03-05
Dear All:

I am reading a txt datafile and creating a form. For 1 and 0 boolean values. I have created checkboxes for example:

<INPUT type=checkbox value=$calldonotdisturb1 name=donotdisturb1>

The above line is a part of larger form.

Now the problem when I check the checkbox.....It does turn the value to 1 it takes the hardwired value from above.

What is teh correct way to implement this...text boxes etc work fine new edited value is taken. With checkboxes it does take the new chceked value.

Please let me know what to do diffirent.

Best Regards

sunnybrad
0
Comment
Question by:sunnybrad
9 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11747710
Hi

I'm not quite sure what your problem is... Can you elaborate a little? What do you mean by turning the value to 1 and what is in $calldonotdisturb1?

Regards,
Zyloch
0
 

Author Comment

by:sunnybrad
ID: 11747775
Hi Zyloch:

The problem is in processing the checkbox...I use perl/cgi to create this page which has checkboxes. I put values in there from a data file.

Now when I go ahead and click the checkbox and say the previous value (The value I print there in the form) was zero the value still is what I printed i.e. 0 and not 1 because I checked it.

What I am doing is I think fundamentally flawed. It work for text boxes etc. For check box this way is flawed.

Let me know how to do this.

Regards

sunnybrad
 
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11747800
Yes, the value will still be 0. Now normally, if you don't do anything, if the checkbox is not checked an you submit the form, nothing about the checkbox will go through, meaning if you already know which checkboxes are being sent, it would be simple to just set the checkbox value as 1. Then, after you send it, check the URL to see if a checkbox was checked.

The other way would be to use Javascript. You can change the value of the checkbox. Then, when you submit the form, check every single checkbox so all of them will go through with the values you set for them.

(I'm assuming if the checkbox is checked, it's 1, otherwise it's 0).

<html>
<head>
<script language="javascript" type="text/javascript">
<!--

function setCheckBoxes() {
   var chkbxes = document.getElementsByTagName("input");
   for (var i=0;i<chkbxes.length;i++) {
      if (chkbxes[i].type=="checkbox") {
         if (chkbxes[i].checked) {
            chkbxes.value = 1;
         } else {
            chkbxes.value = 0;
            chkbxes.checked = true;
         }
      }
   }
}

//-->
</script>
</head>
<body>
<form name="myform" action="myaction" method="POST" onsubmit="setCheckBoxes();">
<!--Checkboxes, etc.-->
</form>
</body>
</html>


The above code will only work if the user has Javascript enabled. Also, it should work on almost all recent browsers. If you want a LOT of backward compatibility, the code will have to be modified slightly. Finally, this assumes all the checkboxes on the page should have values of 0 or 1.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:sunnybrad
ID: 11748324
Hi:

There must be a way in PERL I cannot do things in Javascript. In perl I can do

if ($query->param('donotdisturb1')){
$donotdisturb1 = $query->param('donotdisturb1');
}
else
{
$donotdisturb1 = 0;
}

So I do check if it is checked but the problem is even when checked it has a value of 0 and the above code will keep it zero.
Show me light on this one brothers.

Best Regards

sunnybrad
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11748478
Yes. You have to change the value of the checkbox then, and the only way to do that is with Javascript.

function changeCheckBox(chk) {
   if (chk.checked) {chk.value=1);}
   else {chk.value=0;}
}

<input type="checkbox" value="0" onclick="changeCheckBox(this);">
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 11749054
If you don't actually need the contents of the checkbox, then change

<INPUT type=checkbox value=$calldonotdisturb1 name=donotdisturb1>

to

<input  type="checkbox"  name="donotdisturb1">

Now you can either to

if ($q->param('donotdisturb') eq 'on') {
...
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 11749206
<INPUT type=checkbox value=$calldonotdisturb1 name=donotdisturb1 checked >
0
 
LVL 5

Accepted Solution

by:
ITcrow earned 200 total points
ID: 11867854
Sample Script:
==========

#! /usr/local/bin/perl -w

print "Content-type:text/html\n\n";

print qq~
<form>
<INPUT type=checkbox value=calldonotdisturb1 name=donotdisturb1>
<input type=submit>
</form>
~;

Invocation:
========

http://my_domain.com/path/to/test.cgi

When checkbox is Clicked:
Result is: http://my_domain.com/path/to/test.cgi?donotdisturb1=calldonotdisturb1

When checkbox is not Clicked:
Result is: http://my_domain.com/path/to/test.cgi?

BTW, in your example:
$donotdisturb1 = $query->param('donotdisturb1');

You are looking for value of absolute parameter 'donotdisturb1' whereas what you
want is:
$donotdisturb1 = $query->param($donotdisturb1);
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl program to obtain a machine's memory usage 6 22
cpan issue 1 63
Union rows in array that have common elements (Part 2) 4 75
Extract data from span tag 1 92
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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