Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where is the error in this code.

Status
Not open for further replies.

BestFitSolutions

Technical User
Mar 15, 2007
3
US
I have a form that post all data correctly except the "checked" boxes. What I am looking for is:
If the box is checked, then post only checked box, if it is not checked then post nothing or the fact that is is empty.

Here is the code:
if ($contents{'bcardadvertise25'} != "") {
print MAIL "ADVERT25=$contents{'bcardadvertise25'}\n";
}
if ($contents{'qtrpageadvertise50'} != "") {
print MAIL "ADVERT50=$contents{'qtrpageadvertise50'}\n";
}
if ($contents{'halfpageadvertise75'} != "") {
print MAIL "ADVERT75=$contents{'halfpageadvertise75'}\n";
}
if ($contents{'Fullpageadvertise125'} != "") {
print MAIL "ADVERT125=$contents{'Fullpageadvertise125'}\n";
}

Can anyone help with this?
the website is:
 
'!=' is a math operator, maybe you should use 'ne' if you are checking for an empty string.

Code:
if ($contents{'bcardadvertise25'} ne "") {

but I think you might be better off doing this:

Code:
if ($contents{'bcardadvertise25'}) {
  print MAIL "ADVERT25=$contents{'bcardadvertise25'}\n";
}


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I am not try to determine if there is a text string. It is either a checked or un-checked box.
If the box is checked, then it should show the field. If it is un-checked then it should dispaly nothing.

I have tried both of the expresions above and neither dispalys any out put what so ever; whether the box is checked on not.

Thanks you for efforts on this so far.

The following does display the output as "ON", but it always displays as "ON". I only want it to display if it is indeed checked.

Example:
if ($contents{'bcardadvertise25'}='ON') {
print MAIL "ADVERT25=$contents{'bcardadvertise25'}\n";
}
 
Code:
if ($contents{'bcardadvertise25'}='ON') {
  print MAIL "ADVERT25=$contents{'bcardadvertise25'}\n";
 }

the = above is setting the value to ON
= sets
== checks for numerical equal
eq text match for equal

I would agree that just doing what KevinADC said.
 
If Kevin's code isn't working for you, are you sure that [tt]$contents{'bcardadvertise25'}[/tt] contains what you think it does? Test that by doing a
Code:
  print MAIL "ADVERT25=$contents{'bcardadvertise25'}\n";
without any [tt]if {}[/tt] statement.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
I have done that and found that it always prints as "ON" regardless of whether the checkbox is checked or not checked.

I'm tring to find a way to print it as " " if it is not checked and "On" if it is checked.

When I used:
if ($contents{'bcardadvertise25'}='ON') {
print MAIL "ADVERT25=$contents{'bcardadvertise25'}\n";
}

The output shows the field name and "ON"; always, whether the field is checked or not.

When I used:
if ($contents{'bcardadvertise25'}) {
print MAIL "ADVERT25=$contents{'bcardadvertise25'}\n";
}

The output does not show anything; not even the field name - always, whether checked or not.

So, I am still stumped.
I would think that it being a check box, it should be either "On" or "Off", but maybe I am mistaken.

Thanks again for your assistance.
 
lets see the form code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I have done that and found that it always prints as "ON" regardless of whether the checkbox is checked or not checked.
Then there must be something wrong with the way you're assigning a value to [tt]$contents{'bcardadvertise25'}[/tt]. Are you using the cgi module? If not, you should.

I would think that it being a check box, it should be either "On" or "Off", but maybe I am mistaken.
You are. A checkbox is marked up in HTML like this:
Code:
<input type="checkbox" name="somename" value="X">
If the box is checked,whatever you put in the [tt]value[/tt] attribute (in this case an "X") is returned. If it's unchecked, nothing is returned at all. That's why it's usually enough to test whether the checkbox has a value, without worrying about what that value might be:
Code:
if ( $q->param('somename') ) { 
   print "the box was checked";
}

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top