2

I am trying to convert deg -> c and c -> deg using php. The objective is to use functions containing the formulas and then having a form with radio buttons and a text box. The user should be able to enter the degree in the text box, click the radio button of their choice (F or C) and then submit the form to receive their conversion. I have seen similar posts but the methodology is not specific to the issue I am having.

I have updated my code but am now getting the "White page of death" Can anyone see an error that I cannot see? Thank you!

HTML

<h1>Temperature Conversion</h1>



<form action='lab_exercise_6.php' method ='POST'>
    <p>Enter a temp to be converted and then choose the conversion type below.</p>

<input type ='text' maxlength='3' name ='calculate'/>

 <p>Farenheit
  <input type="radio" name='convertTo' value="f" />
</p>

<p>Celsius
  <input type="radio" name='convertTo' value="c" />
</p>

<input type='submit' value='Convert Temperature' name='convertTo'/>

PHP

<?php

//function 1

function FtoC($deg_f) {
    return ($deg_f - 32) * 5 / 9;
  }

  //function 2
function CtoF($deg_c){
    return($deg_c + 32) * 9/5;
}

if( isset($_POST['convertTo']) && $_POST['convertTo'] ==="c" ){
$farenheit = FtoC($deg_f);
print('This temperature in FARENHEIT is equal to ' . $celsius . ' degrees celsius! </br>');
}else if(isset($_POST['convertTo'])&& $_POST['convertTo']==='f'){
    $celsius = CtoF($deg_c);
    print('This temperature in CELSIUS is equal to ' . $farenheit . ' degrees farenheit! </br>');

}

?>
CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49
  • You didn't add any form of validation. You should check if values exist and if they are numbers. You can do this in two ways and perfect scenario is to implement them in both ways: php and js. JS for validating before sending a form and php for validating after sending form to the server. – spectatorx Aug 02 '18 at 13:02

3 Answers3

2

You haven't added values to the radio boxes.

Then change your radio name same as the other so for example

<p>Farenheit
    <input type = 'radio' name ='temp_type' value='f'/>
</p>

<p>Celsius
    <input type = 'radio' name ='temp_type' value='c'/>
</p>

Now you can access those with PHP.

if($_POST['temp_type'] && ($_POST['calculate']){
  $temp2calculate = $_POST['calculate'];
  $temp_type = $_POST['temp_type'];
}
WKoppel
  • 504
  • 3
  • 15
1

it's this part that makes it behave unexpected:

$farenheit = FtoC($deg_f);
// And then a few lines lower:
if(isset($farenheit)){ /* ... */}

You set it to a value right there, so it will always try to calculate an outcome.
With a small tweak you will use the radiobutton more as it is intended:

<input type="radio" name="convertTo" value="f" />
<input type="radio" name="convertTo" value="c" />

The value in PHP will now always have the same name, you can now just check it's value:

if( isset($_POST['convertTo']) && $_POST['convertTo']==="c" ){
    $farenheit = FtoC($deg_f);
    print('This temperature in FARENHEIT is equal to ' . $celsius . ' degrees celsius! </br>');
}
Martijn
  • 15,791
  • 4
  • 36
  • 68
  • this helped but now when I add a value in the text box, choose celsius as my radio button and click submit, the page just returns blank and the print statement does not render. Any ideas? – CodeConnoisseur Aug 02 '18 at 13:17
  • It's called a White Page Of Death: https://stackoverflow.com/questions/1475297/phps-white-screen-of-death – Martijn Aug 02 '18 at 13:20
  • I am new to PHP, first I am hearing of this white page of death! But thank you for sharing I will try to troubleshoot. – CodeConnoisseur Aug 02 '18 at 13:29
  • 1
    basically it: You have an error, but the default setting is 'dont show errors', which is the proper setting for a live server, but ennoying while testing – Martijn Aug 02 '18 at 13:51
  • I updated my code, do you see any errors that are blatant in my code? – CodeConnoisseur Aug 03 '18 at 01:23
  • Hi. You are not supposed to update your answer. Someone elke might hve the same problem and our answers make no sense anymore. Please revert them :) – Martijn Aug 03 '18 at 07:25
  • But yes, You've also named the button 'convertTo', which doesnt have the value C or F, so no output. For debugging it always helps to have a 'last case catcher', in your case a last `else` after your elseif – Martijn Aug 03 '18 at 07:27
1

You have to give same name for the radio buttons

    <p>Farenheit
        <input type = 'radio' value ='farenheit' name='units'/>
    </p>

    <p>Celsius
        <input type = 'radio' value ='celsius' name='units'/>
    </p>

Check whether which radio button is selected:

<?php
$units = $_POST['units'];
if($units == "fahrenheit"){
 //call function to convert to fahrenheit
}
else{
  //call function to convert to celsius
}
?>
Sandra
  • 418
  • 1
  • 5
  • 14