Jump to content

Recommended Posts

Hi, need help

I have 3 radio buttons and six checkboxes - the radio buttons have different dollar values but the checkboxes have the same dollar value.

 

my first form works great but I can't get the second form to calculate the radio button with the checkboxes...? really need help as the assignment is due in two days and I am getting desperate. Has to be in php tho.

 

Cheers

 

First page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> 
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="styles.css" rel="stylesheet" type="text/css">
  <title>Outdoor Adventures - Play Set Options</title>
</head>
<body>
  <h1>Outdoor Adventures - Play Set Options</h1>

  <p>
    Welcome to Outdoor Adventures. Complete the form below to calculate the cost of one of our play sets.
  </p>
  <form action="cost.php" method="post">
   	<table  width="750" border="0" cellpadding="0" cellspacing="0"> 
	<table  width="750" border="0" cellpadding="0" cellspacing="0"> 
	  <tr>
		<td width="88">First name: </td>
		<td width="662"><input type="text" id="first_name" name="first_name" size="30" maxlength="20" /></td>
	  </tr>
	  <tr>
		<td>Last name: </td>
		<td><input type="text" id="last_name" name="last_name" size="30" maxlength="20" /></td>
	  </tr>
	  <tr>
		<td>Phone: </td>
		<td><input type="text" id="phone" name="phone" size="10" maxlength="10" /></td>
	  </tr>
</table>
	<p>Base model play set:<br>
	  <input type="radio" id="single" name="base" value="Single"> <label for="single">Single<br />
	  <input type="radio" id="double" name="base" value="Double"> <label for="double">Double</label><br />
	  <input type="radio" id="monkey" name="base" value="Double with monkey bars"> <label for="monkey">Monkey</label><br />
	<p>Optional extra components:<br>
	  <input type="checkbox" id="MetalClimbBar" name="extra[0]" value="Metal climb bar" />
	  		<label for="MetalClimbBar">Metal climb bar</label><br />
	  <input type="checkbox" id="FiremanPole" name="extra[1]" value="Fireman pole" /> 
	  		<label for="FiremanPole">Fireman Pole</label><br />
	  <input type="checkbox" id="See-saw" name="extra[2]" value="See-saw" />
	  		<label for="See-saw">See-saw</label><br />
	  <input type="checkbox" id="Rock-climbingWall" name="extra[3]" value="Rock-climbing wall" /> 
	  		<label for="Rock-climbingWall">Rock-climbing Wall</label><br />
	  <input type="checkbox" id="Trapezeandrings" name="extra[4]" value="Trapeze and rings" /> 
	  		<label for="Trapezeandrings">Trapeze and rings combo</label><br />
	  <input type="checkbox" id="RopeLadder" name="extra[5]" value="Rope ladder" /> 
	  		<label for="RopeLadder">Rope Ladder</label><br />
	  
	</p>
	<p>
	  <input type="submit" value="Submit" > <input type="reset">
	</p>

  </form>
</body>
</html>


Second page:
<?php


// Variables for storing data and calculations
$errors = "";
$count = 0;
$cost = 0;


// Function to display errors
function errorMessage($errors) 			
{
	echo "<p>Your order could not be completed. Please check the errors below:<br />";
	echo "<ul>";
    	echo $errors;   						// Display error message
	echo "</ul>";
    	echo "Click the green \"back\" button to try again.<br />";
   }
  
	// if the form has been posted,analyse it, strip out any leading or trailing spaces:
 if(_POST)
{
	foreach ($_POST as $field_name => $value)
	{
		$value = trim($value); 
		$$field_name = $value; 
	} 
}

$extra_arr = array('Climb bar'=>100, 'Fireman pole'=>100, 'See-saw'=>100, 'Rock-climbing wall'=>100, 'Trapeze and rings'=>100,'Rope ladder'=>100);	

// Testing for input errors and nil entries in the various areas of the form

if($first_name == "" || is_numeric($first_name))
{
	$errors .= "<li>a first name needs to be entered</li><br />";
}
if($last_name == "" || is_numeric($last_name))
{
	$errors .= "<li>a last name needs to be entered</li><br />";
}
if($phone === "" || is_numeric($last_name))
{
	$errors .= "<li>a phone number needs to be entered</li><br />";
}


