From previous,we already learn the radio button and for this post,we will learn the checkboxes.For the script of checkbox is quite similar with radio button.First we view on PHP script first:
<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';
if (isset($_POST['SubmitOne']))
{
if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}
}
?>
So,first we unchecked the obj1 and obj2.then we check to see if the Submit button was clicked:
if (isset($_POST['SubmitOne'])) {
}
if (isset($_POST['obj1'])) {
}
this statement are checking if a checkbox was set it is because if we did not check if the checkboxes are set or not,we will deal with a lot of errors,worse is "Undefined" errors.
if ($obj1 == 'male') {
$obj1 = 'checked';
}
.Here is the simple example...
"If the value inside of the variable called $obj1 is 'male' then change the status of obj1 to checked
The second of the if statements are the same – one for each checkbox on the form.
Ok,done for PHP script,ok for HTML form:
<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Boy
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Girl
<P>
<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>
<Input type = 'Radio' Name ='gender' value= 'male' <?PHP print $male_status; ?>>Male
Just a little change,that all..
Finally make the new file named CheckBoxes.php
The code:
<html>
<head>
<title>Lessons for Checkboxes</title>
<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';
if (isset($_POST['SubmitOne'])) {
if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}
}
?>
</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">
<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Male
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Female
<P>
<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>
</body>
</html>
<head>
<title>Lessons for Checkboxes</title>
<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';
if (isset($_POST['SubmitOne'])) {
if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}
}
?>
</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">
<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Male
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Female
<P>
<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>
</body>
</html>
N/NOTE: good luck!question?
No comments:
Post a Comment