Jump to content


Photo

I'm beginner and I have question about the form


  • Please log in to reply
7 replies to this topic

#1 pro

pro
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationUAE

Posted 03 September 2006 - 07:38 PM

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

<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>


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 variables

how i can do that on the php code?

i hope you understand me  :-[



#2 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 03 September 2006 - 07:46 PM

Well if I am understanding you correctly it would be something like this..

<?php
$num = $_POST;
$res = $cc + $cc1;
echo $res;
?>

Also -- you might make your check boxes named different...  otherwise they will overwrite each other...

#3 mort

mort
  • Members
  • PipPipPip
  • Advanced Member
  • 82 posts

Posted 03 September 2006 - 07:58 PM

both your inputs are named cc

you should name 1 cc1 and the other cc2

<?php
$cc1 = $_POST['cc1'];
$cc2 = $_POST['cc2'];

$num = $cc1 + $cc2;

echo "$num";
?>

that should do the trick

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 September 2006 - 08:09 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 pro

pro
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationUAE

Posted 03 September 2006 - 08:47 PM

// 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 question

if I add two more checkbox on the HTML file and I want for evryone a number

for example

cc1 = 2

cc2 = 3

cc3 = 4

cc4 = 5

and i want to choose 2 or 3 of them randomly


where 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?

#6 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 03 September 2006 - 09:18 PM

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.

#7 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 03 September 2006 - 10:00 PM

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...

<?php
if (!$_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));
?>

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...

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 September 2006 - 10:03 PM

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"
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users