if($base == "")
{
	$errors .= "<li>a base needs to be selected</li><br />";
}
if($base == "Single" )
{
	$cost = 550;
}
if($base == "Double")
{
	$cost = 600;
}
if($base == "Double with monkey bars")
{
	$cost = 700;
}


  	for($i = 0; $i < 6; $i++)
{
	if($extra_arr[$i])
	{	
		$count = ++;
	}

	 }

// If no errors, display the form data
if($errors == "")
{
echo <<<END
<p>First name:  $first_name</p>
<p>Last name:  $last_name</p>
<p>Phone:  $phone</p>
<p>Base:   $base</p>
    Optional extra components:
<ul>
END;
  for($i = 0; $i < 6; $i++)
  {
	if($extra_arr[$i])
	{
echo <<<END
		<li>$extra_arr[$i]</li>			
END;
   		$cost++;
	 }
  }
echo <<<END
</ul>
    <p>Cost: $$cost</p>
END;
}
else						// If errors, display errors
{
	errorMessage($errors);
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/152963-calculating-radio-and-checkboxes/
Share on other sites

One easier way of displaying data using ECHO is...

     echo "<p>First name:  $first_name</p>
   <p>Last name:  $last_name</p>
   <p>Phone:  $phone</p>
   <p>Base:   $base</p>
    Optional extra components:
   <ul>";

 

There's no mention of any prices in the HTML (visible to the user) to show them what they're spending.

 

If I click "Double" it unselectes and goes back to Single being selected - missing </label>

        <input type="radio" id="single" name="base" value="Single"> <label for="single">Single</label><br />
        <input type="radio" id="double" name="base" value="Double"> <label for="double">Double</label><br />
        <input type="radio" id="monkey" name="base" value="Monkey"> <label for="monkey">Monkey</label><br />

I've added it for you.

 

I've also changed the value of monkey to Monkey - why have a long line of text?

 

As for the checkboxes I've edited the HTML slightly removing the square brackets:

        <input type="checkbox" id="MetalClimbBar" name="extra0" value="Metal climb bar" />
              <label for="MetalClimbBar">Metal climb bar</label><br />
        <input type="checkbox" id="FiremanPole" name="extra1" value="Fireman pole" />
              <label for="FiremanPole">Fireman Pole</label><br />
        <input type="checkbox" id="See-saw" name="extra2" value="See-saw" />
              <label for="See-saw">See-saw</label><br />
        <input type="checkbox" id="Rock-climbingWall" name="extra3" value="Rock-climbing wall" />
              <label for="Rock-climbingWall">Rock-climbing Wall</label><br />
        <input type="checkbox" id="Trapezeandrings" name="extra4" value="Trapeze and rings" />
              <label for="Trapezeandrings">Trapeze and rings combo</label><br />
        <input type="checkbox" id="RopeLadder" name="extra5" value="Rope ladder" />
              <label for="RopeLadder">Rope Ladder</label><br />

 

You can then run a for() loop with a switch() statement to check for those ticked:

for ($i=0;$i<6;++$i) {
  switch ($i) {
    case 0:if ($_POST['extra0']) {$total+=100;};break;
    case 1:if ($_POST['extra1']) {$total+=200;};break;
    case 2:if ($_POST['extra2']) {$total+=300;};break;
    case 3:if ($_POST['extra3']) {$total+=400;};break;
    case 4:if ($_POST['extra4']) {$total+=500;};break;
    case 5:if ($_POST['extra5']) {$total+=600;};break;
  }
}

That's presuming $total contains the total so far.

 

How's that?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> 
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="styles.css" rel="stylesheet" type="text/css">
  <title>Outdoor Adventures - Play Set Options</title>
</head>
<body>
  <h1>Outdoor Adventures - Play Set Options</h1>

  <p>
    Welcome to Outdoor Adventures. Complete the form below to calculate the cost of one of our play sets.
  </p>
  <form action="cost.php" method="post">
   	<table  width="750" border="0" cellpadding="0" cellspacing="0"> 
	<table  width="750" border="0" cellpadding="0" cellspacing="0"> 
	  <tr>
		<td width="88">First name: </td>
		<td width="662"><input type="text" id="first_name" name="first_name" size="30" maxlength="20" /></td>
	  </tr>
	  <tr>
		<td>Last name: </td>
		<td><input type="text" id="last_name" name="last_name" size="30" maxlength="20" /></td>
	  </tr>
	  <tr>
		<td>Phone: </td>
		<td><input type="text" id="phone" name="phone" size="10" maxlength="10" /></td>
	  </tr>
</table>
	<p>Base model play set:<br>
	  <input type="radio" id="single" name="base" value="Single"> <label for="single">Single<br />
	  <input type="radio" id="double" name="base" value="Double"> <label for="double">Double</label><br />
	  <input type="radio" id="monkey" name="base" value="Double with monkey bars"> <label for="monkey">Monkey</label><br />
	<p>Optional extra components:<br>
	  <input type="checkbox" id="MetalClimbBar" name="extra[0]" value="Metal climb bar" />
	  		<label for="MetalClimbBar">Metal climb bar</label><br />
	  <input type="checkbox" id="FiremanPole" name="extra[1]" value="Fireman pole" /> 
	  		<label for="FiremanPole">Fireman Pole</label><br />
	  <input type="checkbox" id="See-saw" name="extra[2]" value="See-saw" />
	  		<label for="See-saw">See-saw</label><br />
	  <input type="checkbox" id="Rock-climbingWall" name="extra[3]" value="Rock-climbing wall" /> 
	  		<label for="Rock-climbingWall">Rock-climbing Wall</label><br />
	  <input type="checkbox" id="Trapezeandrings" name="extra[4]" value="Trapeze and rings" /> 
	  		<label for="Trapezeandrings">Trapeze and rings combo</label><br />
	  <input type="checkbox" id="RopeLadder" name="extra[5]" value="Rope ladder" /> 
	  		<label for="RopeLadder">Rope Ladder</label><br />
	  
	</p>
	<p>
	  <input type="submit" value="Submit" > <input type="reset">
	</p>

  </form>
</body>
</html>


Second page:
<?php


// Variables for storing data and calculations
$errors = "";
$count = 0;
$cost = 0;


// Function to display errors
function errorMessage($errors) 			
{
	echo "<p>Your order could not be completed. Please check the errors below:<br />";
	echo "<ul>";
    	echo $errors;   						// Display error message
	echo "</ul>";
    	echo "Click the green \"back\" button to try again.<br />";
   }
  
	// if the form has been posted,analyse it, strip out any leading or trailing spaces:
 if($_POST)//
{
	foreach ($_POST as $field_name => $value)
	{
		$value = is_array($value) ? $value : trim($value); 
		$$field_name = $value; 
	} 
}

$extra_arr = array('Climb bar'=>100, 'Fireman pole'=>100, 'See-saw'=>100, 'Rock-climbing wall'=>100, 'Trapeze and rings'=>100,'Rope ladder'=>100);	

// Testing for input errors and nil entries in the various areas of the form

if($first_name == "" || is_numeric($first_name))
{
	$errors .= "<li>a first name needs to be entered</li><br />";
}
if($last_name == "" || is_numeric($last_name))
{
	$errors .= "<li>a last name needs to be entered</li><br />";
}
if($phone === "" || is_numeric($last_name))
{
	$errors .= "<li>a phone number needs to be entered</li><br />";
}


if($base == "")
{
	$errors .= "<li>a base needs to be selected</li><br />";
}
if($base == "Single" )
{
	$cost = 550;
}
if($base == "Double")
{
	$cost = 600;
}
if($base == "Double with monkey bars")
{
	$cost = 700;
}


/*  	for($i = 0; $i < 6; $i++)
{
	if($extra_arr[$i])
	{	
		$count++;//
	}

	 }
*/
$count = count($extra);
// If no errors, display the form data
if($errors == "")
{
echo <<<END
<p>First name:  $first_name</p>
<p>Last name:  $last_name</p>
<p>Phone:  $phone</p>
<p>Base:   $base</p>
    Optional extra components:
<ul>
END;

foreach ($extra as $name){
echo "<li>$name</li>\n";
$cost += $extra_arr[$name];
}

echo <<<END
</ul>
    <p>Cost: $$cost</p>
END;
}
else						// If errors, display errors
{
	errorMessage($errors);
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.