[Year 12 SofDev] PHP and checkboxes and radio buttons
Margaret Iaquinto
iaquinto at ozemail.com.au
Sun Feb 10 17:47:07 EST 2013
I plan to use these files to show structured input.
-------------- next part --------------
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Form</title>
<link rel="stylesheet" type = "text/css" media="screen" href="blue.css" >
</head>
<body>
<header>
<h1>using checkboxes</h1>
<hgroup>
<h4> </h4>
<h4></h4>
</hgroup>
</header>
<form class="contact_form" action="checkboxes.php" method="post" name="contact_form">
<ul>
<li> <!-- checkboxes -- fieldset and legend optional -->
<fieldset class ="checkbox">
<legend>Do you need wheelchair access? </legend>
<label for="chkAccess"> <input type="checkbox" name="chkAccess" class = "checkbox " value = "Yes" />Yes</label>
</fieldset>
</li>
<li> <!-- checkboxes -- fieldset and legend optional -->
<fieldset class ="checkbox">
<legend>Would you like to be on our mailing list? </legend>
<p><label for="mail"><input type="checkbox" name="chkMail" class = "checkbox" value = "Yes" />Yes</label>
</fieldset>
</li>
<li>
<button class="submit" type="submit">Submit Form</button>
</li>
</ul>
</form>
<footer>
<a href="checkboxes.html">Main Menu </a>
</footer>
-----
<?php
/************
file: checkboxes.php
called from checkboxes.html
Single check box
Allows the user to try again.
date_default_timezone_set("Australia/Melbourne");
echo ("You took this survey on: ");
echo date("l, jS F Y");
echo " and the time was ";
echo date("H:i.s");
echo ("<br /><hr />");
echo ("<br />");
****************/
include('library.inc');
page_header('opinions');
if(isset($_POST['chkAccess']) && $_POST['chkAccess'] == 'Yes')
{
echo "You need wheelchair access.";
echo ("<br />");
}
if(isset($_POST['chkMail']) && $_POST['chkMail'] == 'Yes')
{
echo "Thanks for joining our mailing list.";
}
else
{
echo "We will not add you to our mailing list";
}
page_footer('VK3CFI');
?>
----
<?php
/*
file:library.inc
contains page_header and page_footer functions.
*/
function page_header ($title)
{
echo "<html> \n";
echo "<head> \n";
echo " <title> $title </title>\n";
echo " <link rel=\"stylesheet\" href=\"blue.css\" type=\"text/css\">";
echo " </head>";
echo "<body> \n";
}
function page_footer ($yourname)
{
echo "<footer>";
echo "<a href=\"checkboxes.html\">Select again</a>";
echo "<hr />";
echo "© $yourname 2013 <a href=\"mailto:$yourname at gmail.com\">Contact Webmaster</a>";
echo "<hr />";
echo "</footer>";
echo "</body> \n </html>\n";
}
?>
</body>
</html>
-------------- next part --------------
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Using radio buttons</title>
<link rel="stylesheet" type = "text/css" media="screen" href="blue.css" >
</head>
<body>
<header>
<h1>Radio Buttons</h1>
<hgroup>
<h4> </h4>
<h4></h4>
</hgroup>
</header>
<form class="contact_form" action="radio.php" method="post" name="contact_form">
<ul>
<li> <!-- when programming radio buttons --input id must be unique -- match id value to label for value -->
<fieldset >
<legend>Maths subjects </legend>
<p><input id="0" type="radio" name="radMaths" class = "regular-radio" value = "specialist" checked /> <label for="0">Specialist</label> </p>
<p><input id="1" type="radio" name="radMaths" class = "regular-radio"value = "statistics"/>
<label for="1">Statistics</label> </p>
<p><input id="2" type="radio" name="radMaths" class = "regular-radio"value = "Boolean algebra"/>
<label for="2">Boolean algebra</label</p>
</fieldset>
<fieldset >
<legend>Are you an immigrant or a native born Aussie?</legend>
<p><input id="3" type="radio" name="radStatus" class = "regular-radio" value = "3" checked /> <label for="3">Dinky di Aussie</label> </p>
<p><input id="4" type="radio" name="radStatus" class = "regular-radio" value = "4" />
<label for="4">New Australian</label> </p>
</fieldset>
</li>
<li>
<button class="submit" type="submit">Submit Form</button>
</li>
</ul>
</form>
<footer>
<a href="radio.html">Main Menu </a>
</footer>
</body>
</html>
---
<?php
/*********
file: radio.php called from radio.html
must use isset function
*********/
include('library.inc');
page_header('using radio buttons');
/**********
These posted variables work EXCEPT when no choice is made.
Better to use isset function to provide robustness.
$maths = $_POST['maths'];
$status = $_POST['status'];
****/
if(isset($_POST['radMaths']))
{
$maths = $_POST['radMaths'];
echo "Your favourite maths subject is $maths.<br />";
}
if(isset($_POST['radStatus']))
{
$status = $_POST['radStatus'];
echo "Your status is $status<br />";
echo "Notice how this output of $status does not make much sense at this point but, with further programming, it does<br />";
if($status == '1')
{
echo "You are a native born Australian.";
}
else
{
echo "You were born overseas.";
}
}
page_footer('VK3CFI');
---
<?php
/*
file:library.inc
contains page_header and page_footer functions.
*/
function page_header ($title)
{
echo "<html> \n";
echo "<head> \n";
echo " <title> $title </title>\n";
echo " <link rel=\"stylesheet\" href=\"blue.css\" type=\"text/css\">";
echo " </head>";
echo "<body> \n";
}
function page_footer ($yourname)
{
echo "<footer>";
echo "<a href=\"radio.html\">Select again</a>";
echo "<hr />";
echo "© $yourname 2013 <a href=\"mailto:$yourname at gmail.com\">Contact Webmaster</a>";
echo "<hr />";
echo "</footer>";
echo "</body> \n </html>\n";
}
?>
?>
More information about the sofdev
mailing list