Forum Moderators: coopster
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 }?>
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).
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.
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.
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 }
Hope it helps,
IamStang
----------------------------
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>
<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";
?>
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