Jump to content

Archived

This topic is now archived and is closed to further replies.

pro

I'm beginner and I have question about the form

Recommended Posts

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 variables

how i can do that on the php code?

i hope you understand me  :-[

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
both your inputs are named cc

you 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
// 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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]
<?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));
?>
[/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...

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites

×

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.