checkbox calculating - in php form

Discussion in 'PHP' started by Lucy, Apr 15, 2007.

  1. #1
    Hi,

    Was hoping someone out there could help with this problem.

    I have creating a form with an array of 7 checkboxes.

    I am trying to write a script that will calculate a cost of $1 for every checkbox chosen.

    Please help! :)
     
    Lucy, Apr 15, 2007 IP
  2. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Do you want it in php if yes then while checking each check box just submit the form and update it.

    This code may too some extend help you :

    Any more help needed always welcome :)
     
    Subikar, Apr 15, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    
    <html>
    <head></head>
    <!-- I am a free download : http://www.prototypejs.org/download -->
    <script language="javascript" src="prototype.js"></script>
    <!-- I am some simple prototype usage -->
    <script language="javascript">
    	function calculate(  )
    	{
    		var rets = 0;
    		var data = $('mainform').getInputs( 'checkbox' ).toArray();
    		data.each( function(item) { if( item.checked ) { rets += parseInt( item.value ) } } ); 
    		$('result').innerHTML = 'Total : $' + rets + '<br />';
    		$('opt_value').value = rets;
    	}
    </script>
    <body>
    <div id="result"></div>
    <form action="" method="post" name="mainform" id="mainform">
    <input type="checkbox"  name="opt[]" value="5" onclick="calculate( );"/><br />
    <input type="checkbox"  name="opt[]" value="1" onclick="calculate( );"/><br />
    <input type="checkbox"  name="opt[]" value="5" onclick="calculate( );"/><br />
    <input type="checkbox"  name="opt[]" value="1" onclick="calculate( );"/><br />
    <input type="checkbox"  name="opt[]" value="1" onclick="calculate( );"/><br />
    <input type="checkbox"  name="opt[]" value="1" onclick="calculate( );"/><br />
    <input type="checkbox"  name="opt[]" value="1" onclick="calculate( );"/><br />
    <input type="checkbox"  name="opt[]" value="1" onclick="calculate( );"/><br />
    <!-- NO SUBMIT BUTTON NEEDED, SUM CALCULATED ON EACH CHANGE AND STORED IN opt_value -->
    <input type="hidden" name="opt_value" id="opt_value" value="" />
    </form>
    </body>
    </html>
    
    HTML:
    Uses prototype.js link inside, also allows you to have different values assigned to every checkbox in an array......
     
    krakjoe, Apr 16, 2007 IP