kyle01 Posted November 20, 2014 Share Posted November 20, 2014 (edited) Hi there, I have this conversion program I am trying to make. Basically I have one file called 'index.html' and another called 'convert.php', the basic function of this program is to show a form in the html page and then get the calculation from the php form and then show the result back on the html page. The html page uses a dropdown list of different conversions the user can select from. Below is the code I've got. Please can you help with fixing this thing! It's really annoying me! :@ HTML CODE: <HTML> <HEAD> <title>Unit Converter</title> </HEAD> <BODY> <h1>CONVERSION</h1> <form action="convert.php" method="post"> <link rel="StyleSheet" media="screen" href="convert.css"> Type of conversion: <br> <select name="conversionType"> <option value="null" selected="selected">Select type...</option> <option value="null"></option> <option value="null">--Temperature--</option> <option value="C-F">Celsius to Fahrenheit</option> <option value="C-K">Celsius to Kelvin</option> <option value="C-R">Celsius to Rankine</option> <option value="K-C">Kelvin to Celsius</option> <option value="K-F">Kelvin to Fahrenheit</option> <option value="K-R">Kelvin to Rankine</option> <option value="F-C">Fahrenheit to Celsius</option> <option value="F-K">Fahrenheit to Kelvin</option> <option value="F-R">Fahrenheit to Rankine</option> <option value="R-C">Rankine to Celsius</option> <option value="R-K">Rankine to Kelvin</option> <option value="R-F">Rankine to Fahrenheit</option> </select> <br><br> Value to convert:<br> <input type="text" name="conversionInput"> <br><br> <input type="submit" value="Convert"> </form> </BODY> </HTML> PHP CODE: <HTML> <title>Conversion Output</title> </HTML> <?php $conversionType = $_POST['conversionType']; $conversionInput = $_POST['conversionInput']; $conversionValue; $conversionFrom; $conversionTo; if ($conversionType == 'C-F') { $conversionValue = $conversionInput * (5/9) + 32; $conversionFrom = "° Celsius"; $conversionTo = "° Fahrenheit"; } if ($conversionType == 'C-K') { $conversionValue = $conversionInput + 273.15; $conversionFrom = "° Celsius"; $conversionTo = "° Kelvin"; } if ($conversionType == 'C-R') { $conversionValue = ($conversionInput + 273.15) * (9/5); $conversionFrom = "° Celsius"; $conversionTo = "° Rankine"; } if ($conversionType == 'K-C') { $conversionValue = $conversionInput - 273.15; $conversionFrom = "° Kelvin"; $conversionTo = "° Celsius"; } if ($conversionType == 'K-F') { $conversionValue = $conversionInput * (9/5) - 459.67 ; $conversionFrom = "° Kelvin"; $conversionTo = "° Fahrenheit"; } if ($conversionType == 'K-R') { $conversionValue = $conversionInput * (9/5); $conversionFrom = "° Kelvin"; $conversionTo = "° Rankine"; } if ($conversionType == 'F-C') { $conversionValue = ($conversionInput - 32) * (5/9) ; $conversionFrom = "° Fahrenheit"; $conversionTo = "° Celsius"; } if ($conversionType == 'F-K') { $conversionValue = ($conversionInput + 459.67) * (5/9); $conversionFrom = "° Fahrenheit"; $conversionTo = "° Kelvin"; } if ($conversionType == 'F-R') { $conversionValue = $conversionInput + 459.67; $conversionFrom = "° Fahrenheit"; $conversionTo = "° Rankine"; } if ($conversionType == 'R-C') { $conversionValue = ($conversionInput + 491.67) * (5/9); $conversionFrom = "° Rankine"; $conversionTo = "° Celsius"; } if ($conversionType == 'R-K') { $conversionValue = $conversionInput * (5/9); $conversionFrom = "° Rankine"; $conversionTo = "° Kelvin"; } if ($conversionType == 'R-F') { $conversionValue = $conversionInput - 459.67; $conversionFrom = "° Rankine"; $conversionTo = "° Fahrenheit"; } echo "$conversionInput $conversionFrom = $conversionValue $conversionTo"; ?> Edited November 20, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 20, 2014 Share Posted November 20, 2014 Please can you help with fixing this thing! It's really annoying me! Ok. So what issues are you having? Also please wrap code in tags or click the <> button in the editor when posting code! (I have edited your post) Quote Link to comment Share on other sites More sharing options...
kyle01 Posted November 20, 2014 Author Share Posted November 20, 2014 Ah sorry, new to this website, just signed up now, but thank you. Basically, the program runs, as in the html form shows up in Google Chrome, but when i enter a integer into the field and press convert. it just goes to a blank html page and thats about it. I need the form to show the converted result on the html page. Thank you Quote Link to comment Share on other sites More sharing options...
Barand Posted November 20, 2014 Share Posted November 20, 2014 Check your formulae. C-F should be $conversionValue = $conversionInput * (9/5) + 32; You have (5/9). I haven't checked any other Quote Link to comment Share on other sites More sharing options...
kyle01 Posted November 20, 2014 Author Share Posted November 20, 2014 Check your formulae. C-F should be $conversionValue = $conversionInput * (9/5) + 32; You have (5/9). I haven't checked any other Thank you, can you please help with any other problems with this code, it's kind of an emergency Quote Link to comment Share on other sites More sharing options...
kyle01 Posted November 20, 2014 Author Share Posted November 20, 2014 also as a side note; I am using notepad++ to edit the code if that has any importance Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 20, 2014 Share Posted November 20, 2014 It looks basically okay to me. Your submit form HTML is not well formed, and you might want to check that out. Also, no need to "declare" $conversionValue, $conversionFrom, and $conversionTo at the top. You should also consider some basic validation to ensure $conversionType exists (yes, I know you used a select menu so not a huge deal) and that the submitted values are numbers. Quote Link to comment Share on other sites More sharing options...
kyle01 Posted November 20, 2014 Author Share Posted November 20, 2014 It looks basically okay to me. Your submit form HTML is not well formed, and you might want to check that out. Also, no need to "declare" $conversionValue, $conversionFrom, and $conversionTo at the top. You should also consider some basic validation to ensure $conversionType exists (yes, I know you used a select menu so not a huge deal) and that the submitted values are numbers. Could you help by editing the code i have submitted or just submitting the new version of the changed code, I am very new to this html and php and don't really have much of a clue as to what I am doing. Thank you Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 20, 2014 Share Posted November 20, 2014 (edited) it just goes to a blank html page and thats about it. When it goes blank and you right click > view source do you see the PHP code? If you do then you have not setup PHP on your server or you are loading the file directly into the browser (the address bar will start with file://) You need to run your PHP code on a server that is configured with PHP. If you are testing this code on your computer then you can install a package such as WAMP or XAMPP which will setip everything for you. You can then test your code by accessing http://localhost/ Edited November 20, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
kyle01 Posted November 20, 2014 Author Share Posted November 20, 2014 When it goes blank and you right click > view source do you see the PHP code? If you do then you have not setup PHP on your server or you are loading the file directly into the browser (the address bar will start with file://) You need to run your PHP code on a server that is configured with PHP. If you are testing this code on your computer then you can install a package such as WAMP or XAMPP which will setip everything for you. You can then test your code by accessing http://localhost/ so is everything alright with the actual code?.. I am just simply using notepad++ and then running the index.html page off of it to see what it looks like. is this an okay way of doing it or is that why I am getting the blank page? Quote Link to comment Share on other sites More sharing options...
kyle01 Posted November 20, 2014 Author Share Posted November 20, 2014 Thank you everyone. It is now working Quote Link to comment Share on other sites More sharing options...
mik_se7 Posted November 20, 2014 Share Posted November 20, 2014 I cannot understand why you would want to convert and show the answer on a different page? you could divide your page into div's and have a results div at the bottom of the page near the submit button. you could then either have the php code above the HTML on the same page or use the include function to include the php stored on another page. that way you would have the user select conversion > submit form > php converts > answer appears in result div. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 20, 2014 Share Posted November 20, 2014 I shortened your code for you <?php $result = ''; if (isset($_POST['conversionType'])) { $conversionType = $_POST['conversionType']; $conversionInput = $_POST['conversionInput']; $conversionValue = 0; list ($from, $to) = explode('-', $conversionType); $scales = array ( 'C' => 'Celcius', 'F' => 'Fahrenheit', 'K' => 'Kevin', 'R' => 'Rankine' ); define('DEGREES', '°'); switch ($conversionType) { case 'C-F': $conversionValue = $conversionInput * (9/5) + 32; break; case 'C-K': $conversionValue = $conversionInput + 273.15; break; case 'C-R': $conversionValue = ($conversionInput + 273.15) * (9/5); break; case 'K-C': $conversionValue = $conversionInput - 273.15; break; case 'K-F': $conversionValue = $conversionInput * (9/5) - 459.67; break; case 'K-R': $conversionValue = $conversionInput * (9/5); break; case 'F-C': $conversionValue = ($conversionInput - 32) * (5/9); break; case 'F-K': $conversionValue = ($conversionInput + 459.67) * (5/9); break; case 'F-R': $conversionValue = $conversionInput + 459.67; break; case 'R-C': $conversionValue = ($conversionInput + 491.67) * (5/9); break; case 'R-F': $conversionValue = $conversionInput - 459.67; break; case 'R-K': $conversionValue = $conversionInput * (5/9); break; } $result = sprintf('%s%s %s = %0.2f%s %s', $conversionInput, DEGREES, $scales[$from], $conversionValue, DEGREES, $scales[$to]); } ?> <html> <head> <title>Unit Converter</title> </head> <body> <h1>CONVERSION</h1> <form action="" method="post"> <link rel="StyleSheet" media="screen" href="convert.css"> Type of conversion: <br> <select name="conversionType"> <option value="null" selected="selected">Select type...</option> <option value="null"></option> <option value="null">--Temperature--</option> <option value="C-F">Celsius to Fahrenheit</option> <option value="C-K">Celsius to Kelvin</option> <option value="C-R">Celsius to Rankine</option> <option value="K-C">Kelvin to Celsius</option> <option value="K-F">Kelvin to Fahrenheit</option> <option value="K-R">Kelvin to Rankine</option> <option value="F-C">Fahrenheit to Celsius</option> <option value="F-K">Fahrenheit to Kelvin</option> <option value="F-R">Fahrenheit to Rankine</option> <option value="R-C">Rankine to Celsius</option> <option value="R-K">Rankine to Kelvin</option> <option value="R-F">Rankine to Fahrenheit</option> </select> <br><br> Value to convert:<br> <input type="text" name="conversionInput"> <br><br> <input type="submit" value="Convert"> </form> <br><br> <?php echo $result;?> </body> </html> 1 Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 20, 2014 Share Posted November 20, 2014 I shortened your code for you Yea, it was kind of bugging me too... Quote Link to comment 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.