I am having a little problem:

I am pretty "wet behind the ears" in the PHP world, so please, bare with me...I am tasked with writing a script that does two things:

(1) Allows a user to enter a temperature and based on a selection from a radio button, converts the temperature to either celcius or fahrenheit.

(2) Writes the output to another php page.

My problem lies when selecting either the celcius or fahrenheit radio button. How do I get either, based on selection, to write to the output? Do I need to write if/else statment or can I use case/switch?

Any and all advice is welcome. Remember, I am not at the advanced level yet, so please, bring yourself down a level or two (i.e. put yourself back at the beginning when you were first starting to develop... =) )

Here's my code for the output:

<?php

$mytempEntry=$_POST["tempEntry&qu ot;];
$convCelTemp=$_POST["convCel" ;];
$convFahTemp=$_POST["convFah" ;];

$convCelTemp=($mytempEntry - 32) *.55;
#echo $convCelTemp;
$convFahTemp=($mytempEntry * 1.8) + 32;
#echo $convFahTemp;

$celconvMsg=Celcius;
$fahconvMsg=Fahrenheit;

?>
<!doctype html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>
<!-- Author: David Lindsey -->
<!-- ITEC2171 Mid-Term Page 2 -->
<!-- Last Revision: 3/7/05 -->

<title>Temperature Conversion 2</title>
<style>
body
{
font-family:tahoma,verdana,arial;
font-size:15pt;
}
h1
{
text-align:left;
}
</style>
</head>

<body>
<h1>Temperature Conversion</h1>

<p>The temperature you entered was <?php echo $_POST["tempEntry"]; ?>.</p>

<p>That is equivalent to <?php printf("%2.0f",$convCelTemp) ?> <?php echo $celconvMsg; ?>.</p>

<a href='convertTemp.php'?>Return to Temperature Page</a>

</body>
</html>

Thanks...

(1) Allows a user to enter a temperature and based on a selection from a radio button, converts the temperature to either celcius or fahrenheit.

(2) Writes the output to another php page.

My problem lies when selecting either the celcius or fahrenheit radio button. How do I get either, based on selection, to write to the output? Do I need to write if/else statment or can I use case/switch?

I don't see your form, but sounds like either case or if else will do the trick. Imagining you are sending a radio button called temp_format
<INPUT type="radio" name="temp_format" value="c"> Use Celcius
<INPUT type="radio" name="temp_format" value="f"> Use Farenheit

Then in your php page you would just

if ($temp_format == 'c') { // or use the "proper" $_POST["temp_format"]

// show the celcius version

} else {

// show the farenheit version
}

You're not asking us to do your homework are you? ;)

Thanks barnamos...ok, I am busted. The gig is up. Actually, I understand alot, but every once in a while my mind goes blank. I think we've all had that happen. An additional objective is that if the user enters a number and selects Convert to Celcius, then the message on the convertScript page will read: The temperature you entered was Fahrenheit. That is equivalent to x Celcius.

I am more inclined to use case/switch for this one. What is your take?

Thanks again...

Here's the form:

<!doctype html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>


<title>Temperature Conversion 1</title>


<style>
body
{
font-family:tahoma,verdana,arial;
font-size:15pt;
}
h1
{
text-align:left;
}
</style>
</head>


<body>
<h1>Temperature Conversion</h1>


<form
name='myForm'
id='myForm'
method='post'
action='convertScript.php'
onsubmit=''
onreset="">


<p>Enter a temperature:
<input
type='text'
name='tempEntry'
id='tempEntry'
/><br>
</p>


<input
type='radio'
name='temp'
checked='checked'
value='Celcius' /> Convert to Celcius <br>


<input
type='radio'
name='temp'
value='Fahrenheit'/> Convert to Fahrenheit<br><br>



<input type='reset'
name='clear_form'
value='Reset Temperature'
/>



<input type='submit'
name='btnSubmit'
id='btnSubmit'
value='Convert'
/>


</form>
</body>
</html>
<input
type='radio'
name='temp'
checked='checked'
value='Celcius' /> Convert to Celcius <br>

<input
type='radio'
name='temp'
value='Fahrenheit'/> Convert to Fahrenheit<br><br>

The above means that you need to look for the post variable 'temp' like so:

$convTemp = $_POST['temp'];

Then whenever you reference $convTemp:

if($convTemp == 'Fahrenheit') { do stuff }
if($convTemp == 'Celcius') {do stuff }
if(($convTemp != 'Fahrenheit') && ($convTemp !='Celcius')) { die('we have a big problem!'); }

you will see Fahrenheit or Celcius, whichever the user picked.

If I remember correctly I read somewhere (can't remember where) that if-then-else statements are faster than case-switch statements, but as you are only dealing with 2 options it probably doesn't matter which you use as long as it works.

Well, thanks to both of you, barnamos and Dance Instructor. Based on what you provided me with, I was able to figure out what I needed to do. I tested and it worked!!! Like I side, sometimes your brain goes in other directions. Actually, I sat down and wrote out my vars, etc, and then it came to me where each var needed to go in the if/else statements and then w/in my php tags.

Happy trails and again, many thanks!!!

venetian_jigsaw

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.