Forum Moderators: coopster

Message Too Old, No Replies

keep checkbox checked if it was checked

else display error message

         

H2O_aa

9:46 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



I have a form with a bunch of text fields along with two checkboxes. At least one checkbox has to be checked or else an error message will display. If the checkbox has been checked, the form has to retain that value. I have no problem programming the text fields that way, but don't know what to do with checkboxes.

if(isset($a) && isset($b)) {
if($a!= "checked" ¦¦ $b!= "checked")
$error = TRUE;
else //else if a or b was already checked, keep it checking, but I don't know how to write that.

}

a <input type="checkbox" name="a" value="checked">
b <input type="checkbox" name="b" value="checked">

This is how I do the text fields
-------------------------------------
if(isset($Name)) {
if(empty($Name))
$Name_error = TRUE; //i have another section of code dealing with this to display the appropriate message
else
$Name_new = $Name;
}

<?php if(isset($Name_new)) {?>
<input name="Name" value ="<?php echo $Name_new;?>" type="text">
<?php } else {?>
<input name="Name" type="text">
<?php }?>

ironik

10:00 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



How about:

a <input type="checkbox" name="a" <?php if (isset($a)) { echo 'value="checked"'; }?> >
b <input type="checkbox" name="b" <?php if (isset($b)) { echo 'value="checked"'; }?> >

