2 Replies - 352 Views - Last Post: 31 October 2013 - 07:09 PM Rate Topic: -----

#1 joetp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-October 13

Total the cost value of input form

Posted 31 October 2013 - 04:30 PM

I have an input form that POSTS to self. I'm trying to calculate the amount of the selected items. But can't!! Why?


<?php

/////////////////Posted input
$race1 = $_POST['$race1'];
$race2 = $_POST['$race2'];
$splash = $_POST['$splash'];
$bothswims = $_POST['$bothswims'];
$flipthrash = $_POST['$flipthrash'];	
$bikeride = $_POST['$bikeride'];	
$biketrans = $_POST['$biketrans'];	
$funrun = $_POST['$funrun'];	
$funrunfam = $_POST['$funrunfam'];	
	
////////////Declare variable values	
$race1=20;
$race2=30;
$splash=40;
$bothswims=-10;
$flipthrash=15;
$bikeride=40;
$biketrans=20
$funrun=10;
$funrunfam=20;

	
$race1_total = $race1	
$race2_total = $race2	
$splash_total = $splash	
$bothswims_total = $bothswims
$flipthrash_total = $flipthrash
$bikeride_total = $bikeride
$biketrans_total = $biketrans
$funrun_total = $funrun
$funrunfam_total = $funrunfam


/////Calculate Total $amt

 $amt = $race1_total + $race2_total + $splash_total + $bothswims_total + $flipthrash_total + $bikeride_total + $biketrans_total + $funrun_total + $funrunfam_total;

?>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<p align="left">&nbsp;</p>
<form action="total.php" method="post" enctype="multipart/form-data">
  <p align="left">
</td>
            </tr>   
            <tr>
              <td colspan="2" align="right" class="alignRt"><h3 align="left">600m Dash </h3>
                <p align="left"><strong>Juniors</strong><br />
                  <label for="1">10 - 13 Years ($20.00)</label>                 
                  <input type="radio" name="race1" id="race1" value="race1" />
                  
                  <label for="2">14 - 17 Years ($20.00)</label>                  
                  <input type="radio" name="race1" id="race1" value="race2" />
                  
                                    <br />
                    <br />
                    <strong>Seniors</strong><br />
                  <label for="3">18 - 29  Years ($30.00)</label>               
                  <input type="radio" name="race2" id="race3" value="race3" />
                  <label for="4">30 - 39  Years ($30.00)</label>                 
                  <input type="radio" name="race2" id="race4" value="race4" />
                  
                  <label for="5">40 - 49  Years ($30.00)</label>                                    
                  <input type="radio" name="race2" id="race5" value="race5" />
                
                
                  
                  <label for="6">50 - 59  Years ($30.00)</label>                  
                  <input type="radio" name="race2" id="race6" value="race6" />
                  
                  <label for="7">60 - 69  Years ($30.00)</label>                                   
                  <input type="radio" name="race2" id="race7" value="race7" />
                  
                  <label for="8">70 +  Years ($30.00)</label>
                  <input type="radio" name="race2" id="race8" value="race8" />
                </p></td>
            </tr>   
            <tr>
              <td colspan="2" align="left" class="alignRt"><h3 align="left">1200m Splash</h3>
                
                <label for="splash"> 1200m Splash ($40.00)</label>
                <input name="splash" type="checkbox" id="splash" value="Entered" /></p></td>
            </tr>
            <tr>
              <td colspan="2" align="left" class="alignRt">
                <label for="bothswims">($10.00) Discount for Entering: Both 600m Dash and 1200m Splash</label>           
                <input name="bothswims" type="checkbox" id="BothSwims" value="Discount" />
                
               </td>
            </tr>
            <tr>
              <td colspan="2" align="left" class="alignRt"><p>
                <label for="flipthrash">Flipper Thrash over 12 years ($15.00)</label>
                <input type="checkbox" name="flipthrash" id="flipthrash" value="Entered"/>
                
               </td>
            </tr>
            <tr>
              <td colspan="2" align="left" class="alignRt"><p>
   
                  
                  <label for="bikeride"> Bike Ride ($40.00)</label>
                  <input type="checkbox" name="bikeride" id="bikeride" value="Entered"/>
                  
                  
                  <label for="biketrans">Bike Transport ($20.00) </label>
                  <input type="checkbox" name="biketrans" id="biketrans" value="Entered" />
                  
                </td>
            </tr>
             <tr>
              <td colspan="2" align="left" class="alignRt"><p>
                  <label for="funrun">Fun Run ($10.00)</label>
                  <input type="checkbox" name="funrun" id="funrun" value="Entered"/>


                  <label for="funrun">Family Fun Run  2 Adults + 3 Children ($20.00)</label>
                  <input type="checkbox" name="funrunfam" id="fam" value="Entered"/>
                  
                 
                </td>
            </tr>
            <td><p><br />
                <input type="submit" name="Submit" value="Submit Form" />
              </p></td>
          </form>


$<?php print $amt; ?>
</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Total the cost value of input form

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Total the cost value of input form

Posted 31 October 2013 - 07:07 PM

"But can't!!" isn't much of a problem description. Explain what is happening, and what you were expecting to happen. What is going wrong? Are you getting any errors? (Is error reporting even turned on?)

Also keep in mind that the $_POST array is only populated after a POST form is posted to the page. Meaning that during the first run of the script you posted; before the form submits it to itself, the array will have no values. Yet, your code seems to assume the array will always have values. - Look into the isset() and empty() functions.
Was This Post Helpful? 1
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 702
  • View blog
  • Posts: 3,027
  • Joined: 03-December 12

Re: Total the cost value of input form

Posted 31 October 2013 - 07:09 PM

I dont know what values you are trying to pass, but you do realize that when you do this:

////////////Declare variable values	
$race1=20;
$race2=30;
$splash=40;
$bothswims=-10;
$flipthrash=15;
$bikeride=40;
$biketrans=20
$funrun=10;
$funrunfam=20;


That is replaces these:
/////////////////Posted input
$race1 = $_POST['$race1'];
$race2 = $_POST['$race2'];
$splash = $_POST['$splash'];
$bothswims = $_POST['$bothswims'];
$flipthrash = $_POST['$flipthrash'];	
$bikeride = $_POST['$bikeride'];	
$biketrans = $_POST['$biketrans'];	
$funrun = $_POST['$funrun'];	
$funrunfam = $_POST['$funrunfam'];	


and that your $_POST arrays will stay empty because they are trying to get values from the names of the HTML that is posting them not the variable used on the server-side.

What happened to the code you had going a few days ago?

This post has been edited by astonecipher: 31 October 2013 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1