?
Solved

Checkbox  PHP

Posted on 2011-05-04
5
Medium Priority
?
328 Views
Last Modified: 2012-05-11
I've looked at hundreds of posts and tried many of them, but still just cannot get this to work.  Several said that checkboxes don't register with isset if they weren't changed, but that absolutely is not true.
I tried an old ASP trick, but it didn't work either.
Also, it dioesn't always show correctly if checked - sometimes it shows checked, sometimes it doesn't.  There are two unrelated checkboxes.
Please help!
<?php 
 if ($str[3] == "ON"){
 echo "<input type=checkbox name=Premiere  value='ON' checked='checked'> ";
 echo "<input type='hidden' name='Premierex'  value='ON' checked='checked'>";

 } else {
 echo "<input type=checkbox name=Premiere  value='OFF' > ";
 echo "<input type='hidden' name='Premierex'  value='OFF' >";

 }
 echo "<b>Premier Account</b><BR>";
 if ($str[1] == "ON"){
  echo "<input type=checkbox name=Approval  value='ON' checked='checked'> ";
  echo "<input type='hidden' name='Approvalx'  value='ON' checked='checked'>";

 } else {
  echo "<input type=checkbox name=Approval  value='OFF'> ";
  echo "<input type='hidden' name='Approvalx'  value='OFF'>";

 }
 ?>

 <b>Approval</b>


then in the submitted update pgm - tried it two different ways - neither works:
<?php
$sApproval=$sApprovalx;
if (isset($sApproval))
{
 echo "0-$sApproval-$sApprovalx<br>";
 if ($sApproval != $sApprovalx && $sApprovalx == "") {
 echo "1-$sApproval-$sApprovalx<br>";
 $sApproval = "ON";
 } elseif ($sApproval == "ON" && $sApprovalx == "ON") {
 echo "2-$sApproval-$sApprovalx<br>";
 $sApproval = "";
 } elseif ($sApproval == "OFF" && $sApprovalx == "OFF") {
 echo "3-$sApproval-$sApprovalx<br>";
 $sApproval = "ON"; }
}

$sPremiere = PostedVars('Premiere');
$sPremierex = PostedVars('Premierex');

if ($sPremiere != $sPremierex && $sPremiere != "") {
  $sPremiere = "ON"; 
} elseif ($sPremiere != $sPremierex && $sPremiere == "") { 
  $sPremiere = ""; 

} else {
 $sPremiere=$sPremierex;
}
?>

Open in new window

0
Comment
Question by:dcass
  • 3
5 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 35688543
Here I  test  passing values in your two array:
<?php 
$str[3]="ON";
$str[1]="ON";
 if ($str[3] == "ON"){
 echo "<input type=checkbox name=Premiere  value='ON' checked='checked'> ";
 echo "<input type='hidden' name='Premierex'  value='ON' checked='checked'>";

 } else {
 echo "<input type=checkbox name=Premiere  value='OFF' > ";
 echo "<input type='hidden' name='Premierex'  value='OFF' >";

 }
 echo "<b>Premier Account</b><BR>";
 if ($str[1] == "ON"){
  echo "<input type=checkbox name=Approval  value='ON' checked='checked'> ";
  echo "<input type='hidden' name='Approvalx'  value='ON' checked='checked'>";

 } else {
  echo "<input type=checkbox name=Approval  value='OFF'> ";
  echo "<input type='hidden' name='Approvalx'  value='OFF'>";

 }
 ?>

Open in new window



<input type=checkbox name=Premiere  value='ON' checked='checked'> <input type='hidden' name='Premierex'  value='ON' checked='checked'><b>Premier Account</b><BR><input type=checkbox name=Approval  value='ON' checked='checked'> <input type='hidden' name='Approvalx'  value='ON' checked='checked'>

Open in new window

0
 

Author Comment

by:dcass
ID: 35689386
OK - let me know how to fix that so it comes up checked or not according to if the database has ON or OFF,  then how to set it once the user has checked or unchecked it.  And how to ignore it if the user has not done either.
0
 

Accepted Solution

by:
dcass earned 0 total points
ID: 35690478
Found out how to work them - simple solution.
Check for "on" - that's it.  
<form method="POST" action="adUpdt">
RETRIEVE FROM DATABASE
<?php 
 echo "<input type=checkbox name=Premiere ";

 if ($str[3] == "on"){

  echo "checked='checked'>";

  } else {	echo ">";  }

  echo "<b>Premier Account</b><BR>";
?>
<br>
<input type="submit" name="Submit" id="button" value="Submit"  class="button" style="font-family: Verdana; font-size: 10pt; font-weight: bold">

</form></html>

Then THE UPDATE PGM adUpdt:
$sPremiere = $_POST('Premiere');

if ($sPremiere <> "on")

{

 $sPremiere = "";

}
Then UPDATE TO DATABASE.

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35690557
Two ways you can handle this.  One is to put the hidden input tag before the checkbox input tag.  The other is to test the checkbox in the request with isset.  Checkboxes that are not checked do not come through to PHP, so in those cases the hidden tag or the complete absence of the checkbox tag will give you the answer.
0
 

Author Closing Comment

by:dcass
ID: 35718389
Figured it out myself.

Question - would you tell me if I should give points for what was done here - basically a repeat of the question hoping for points?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 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