pro Posted September 3, 2006 Share Posted September 3, 2006 Hi everybody,sorry, I have silly question because I'm still beginner in php programming and sorry for my bad English :-[my question is:for example,I have an HTML form [code]<html><body><form method="post" action="x.php"><h3> variable1 </h3><input type="checkbox" name="cc" value ="on" /><h3> variable2 </h3><input type="checkbox" name="cc" value ="on" /><BR /><input type="submit" value="send" /></form></body></html>[/code]and i want to add a values to variable 1 and 2 (numbers) that will be calculated on the php code (x.php)for example when you check on variable1 and variable2 and then depress send, the result should be a calculation of these variableshow i can do that on the php code?i hope you understand me :-[ Quote Link to comment Share on other sites More sharing options...
radar Posted September 3, 2006 Share Posted September 3, 2006 Well if I am understanding you correctly it would be something like this..[code]<?php$num = $_POST;$res = $cc + $cc1;echo $res;?>[/code]Also -- you might make your check boxes named different... otherwise they will overwrite each other... Quote Link to comment Share on other sites More sharing options...
mort Posted September 3, 2006 Share Posted September 3, 2006 both your inputs are named ccyou should name 1 cc1 and the other cc2[code]<?php$cc1 = $_POST['cc1'];$cc2 = $_POST['cc2'];$num = $cc1 + $cc2;echo "$num";?>[/code]that should do the trick Quote Link to comment Share on other sites More sharing options...
.josh Posted September 3, 2006 Share Posted September 3, 2006 uh, i'd also like to point out that you have a default value of "on" in your input boxes, so they will be auto-filled with "on". if you press submit without entering in numbers, the ^ code will try to do "on" + "on" which obviously won't work. Quote Link to comment Share on other sites More sharing options...
pro Posted September 3, 2006 Author Share Posted September 3, 2006 // thanx radar and mort for reminde me, I fogot to change the names :)// Crayon, in this time I have to specify numbers for the check books?!I will add more to my questionif I add two more checkbox on the HTML file and I want for evryone a numberfor examplecc1 = 2cc2 = 3cc3 = 4cc4 = 5and i want to choose 2 or 3 of them randomlywhere i heve to add these values so that next i will use (IF) function \\ Crayon>>\\ in this time there is no problem for the default value of "on" ?right? Quote Link to comment Share on other sites More sharing options...
radar Posted September 3, 2006 Share Posted September 3, 2006 Well you see the value submits what is passed through to the php script.. so if each one has a value of on -- then when you take cc1 + cc2 you are going to either get an error or get onon which probably isnt what you want...So if you decide you want to add 2 more check boxes you would do something like this.. first off have the value of checkbox the number you want them to equal...I am working on a code that will allow you to add as many check boxes as you would like. Quote Link to comment Share on other sites More sharing options...
radar Posted September 3, 2006 Share Posted September 3, 2006 Okay -- I was going to do a large setup using arrays and stuff from the form to determine how many were selected, etc.. though at this time i am brain dead so this will have to work...[code]<?phpif (!$_POST) {print_r ($_POST);echo "error";} else {if ($_POST[checkbox] ) {$checkbox = $_POST[checkbox];} if ($_POST[checkbox2] ) {$checkbox2 = $_POST[checkbox2];} if ($_POST[checkbox3] ) {$checkbox3 = $_POST[checkbox3];} if ($_POST[checkbox4] ) {$checkbox4 = $_POST[checkbox4];} }echo (($checkbox) + ($checkbox2) + ($checkbox3) + ($checkbox4));?>[/code]Now one thing I was having trouble with is i named my check boxes cc, cc2, cc3 and cc4.. though when i typed this..print_r ($_POST);it gave me the name as checkbox -- so at the beginning of your processing script I would add the print_r line i just typed and see what it puts your name as.. after that just put whatever that is into the code and it should work.. take if statements out if need to or add some in... if you add any in you'll have to add them into the echo field as well with () around them... Quote Link to comment Share on other sites More sharing options...
.josh Posted September 3, 2006 Share Posted September 3, 2006 simply remove the value="on" from your input tags. that whole value=" " business is just there to populate the field with a default value. just remove it altogether, unless you want it to be some default value, like 1 or something. then change the "on" to "1" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.