Jump to content

PHP & HTML HELP!


kyle01

Recommended Posts

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 by Ch0cu3r
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

 

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 by Ch0cu3r
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.