lschena
08-17-2011, 02:46 AM
Hello,
Have mercy on me please, I am a total beginner. For an exercise/project, I want to make a form that a user would select one of two checkboxes, "Cappaccino" and "Latte" and the user would then select two radio buttons "Expensive" and "Inexpensive". Then they would click a "Check Form" button in which a box appears stating what they chose along with a suggestion of a coffee machine that meets their criteria.
The code I have below only states the selections the customer makes in the box after the "Check Form" is clicked but not a suggestion of a product. I wanted to reformat it into what I wanted but got stuck and could not even make it work as the original source had it working not sure why. This is as far as I got and below my experimental code is the original source code I got from a book. Please help and try to be a specific as possible as I am a complete beginner and am trying to understand this as best as I can.
If it is too difficult to show me how to get it working as I wanted with the suggestion of a product, how then can I get it to work as the original method of just showing the users choices only in the box after "Check Form" is clicked? Help please-I am having a tough time with this!
Here is my code I am working with as a template but can't get it to work in any way after the Check form button is clicked:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cappaccino or Coffee Maker Features</title>
<style type="text/css">
body {
margin:0;
padding:40px;
background:#fff;
font:100% Arial, Helvetica, sans-serif;
color:#555;
line-height:180%;
}
h1{
font-size:180%;
font-weight:normal;
color:#555;
}
h2{
clear:both;
font-size:160%;
font-weight:normal;
color:#555;
margin:0;
padding:.5em 0;
}
a {
text-decoration:none;
color:#f30;
}
p {
clear:both;
margin:0;
padding:.5em 0;
}
pre {
display:block;
font:100% "Arial";
padding:10px;
border:1px solid #bae2f0;
background:#e3f4f9;
margin:.5em 0;
overflow:auto;
width:800px;
}
</style>
<script type="text/javascript">
var machineTypeIndex = 0;
function machineType_onclick(machIndex)
{
var returnValue = true;
if (machIndex == 1)
{
returnValue = false;
alert("Sorry that cappaccino machine is currently unavailable");
// Next line works around a bug in IE that doesn’t cancel the
// Default action properly
document.form1.machineType[machineTypeIndex].checked = true;
}
else
{
machineTypeIndex = machIndex;
}
return returnValue;
}
function btnCheck_onclick()
{
var controlIndex;
var element;
var numberOfControls = document.form1.length;
var compSpec = "Your chosen cappaccino machine is ";
compSpec = compSpec + document.form1.machineType[machineTypeIndex].value;
compSpec = compSpec + "\nWith the following additional features\n";
for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)
{
element = document.form1[controlIndex];
if (element.type == "checkbox")
{
if (element.checked == true)
{
compSpec = compSpec + element.value + "\n";
}
}
}
alert(compSpec);
}
</script>
</head>
<body>
<form action="" name="form1">
<p>
Tick all of the features you want included on your machine
</p>
<table>
<tr>
<td>
Cappaccino Maker
</td>
<td>
<input type="checkbox" name="chkCappa" value="Cappaccino" />
</td>
</tr>
<tr>
<td>
Latte Maker
</td>
<td>
<input type="checkbox" name="chkLatte" value="Latte" />
</td>
</tr>
</table>
<p>
Select the price range you would like
</p>
<table>
<tr>
<td>
<input type="radio" name="pricerange" checked="checked"
onclick="return pricerange_onclick(0)" value="budget" />
</td>
<td>
Budget
</td>
<td>
<input type="radio" name="pricerange" value="expensive"
onclick="return pricerange_onclick(1)" />
</td>
<td>
Expensive
</td>
</tr>
</table>
<input type="button" value="Check Form" name="btnCheck"
onclick="return btnCheck_onclick()" />
</form>
</body>
</html>
Original Source Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chapter 7: Example 6</title>
<script type="text/javascript">
var radCpuSpeedIndex = 0;
function radCPUSpeed_onclick(radIndex)
{
var returnValue = true;
if (radIndex == 1)
{
returnValue = false;
alert("Sorry that processor speed is currently unavailable");
// Next line works around a bug in IE that doesn’t cancel the
// Default action properly
document.form1.radCPUSpeed[radCpuSpeedIndex].checked = true;
}
else
{
radCpuSpeedIndex = radIndex;
}
return returnValue;
}
function btnCheck_onclick()
{
var controlIndex;
var element;
var numberOfControls = document.form1.length;
var compSpec = "Your chosen processor speed is ";
compSpec = compSpec + document.form1.radCPUSpeed[radCpuSpeedIndex].value;
compSpec = compSpec + "\nWith the following additional components\n";
for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)
{
element = document.form1[controlIndex];
if (element.type == "checkbox")
{
if (element.checked == true)
{
compSpec = compSpec + element.value + "\n";
}
}
}
alert(compSpec);
}
</script>
</head>
<body>
<form action="" name="form1">
<p>
Tick all of the components you want included on your computer
</p>
<table>
<tr>
<td>
DVD-ROM
</td>
<td>
<input type="checkbox" name="chkDVD" value="DVD-ROM" />
</td>
</tr>
<tr>
<td>
CD-ROM
</td>
<td>
<input type="checkbox" name="chkCD" value="CD-ROM" />
</td>
</tr>
<tr>
<td>
Zip Drive
</td>
<td>
<input type="checkbox" name="chkZip" value="ZIP Drive" />
</td>
</tr>
</table>
<p>
Select the processor speed you require
</p>
<table>
<tr>
<td>
<input type="radio" name="radCPUSpeed" checked="checked"
onclick="return radCPUSpeed_onclick(0)" value="3.8 GHz" />
</td>
<td>
3.8 GHz
</td>
<td>
<input type="radio" name="radCPUSpeed" value="4.8 GHz"
onclick="return radCPUSpeed_onclick(1)" />
</td>
<td>
4.8 GHz
</td>
<td>
<input type="radio" name="radCPUSpeed" value="6 Ghz"
onclick="return radCPUSpeed_onclick(2)" />
</td>
<td>
6 GHz
</td>
</tr>
</table>
<input type="button" value="Check Form" name="btnCheck"
onclick="return btnCheck_onclick()" />
</form>
</body>
</html>
Thanks a bunch!
Laurie
Have mercy on me please, I am a total beginner. For an exercise/project, I want to make a form that a user would select one of two checkboxes, "Cappaccino" and "Latte" and the user would then select two radio buttons "Expensive" and "Inexpensive". Then they would click a "Check Form" button in which a box appears stating what they chose along with a suggestion of a coffee machine that meets their criteria.
The code I have below only states the selections the customer makes in the box after the "Check Form" is clicked but not a suggestion of a product. I wanted to reformat it into what I wanted but got stuck and could not even make it work as the original source had it working not sure why. This is as far as I got and below my experimental code is the original source code I got from a book. Please help and try to be a specific as possible as I am a complete beginner and am trying to understand this as best as I can.
If it is too difficult to show me how to get it working as I wanted with the suggestion of a product, how then can I get it to work as the original method of just showing the users choices only in the box after "Check Form" is clicked? Help please-I am having a tough time with this!
Here is my code I am working with as a template but can't get it to work in any way after the Check form button is clicked:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cappaccino or Coffee Maker Features</title>
<style type="text/css">
body {
margin:0;
padding:40px;
background:#fff;
font:100% Arial, Helvetica, sans-serif;
color:#555;
line-height:180%;
}
h1{
font-size:180%;
font-weight:normal;
color:#555;
}
h2{
clear:both;
font-size:160%;
font-weight:normal;
color:#555;
margin:0;
padding:.5em 0;
}
a {
text-decoration:none;
color:#f30;
}
p {
clear:both;
margin:0;
padding:.5em 0;
}
pre {
display:block;
font:100% "Arial";
padding:10px;
border:1px solid #bae2f0;
background:#e3f4f9;
margin:.5em 0;
overflow:auto;
width:800px;
}
</style>
<script type="text/javascript">
var machineTypeIndex = 0;
function machineType_onclick(machIndex)
{
var returnValue = true;
if (machIndex == 1)
{
returnValue = false;
alert("Sorry that cappaccino machine is currently unavailable");
// Next line works around a bug in IE that doesn’t cancel the
// Default action properly
document.form1.machineType[machineTypeIndex].checked = true;
}
else
{
machineTypeIndex = machIndex;
}
return returnValue;
}
function btnCheck_onclick()
{
var controlIndex;
var element;
var numberOfControls = document.form1.length;
var compSpec = "Your chosen cappaccino machine is ";
compSpec = compSpec + document.form1.machineType[machineTypeIndex].value;
compSpec = compSpec + "\nWith the following additional features\n";
for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)
{
element = document.form1[controlIndex];
if (element.type == "checkbox")
{
if (element.checked == true)
{
compSpec = compSpec + element.value + "\n";
}
}
}
alert(compSpec);
}
</script>
</head>
<body>
<form action="" name="form1">
<p>
Tick all of the features you want included on your machine
</p>
<table>
<tr>
<td>
Cappaccino Maker
</td>
<td>
<input type="checkbox" name="chkCappa" value="Cappaccino" />
</td>
</tr>
<tr>
<td>
Latte Maker
</td>
<td>
<input type="checkbox" name="chkLatte" value="Latte" />
</td>
</tr>
</table>
<p>
Select the price range you would like
</p>
<table>
<tr>
<td>
<input type="radio" name="pricerange" checked="checked"
onclick="return pricerange_onclick(0)" value="budget" />
</td>
<td>
Budget
</td>
<td>
<input type="radio" name="pricerange" value="expensive"
onclick="return pricerange_onclick(1)" />
</td>
<td>
Expensive
</td>
</tr>
</table>
<input type="button" value="Check Form" name="btnCheck"
onclick="return btnCheck_onclick()" />
</form>
</body>
</html>
Original Source Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chapter 7: Example 6</title>
<script type="text/javascript">
var radCpuSpeedIndex = 0;
function radCPUSpeed_onclick(radIndex)
{
var returnValue = true;
if (radIndex == 1)
{
returnValue = false;
alert("Sorry that processor speed is currently unavailable");
// Next line works around a bug in IE that doesn’t cancel the
// Default action properly
document.form1.radCPUSpeed[radCpuSpeedIndex].checked = true;
}
else
{
radCpuSpeedIndex = radIndex;
}
return returnValue;
}
function btnCheck_onclick()
{
var controlIndex;
var element;
var numberOfControls = document.form1.length;
var compSpec = "Your chosen processor speed is ";
compSpec = compSpec + document.form1.radCPUSpeed[radCpuSpeedIndex].value;
compSpec = compSpec + "\nWith the following additional components\n";
for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)
{
element = document.form1[controlIndex];
if (element.type == "checkbox")
{
if (element.checked == true)
{
compSpec = compSpec + element.value + "\n";
}
}
}
alert(compSpec);
}
</script>
</head>
<body>
<form action="" name="form1">
<p>
Tick all of the components you want included on your computer
</p>
<table>
<tr>
<td>
DVD-ROM
</td>
<td>
<input type="checkbox" name="chkDVD" value="DVD-ROM" />
</td>
</tr>
<tr>
<td>
CD-ROM
</td>
<td>
<input type="checkbox" name="chkCD" value="CD-ROM" />
</td>
</tr>
<tr>
<td>
Zip Drive
</td>
<td>
<input type="checkbox" name="chkZip" value="ZIP Drive" />
</td>
</tr>
</table>
<p>
Select the processor speed you require
</p>
<table>
<tr>
<td>
<input type="radio" name="radCPUSpeed" checked="checked"
onclick="return radCPUSpeed_onclick(0)" value="3.8 GHz" />
</td>
<td>
3.8 GHz
</td>
<td>
<input type="radio" name="radCPUSpeed" value="4.8 GHz"
onclick="return radCPUSpeed_onclick(1)" />
</td>
<td>
4.8 GHz
</td>
<td>
<input type="radio" name="radCPUSpeed" value="6 Ghz"
onclick="return radCPUSpeed_onclick(2)" />
</td>
<td>
6 GHz
</td>
</tr>
</table>
<input type="button" value="Check Form" name="btnCheck"
onclick="return btnCheck_onclick()" />
</form>
</body>
</html>
Thanks a bunch!
Laurie