apg1985 Posted January 15, 2009 Share Posted January 15, 2009 Hi Guys, If you could help me that would be great, its very important for a project and needed ASAP. I've got a form with select fields and 1 label at the very end. The first select field you can choose £1000 or £2000, once you select one of them it shows up in the label at the end. After that select field is another select field with yes and no, if you choose yes nothing happens but if you choose no it takes 10% away from the label, after that select field is another select field the same as the yes and no one but when you select no this time it adds £100. I've add the form below and also added a name for each selection which I hope helps. Any direction would really help. <form> <table> <tr> <td>Select value</td> <td> <select name="select1"> <option name=”1000”>£1000</option> <option name=”2000”>£2000</option> </select> <td> </tr> <tr> <td>Select Yes or No</td> <td> <select name="select2"> <option name=”yes1”>Yes</option> <option name=”no1”>No</option> </select> <td> </tr> <tr> <td>Select Yes or No</td> <td> <select name="select3"> <option name=”yes2”>Yes</option> <option name=”no2”>No</option> </select> <td> </tr> <label name=”label”/> </form> Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/ Share on other sites More sharing options...
Daniel0 Posted January 15, 2009 Share Posted January 15, 2009 Uh... Isn't that basic arithmetic? $price = (float) $_POST['select1']; if ($_POST['select2'] == 'yes') { $price *= 1.1; } if ($_POST['select3'] == 'yes') { $price += 100; } You need to give your <option>s some values though... Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-737905 Share on other sites More sharing options...
akitchin Posted January 15, 2009 Share Posted January 15, 2009 you need to multiply by 0.9, not 1.1. but that's all you'd need to do, for sure. Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-737931 Share on other sites More sharing options...
Daniel0 Posted January 15, 2009 Share Posted January 15, 2009 Somehow I read it as "add 10%". Sorry about that. Same kind of logic though. Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-737947 Share on other sites More sharing options...
apg1985 Posted January 15, 2009 Author Share Posted January 15, 2009 Thanks for doing that. It is basic arithmetic but because im new to php I find it quite hard to put the calculation from paper into code. Stupid question but got to ask, is $price the label? Why do I need to give the options a value is it to clean the code up or for a different reason (just to help give me a better understanding of what going on)? O and do you mean like <option name="name" value="value"> Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-737977 Share on other sites More sharing options...
Daniel0 Posted January 15, 2009 Share Posted January 15, 2009 Why do I need to give the options a value is it to clean the code up or for a different reason (just to help give me a better understanding of what going on)? If they do not have any value then you aren't sending anything. It's just a matter of doing e.g. <option value="1000">$1000</option> essentially. Stupid question but got to ask, is $price the label? $price was set to $_POST['select1'], i.e. the first select in your code. Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-737979 Share on other sites More sharing options...
apg1985 Posted January 15, 2009 Author Share Posted January 15, 2009 So its just say whatever value is selected in select1 put in $price which is the label, then the rest of the php code is changing the figure in $price (label). Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-737983 Share on other sites More sharing options...
Daniel0 Posted January 15, 2009 Share Posted January 15, 2009 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-737988 Share on other sites More sharing options...
apg1985 Posted January 16, 2009 Author Share Posted January 16, 2009 I'm testing this now and for some reason uits not working, please help, its not taking the selected number and showing it in the input field at the end ($price) CODE: <!-- START EDITABLE1 --> <?php $price = (float) $_POST['select1']; if ($_POST['select2'] == 'yes') { $price *= 0.9; } if ($_POST['select3'] == 'yes') { $price += 100; } ?> <form method="post" name="form1" id="form1"> <table width="500px"> <tr> <td>Select value</td> <td> <select name="select1"> <option name="1000">£1000</option> <option name="2000">£2000</option> </select> <td> </tr> <tr> <td>Select Yes or No</td> <td> <select name="select2"> <option name="yes1">yes</option> <option name="no1">no</option> </select> <td> </tr> <tr> <td>Select Yes or No</td> <td> <select name="select3"> <option name="yes2">yes</option> <option name="no2">no</option> </select> <td> </tr> <tr> <td><input type="button" value="Submit"/></td> <td><input type="text" name="price" id="price"/></td> </tr> </table> </form> <!-- END EDITABLE1 --> Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-738232 Share on other sites More sharing options...
Mark Baker Posted January 16, 2009 Share Posted January 16, 2009 It won't display it by magic... only if you tell it to: <td><input type="text" name="price" id="price" value="<?php echo $price; ?>" /></td> Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-738234 Share on other sites More sharing options...
apg1985 Posted January 16, 2009 Author Share Posted January 16, 2009 ok cool that kinda worked its putting a value in the price field but its a 0 not the value selected Any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-738237 Share on other sites More sharing options...
Mark Baker Posted January 16, 2009 Share Posted January 16, 2009 ok cool that kinda worked its putting a value in the price field but its a 0 not the value selected Yes, it's not submitting the form <td><input type="submit" value="Submit"/></td> Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-738245 Share on other sites More sharing options...
apg1985 Posted January 16, 2009 Author Share Posted January 16, 2009 See the button isnt part of it, all I want is the user to be able to select a number say £1000 and that to appear in the input box at the end. Do the rest of the selections and while that is happening %'s are being taken off the number in the input box at end. When the submit button is clicked its just going to take the final value in that input box and email it to me. Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-738254 Share on other sites More sharing options...
Mark Baker Posted January 16, 2009 Share Posted January 16, 2009 See the button isnt part of it, all I want is the user to be able to select a number say £1000 and that to appear in the input box at the end. Do the rest of the selections and while that is happening %'s are being taken off the number in the input box at end. When the submit button is clicked its just going to take the final value in that input box and email it to me. PHP can only do the calculation if a request is sent to the server. If you want to do this calculation without submitting a request to the server, then you'll need to use javascript, or other client-side scripting language to perform the mathematics. Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-738264 Share on other sites More sharing options...
apg1985 Posted January 16, 2009 Author Share Posted January 16, 2009 Dam, Ok well would it be possible to do what i want if it did the php on submission like through the button So like <form action="loadphp.php"> and in the loadphp page I had <?php $price = (float) $_POST['select1']; if ($_POST['select2'] == 'yes1') { $price *= 0.9; } if ($_POST['select3'] == 'yes2') { $price += 100; } if(isset($_POST['submit'])) { $to = "my email address"; $subject = "FORM"; $price = $_POST['price']; $body = "$price"; header("Location: index.php"); mail($to, $subject, $body); } else { echo ("Not Sent"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-738281 Share on other sites More sharing options...
apg1985 Posted January 16, 2009 Author Share Posted January 16, 2009 Ok this is just a update of my code, i think were nearly there. Ive got the submit button working and its sending the number in the price field to my email address but its showing a 0. HTML: <?php $price = (float) $_POST['select1']; if ($_POST['select2'] == 'yes1') { $price *= 0.9; } if ($_POST['select3'] == 'yes2') { $price += 100; } ?> <form action="1testformphp.php" method="post" name="form1" id="form1"> <table width="500px"> <tr> <td>Select value</td> <td> <select name="select1"> <option name="1000" value="1000">1000</option> <option name="2000" value="2000">2000</option> </select> <td> </tr> <tr> <td>Select Yes or No</td> <td> <select name="select2"> <option name="selectbox">select box</option> <option name="yes1" value="yes1" id="yes1">yes1</option> <option name="no1" value="no1" id="yes1">no1</option> </select> <td> </tr> <tr> <td>Select Yes or No</td> <td> <select name="select3"> <option name="yes2" value="yes2" id="yes2">yes2</option> <option name="no2" value="no2" id="no2">no2</option> </select> <td> </tr> <tr> <td><input type="text" name="price" id="price" value="<?php echo $price; ?>"></td> <td><input type="submit" name="submit" value="Submit"/></td> </tr> </table> 1testformphp.php (email php) <?php if(isset($_POST['submit'])) { $to = ""; $subject = "FORM"; $price = $_POST['price']; $body = "$price"; header("Location: index.php"); mail($to, $subject, $body); } else { echo ("Not Sent"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140983-need-help-with-maths-in-a-form-please-help-very-important/#findComment-738283 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.