(I'd probably be using $_POST['a'] and $_POST['b'] instead of the $a and $b ... register globals on can leave you open to security holes, so if you are able it should get turned off).

H2O_aa

10:23 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



That doesn't retain the check.

You see, I have 10 text fields then the two checkboxes in the form. All the text fields are required and at least one checkbox has to be checked.

Say if I filled out 9 text fields and checked one checkbox, but I accidentally left one text field unfilled. Then the error message comes up telling me I need to fill out one more text field. If I don't retain the values in all those other fields, the users will have to fill them in again. That's the reaon why if the checkbox is already checked, keep it checking. If a text field has already been filled in, keep the value in.

ironik

11:57 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



You've kinda lost me there, I can't see the reason for having the checkbox's (if you only need to test for empty text fields)... probably because I'm not sure exactly what context you are using them in.

What are you attempting to do with your form?

H2O_aa

5:05 pm on Apr 8, 2005 (gmt 0)

10+ Year Member



Let me try my explanation again.

I have a web form containing a question that asks users what ways do they liked to be contacted by:

What way(s) would you like to be contacted by, you may check all that apply:
Phone <input type="checkbox" name="Phone" value="checked">
Email <input type="checkbox" name="Email" value="checked">

That's a required field, they have to check at least one box, but they may check both boxes if both ways are okay with them.

----------------
Now, my web form contains a lot of other questions as well, such as asking for their name, email, phone, etc. And every single field on the form are required. So I need error checking for required fields so if users missed filling out a field, they would be prompted with an error message.

Am I making sense so far?

----------------
Let's say I am a user and I thought I filled out all my fields (without knowing that I left the "phone" field unfilled) and pressed the submit button to submit my information. Now, the error message comes up, telling me I have to fill out the "phone" field. But all the info I have filled out in those other fields are gone also. That's why in my previous posts, you see that I have some php code first checks each field one by one to see if it's filled out, if it is, assign that info to a variable.

Then if one field it's not filled out, the error message will show up, but for those other fields that have already been filled out, display the variables that have been assigned the values. So that the user only has to fill in the empty box, not every single one again.

If you look at my code above, I know how to write that to a textfield (and it works by my testing), but I don't know how to write that to two checkboxes.

IamStang

3:07 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



I have a lengthy form that I use and "my" method seems to work although it may not be the best.

I use a switch with the default being the code for the form as a visitor would see it on the first page load.

I have the default call for switch "chk" and do something like the following in that function.


// for a simple form with only a few fields
// can be modified for as many as you want
$textField1 = $_POST['textField1'];
$textField2 = $_POST['textField2'];
$checbox1 = $_POST['checkbox1'];
$checbox2 = $_POST['checkbox2'];

if($textField1 == ""){ $error = 1; $tf1 = "This is empty"; }

if($textField2 == ""){ $error = 1; $tf2 = "This is empty"; }

if(isset($_POST['checkbox1'])) { $cb1 = "checked"; }
elseif(isset($_POST['checkbox2'])) { $cb2 = "checked"; }
else { $error = 1; $cb = "Please check one"; }

if($error == 1){
?>
There were empty fields
<form>
<textfield1 value=<?=$textField1?> ><?=$tf1?>
<textfield2 value=<?=$textField2?> ><?=$tf2?>
<checkbox1 <?=$cb1?> ><checkbox2 <?=$cb2?> ><?=$cb?>
<?
}
else {
// continue to next page or whatever you need to do with the information }


Granted, the above code is not 100% complete as I shortened some things up. But it should give anyone with at least some knowledge of html/php a place to start. I was trying more to get the "method" across rather than write the code.

Hope it helps,
IamStang

H2O_aa

10:26 pm on Apr 11, 2005 (gmt 0)

10+ Year Member



Thank you IamStung. I finally got what I needed to work.

----------------------------
But can someone tell me why this is not working, but that (more below) works. The following two sets of code are complete code, so if it's easier for you, you may copy and paste them onto a doc to test them out.

<?PHP
if(isset($Phone) && isset($Email)) {
if($Phone!= "checked" && $Email!= "checked") {
print "missing field";
} else {
if($Phone == "checked") {
$cb1 = true;
}
if($Email == "checked") {
$cb2 = true;
}
}
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<form action="<?PHP echo $PHP_SELF;?>" method="post">

<?php if(isset($cb1)) {?>
Phone <input name="Phone" type="checkbox" value="checked" checked>
<?PHP } else {?>
Phone <input name="Phone" type="checkbox" value="checked">
<?PHP }?>

<?php if(isset($cb2)) {?>
Email <input name="Email" type="checkbox" value="checked" checked>
<?PHP } else {?>
Email <input name="Email" type="checkbox" value="checked">
<?PHP }?>

<p><input name="submit" type="submit" value="submit"></p>
</form>

</body>
</html>

-------------------------
Instead i have to add a hidden field in order for it to work. WHY?

<?PHP
if(isset($contact)) {
if($Phone!= "checked" && $Email!= "checked") {
print "missing field";
} else {
if($Phone == "checked") {
$cb1 = true;
}
if($Email == "checked") {
$cb2 = true;
}
}
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>

<form action="<?PHP echo $PHP_SELF;?>" method="post">

<input name="contact" type="hidden" value="">

<?php if(isset($cb1)) {?>
Phone <input name="Phone" type="checkbox" value="checked" checked>
<?PHP } else {?>
Phone <input name="Phone" type="checkbox" value="checked">
<?PHP }?>

<?php if(isset($cb2)) {?>
Email <input name="Email" type="checkbox" value="checked" checked>
<?PHP } else {?>
Email <input name="Email" type="checkbox" value="checked">
<?PHP }?>

<p><input name="submit" type="submit" value="submit"></p>
</form>

</body>
</html>

ironik

1:25 am on Apr 12, 2005 (gmt 0)

10+ Year Member



Instead i have to add a hidden field in order for it to work. WHY?

Checkbox values don't exist in the $_POST variable unless they are checked, so you need to use isset() on the checkbox post variables to see whether the user has clicked on it.

H2O_aa

5:54 am on Apr 12, 2005 (gmt 0)

10+ Year Member



ironik,

isset seems to be the problem in my case, not the other way around. I used isset on my first code to check if the checkbox variables exist and it doesn't work. But on my second code, when I defined a hidden input (not a checkbox), then it works.

H2O_aa

6:13 am on Apr 12, 2005 (gmt 0)

10+ Year Member



Yup, i did a simple test below, isset does NOT work on checkboxes. When you press the submit button, the following code only gives you "print". Why's that? I thought isset() only checks to see if a variable exists, if it does, it will simple return true. The variable "web" does exist, why doesn't it see it just because it's a checkbox?

<form name="form1" method="post" action="<?PHP echo $PHP_SELF;?>">
<input name="web" type="checkbox" value="">
<input name="print" type="text">
<input name="submit" type="submit">
</form>

<?PHP
if(isset($web))
print "web";
if(isset($print))
print "print";
?>

IamStang

11:33 am on Apr 12, 2005 (gmt 0)

10+ Year Member



H2)_aa wrote
The variable "web" does exist, why doesn't it see it just because it's a checkbox?

Well, I have spent quite a bit of time this morning researching your question for a concrete answer. However, I have failed to find one better than......

ironik wrote

Checkbox values don't exist in the $_POST variable unless they are checked.

Later.
IamStang