Jump to content

form that multiply values


Gatsu

Recommended Posts

Shouldnt this code give me 3 instead of 0 when I echo $total; ???

<?php <select name="1" type="post"><option value="1">1</option><option value="1">1</option></select><select name="2" type="post"><option value="2">2</option><option value="2">2</option></select> <?php$total = $_POST['1'] * $_POST['2'];echo $total; ?> ?>

Link to comment
Share on other sites

No, you can't use Post variables within the same page. And other than that, if you mean to sum the numbers use the + operator (2+1=3, not 2*1=3 --> 2*1=2)With PHP you'd need two pages:page1.html<form action="process.php" method="post"><select name="val1" ><option value="1">1</option><option value="1">1</option></select><select name="val2" type="post"><option value="2">2</option><option value="2">2</option></select> <input type="submit" /></form>process.php<?php$total = intval($_POST['val1']) + intval($_POST['val2']);echo $total;?> ?>Or you can use Javascript within the same page:<script type="textJavascript">function addition() {result = Number(document.getElementById("val1")) + Number(document.getElementById("val2"))document.getElementById("result").firstChild.nodeValue = result;}</script><select id="val1" onchange="addition" ><option value="1">1</option><option value="1">1</option></select><select id="val2" type="post"><option value="2">2</option><option value="2">2</option></select><span id="result"></span>

Link to comment
Share on other sites

There are a number of problems with your code.1) You've got nested <?php tags. You can scrap the outer set because all your php code is within the inner one.2) As Obi1 says, avoid starting a form component's name or id attribute with a number.3) Select tags don't have a type attribute, what you need to do is wrap all the form components in a form tag and then put a method="post" attribute in the form tag.4) Your form will also need a submit button which if you put in like this <input type="submit" name="submit" value="submit"> then that will help with the next point. Clicking the submit button will send the form's values to the location of the action attribute.5) By putting all your php code in the same page as your form, the php code will try to execute everytime that page loads. This will be a problem the first time it loads before you've submitted the form because $_POST[] won't have any value to retrieve. The simplest solution here is to use Ingolme's approach of a separate php file, but if you want it all in the same file post again here and someone'll let you know how.

<form action="process.php" method="post"><select name="s1"><option value="1">1</option><option value="1">1</option></select><select name="s2" type="post"><option value="2">2</option><option value="2">2</option></select><input type="submit" value="submit" name="submit"/></form>

<?php$total = $_POST['1'] * $_POST['2'];echo $total;?>

Link to comment
Share on other sites

hmm how could I have all the values between my choises to be added? like if I choose 1 and 20 I want all values in 1 to 20 to be + into one sum.this is what I use atm:

<?php$total = intval($_POST['val1']) + intval($_POST['val2']);$usd = $gp*3/10;echo "Price: ". $total ."k or $". $usd ." USD";echo "<br \>";echo "<br \>";echo "<br \>";echo "All prices will be discussed on msn!";?>

Link to comment
Share on other sites

You mean so if you choose 1 and 20 it will do 1+2+3+4+5+6+7+8...+18+19+20? You could use's Pascal's triangular number formula

$val1 = inval($_POST['val1']);$val2 = intval($_POST['val2']);$total = ($val2 * ($val2 + 1)) / 2 - ($val1 * ($val1 - 1)) / 2;//Etc

Link to comment
Share on other sites

Thats right, but when I use this I choose 1 to 20 with value 1000 on each number but I get 1000 instead of 20,000. how come?

<form action="process.php" method="post"><select name="val1"><option value="1000">1</option><option value="1000">2</option><option value="1000">3</option><option value="1000">4</option><option value="1000">5</option><option value="1000">6</option><option value="1000">7</option><option value="1000">8</option><option value="1000">9</option><option value="1000">10</option><option value="1000">11</option><option value="1000">12</option><option value="1000">13</option><option value="1000">14</option><option value="1000">15</option><option value="1000">16</option><option value="1000">17</option><option value="1000">18</option><option value="1000">19</option><option value="1000">20</option></select><select name="val2"><option value="1000">1</option><option value="1000">2</option><option value="1000">3</option><option value="1000">4</option><option value="1000">5</option><option value="1000">6</option><option value="1000">7</option><option value="1000">8</option><option value="1000">9</option><option value="1000">10</option><option value="1000">11</option><option value="1000">12</option><option value="1000">13</option><option value="1000">14</option><option value="1000">15</option><option value="1000">16</option><option value="1000">17</option><option value="1000">18</option><option value="1000">19</option><option value="1000">20</option></select> <input type="submit" value="Calculate"/></form>

Link to comment
Share on other sites

Because if you give an <option> a value attribute, then it posts the value in the value attribute and not the innertext. So val1 would = 1000 and val2 would also = 1000, and adding all the numbers between 1000 and 1000 = 1000. You would need to get rid of the value attribute and just multipy the $total by 1000 in the PHP part.

Link to comment
Share on other sites

It doesn't work because you gave all your options the same value, that's what Synook says.<option value="1000">1</option><option value="1000">2</option><option value="1000">3</option>No matter which option someone chooses the PHP script will only see 1000. It can't tell if they chose 1 or 2 or 3 because all of them have the same value. Your values need to match what the numbers are (1, 2, 3, etc), and in the PHP script you can multiply the value by 1000 if you want to use that.

Link to comment
Share on other sites

You're not understanding how this works. Since the "value" attribute of the options for 1 and 20 are both set to "1000", when you choose those options the server sees "1000" and "1000", not "1" and "20". If you want the server to see 1 and 20 instead of 1000 and 1000 then change the values from 1000 to 1 and 20.

Link to comment
Share on other sites

The problem is that if you choose 1 and 5 the server does not see 1 and 5, it sees 1000 and 1000. For ANY NUMBER YOU PICK the server ONLY sees 1000. It DOES NOT EVEN KNOW which number you chose, because the only value you send is 1000, for everything. If you want the server to see 1000 and 1000, fine, that's what it's doing now. If you want the server to actually see the numbers you chose then you need to change the values.

Link to comment
Share on other sites

Of course not, you can do anything you want to do. You just can't do it like that. I'm not sure exactly what you're trying to calculate though. You can subtract the first number from the second number to get the difference between them and multiply that by 1000, I'm just not sure what you're trying to calculate.

Link to comment
Share on other sites

If the numbers follow some formula, like in that case the dollar amount is (1+value)*5, then you can apply the formula to get the result. If the numbers don't follow a formula then you will need some list of all the values that you can refer to so that you know which values to add up together.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...