Gatsu Posted November 15, 2007 Share Posted November 15, 2007 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 More sharing options...
Ingolme Posted November 15, 2007 Share Posted November 15, 2007 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 More sharing options...
Obi1-Cannabis Posted November 15, 2007 Share Posted November 15, 2007 i don't think a 'name' or an 'id' can start whith a number try using <select name="p1"... and <select name="p2"...$total = $_POST['p1']*$_POST['p2'];btw 1*2 = 2 and not 1*2= 3. Link to comment Share on other sites More sharing options...
Gatsu Posted November 15, 2007 Author Share Posted November 15, 2007 thank you it worked =) Link to comment Share on other sites More sharing options...
Pollux Posted November 15, 2007 Share Posted November 15, 2007 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 More sharing options...
Gatsu Posted November 15, 2007 Author Share Posted November 15, 2007 I fixed it Link to comment Share on other sites More sharing options...
Gatsu Posted November 15, 2007 Author Share Posted November 15, 2007 sorry forget this post Link to comment Share on other sites More sharing options...
Gatsu Posted November 18, 2007 Author Share Posted November 18, 2007 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 More sharing options...
Synook Posted November 18, 2007 Share Posted November 18, 2007 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 More sharing options...
Gatsu Posted November 18, 2007 Author Share Posted November 18, 2007 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 More sharing options...
Synook Posted November 19, 2007 Share Posted November 19, 2007 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 More sharing options...
Gatsu Posted November 19, 2007 Author Share Posted November 19, 2007 that doesent work because the options contineu to 70 and the values increases the higher it gets. 21-30 would be value="1500"I just made it shorter in forum to make it easyer to read. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 19, 2007 Share Posted November 19, 2007 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 More sharing options...
Gatsu Posted November 19, 2007 Author Share Posted November 19, 2007 but its the value that I want to be printed. as if I choose 1 and 20 it should calculate 1+2+3+4+5+6+7+8...+18+19+20 = $total of all values. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 19, 2007 Share Posted November 19, 2007 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 More sharing options...
Gatsu Posted November 19, 2007 Author Share Posted November 19, 2007 I want the server to see 1000 and 1000 and I want it to + them togeather and all the 1000 inbetween so if I choose 1 and 5 I get 1000+1000+1000+1000+1000 and this is what I mean with 1+2+3+4+5 Link to comment Share on other sites More sharing options...
justsomeguy Posted November 19, 2007 Share Posted November 19, 2007 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 More sharing options...
Gatsu Posted November 19, 2007 Author Share Posted November 19, 2007 ooooh! I understand =( so its impossible to reach my goal here? :/ Link to comment Share on other sites More sharing options...
justsomeguy Posted November 19, 2007 Share Posted November 19, 2007 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 More sharing options...
Gatsu Posted November 19, 2007 Author Share Posted November 19, 2007 I'll try to explain.take a look at http://www.planet-zix.se/leveling/price.phpI want to make every numer worth different values, the higher number the higher value, not number*1000.example:number 1 = $10 usdnumber 2 = $15 usdnumber 3 = $20 usechoose 1 and 3 and it calculates 10+15+20=$total Link to comment Share on other sites More sharing options...
justsomeguy Posted November 19, 2007 Share Posted November 19, 2007 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 More sharing options...
Gatsu Posted November 19, 2007 Author Share Posted November 19, 2007 ok I'll try some more now when I understand, thank you Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.